diff --git a/config/config.go b/config/config.go index 076c45f..084f55c 100644 --- a/config/config.go +++ b/config/config.go @@ -16,12 +16,12 @@ type Config struct { FriendAddPolicy string TokenFile string DeviceFile string - MsgRetry int } var ( Instance *Config Session *chatgpt.Session + Browser *chatgpt.Browser Chats = make(map[int64]*chatgpt.Chat) ) @@ -35,7 +35,6 @@ func init() { FriendAddPolicy: os.Getenv("QQ_CHAT_GPT_POLICY"), TokenFile: "qq.token", DeviceFile: "device.json", - MsgRetry: 3, } if Instance.AIUsername != "" && Instance.AIPassword != "" && Instance.Key != "" { Session = chatgpt.NewSessionWithCredential(Instance.AIUsername, Instance.AIPassword, Instance.Key).AutoRefresh() @@ -46,8 +45,4 @@ func init() { if err == nil { Instance.QQ = qq } - msgRetry, err := strconv.Atoi(os.Getenv("QQ_MSG_RETRY")) - if err == nil && msgRetry > 0 { - Instance.MsgRetry = msgRetry - } } diff --git a/event/groupmessage.go b/event/groupmessage.go index 7f394e5..1fb3857 100644 --- a/event/groupmessage.go +++ b/event/groupmessage.go @@ -12,21 +12,17 @@ func GroupMessage(client *client.QQClient, event *message.GroupMessage) { isAt, content := helpers.GetTextElementFromElements(client.Uin, event.Elements) if isAt { if _, ok := config.Chats[event.Sender.Uin]; !ok { - config.Chats[event.Sender.Uin] = chatgpt.NewChat(config.Session) + config.Chats[event.Sender.Uin] = chatgpt.NewChat(config.Browser, config.Session) } - for i := 0; i < config.Instance.MsgRetry; i++ { - res, err := config.Chats[event.Sender.Uin].Send(content) - if err == nil { - client.SendGroupMessage(event.GroupCode, &message.SendingMessage{ - Elements: []message.IMessageElement{message.NewAt(event.Sender.Uin), message.NewText(res.Message.Content.Parts[0])}, - }) - break - } - if i == config.Instance.MsgRetry-1 { - client.SendGroupMessage(event.GroupCode, &message.SendingMessage{ - Elements: []message.IMessageElement{message.NewAt(event.Sender.Uin), message.NewText(err.Error())}, - }) - } + res, err := config.Chats[event.Sender.Uin].Send(content) + if err != nil { + client.SendGroupMessage(event.GroupCode, &message.SendingMessage{ + Elements: []message.IMessageElement{message.NewAt(event.Sender.Uin), message.NewText(err.Error())}, + }) + } else { + client.SendGroupMessage(event.GroupCode, &message.SendingMessage{ + Elements: []message.IMessageElement{message.NewAt(event.Sender.Uin), message.NewText(res.Message.Content.Parts[0])}, + }) } } } diff --git a/event/privatemessage.go b/event/privatemessage.go index 3e2a36b..23530fa 100644 --- a/event/privatemessage.go +++ b/event/privatemessage.go @@ -17,20 +17,16 @@ func PrivateMessage(client *client.QQClient, event *message.PrivateMessage) { return } if _, ok := config.Chats[event.Sender.Uin]; !ok { - config.Chats[event.Sender.Uin] = chatgpt.NewChat(config.Session) + config.Chats[event.Sender.Uin] = chatgpt.NewChat(config.Browser, config.Session) } - for i := 0; i < config.Instance.MsgRetry; i++ { - res, err := config.Chats[event.Sender.Uin].Send(content) - if err == nil { - client.SendPrivateMessage(event.Sender.Uin, &message.SendingMessage{ - Elements: []message.IMessageElement{message.NewText(res.Message.Content.Parts[0])}, - }) - break - } - if i == config.Instance.MsgRetry-1 { - client.SendPrivateMessage(event.Sender.Uin, &message.SendingMessage{ - Elements: []message.IMessageElement{message.NewText(err.Error())}, - }) - } + res, err := config.Chats[event.Sender.Uin].Send(content) + if err != nil { + client.SendPrivateMessage(event.Sender.Uin, &message.SendingMessage{ + Elements: []message.IMessageElement{message.NewText(err.Error())}, + }) + } else { + client.SendPrivateMessage(event.Sender.Uin, &message.SendingMessage{ + Elements: []message.IMessageElement{message.NewText(res.Message.Content.Parts[0])}, + }) } } diff --git a/go.mod b/go.mod index 3782203..e05f7a7 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f github.com/Mrs4s/MiraiGo v0.0.0-20221206193101-cf66e2864798 github.com/tuotoo/qrcode v0.0.0-20220425170535-52ccc2bebf5d - github.com/yxw21/chatgpt v0.6.0 + github.com/yxw21/chatgpt v0.7.0 ) require ( diff --git a/go.sum b/go.sum index 44e5ac6..4fec18f 100644 --- a/go.sum +++ b/go.sum @@ -103,8 +103,8 @@ github.com/ulikunitz/xz v0.5.9 h1:RsKRIA2MO8x56wkkcd3LbtcE/uMszhb6DpRf+3uwa3I= github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo= github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos= -github.com/yxw21/chatgpt v0.6.0 h1:lUPHh7fgH46UIw2G2oaFg7I/GFrnuWHCdXsXrVMG0dA= -github.com/yxw21/chatgpt v0.6.0/go.mod h1:40HZqYFtcZoUNXsG0LKCgzea+KHdW1XJ7MnVvhFm8z4= +github.com/yxw21/chatgpt v0.7.0 h1:7NVjO6+pyXof340nrFR1eXd8KcbDP/PRBXuzieKxMEA= +github.com/yxw21/chatgpt v0.7.0/go.mod h1:40HZqYFtcZoUNXsG0LKCgzea+KHdW1XJ7MnVvhFm8z4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/exp v0.0.0-20221205204356-47842c84f3db h1:D/cFflL63o2KSLJIwjlcIt8PR064j/xsmdEJL/YvY/o= golang.org/x/exp v0.0.0-20221205204356-47842c84f3db/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= diff --git a/qqchatgpt.go b/qqchatgpt.go index bbe3d44..5803841 100644 --- a/qqchatgpt.go +++ b/qqchatgpt.go @@ -3,6 +3,7 @@ package main import ( "context" "github.com/Mrs4s/MiraiGo/client" + "github.com/yxw21/chatgpt" "log" "qqgpt/config" "qqgpt/event" @@ -14,6 +15,12 @@ func main() { qq *client.QQClient err error ) + browser, closeBrowser, err := chatgpt.NewBrowser("") + if err != nil { + log.Fatal(err) + } + defer closeBrowser() + config.Browser = browser ctx, cancel := context.WithCancel(context.Background()) defer cancel() if err = helpers.AutoLoadDevice(); err != nil {