forked from eryajf/chatgpt-dingtalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
230 lines (216 loc) · 7.45 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/eryajf/chatgpt-dingtalk/public"
"github.com/eryajf/chatgpt-dingtalk/public/logger"
"github.com/solywsh/chatgpt"
)
func init() {
public.InitSvc()
}
func main() {
Start()
}
var Welcome string = `Commands:
=================================
🙋 单聊 👉 单独聊天
📣 串聊 👉 带上下文聊天
🔃 重置 👉 重置带上下文聊天
💵 余额 👉 查询剩余额度
🚀 帮助 👉 显示帮助信息
=================================
🚜 例:@我发送 空 或 帮助 将返回此帮助信息
💪 Power By https://github.com/eryajf/chatgpt-dingtalk
`
// 💵 余额 👉 查看接口可调用额度
func Start() {
// 定义一个处理器函数
handler := func(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
logger.Warning(fmt.Sprintf("read request body failed: %v\n", err.Error()))
return
}
if len(data) == 0 {
logger.Warning("回调参数为空,以至于无法正常解析,请检查原因")
return
}
var msgObj = new(public.ReceiveMsg)
err = json.Unmarshal(data, &msgObj)
if err != nil {
logger.Warning(fmt.Errorf("unmarshal request body failed: %v", err))
}
if msgObj.Text.Content == "" || msgObj.ChatbotUserID == "" {
logger.Warning("从钉钉回调过来的内容为空,根据过往的经验,或许重新创建一下机器人,能解决这个问题")
return
}
// TODO: 校验请求
if len(msgObj.Text.Content) == 1 || strings.TrimSpace(msgObj.Text.Content) == "帮助" {
// 欢迎信息
_, err := msgObj.ReplyText(Welcome, msgObj.SenderStaffId)
if err != nil {
logger.Warning(fmt.Errorf("send message error: %v", err))
}
} else {
logger.Info(fmt.Sprintf("dingtalk callback parameters: %#v", msgObj))
err = ProcessRequest(*msgObj)
if err != nil {
logger.Warning(fmt.Errorf("process request failed: %v", err))
}
}
}
// 创建一个新的 HTTP 服务器
server := &http.Server{
Addr: ":8090",
Handler: http.HandlerFunc(handler),
}
// 启动服务器
logger.Info("Start Listen On ", server.Addr)
err := server.ListenAndServe()
if err != nil {
logger.Danger(err)
}
}
func ProcessRequest(rmsg public.ReceiveMsg) error {
content := strings.TrimSpace(rmsg.Text.Content)
switch content {
case "单聊":
public.UserService.SetUserMode(rmsg.SenderStaffId, content)
_, err := rmsg.ReplyText(fmt.Sprintf("=====现在进入与👉%s👈单聊的模式 =====", rmsg.SenderNick), rmsg.SenderStaffId)
if err != nil {
logger.Warning(fmt.Errorf("send message error: %v", err))
}
case "串聊":
public.UserService.SetUserMode(rmsg.SenderStaffId, content)
_, err := rmsg.ReplyText(fmt.Sprintf("=====现在进入与👉%s👈串聊的模式 =====", rmsg.SenderNick), rmsg.SenderStaffId)
if err != nil {
logger.Warning(fmt.Errorf("send message error: %v", err))
}
case "重置":
public.UserService.ClearUserMode(rmsg.SenderStaffId)
public.UserService.ClearUserSessionContext(rmsg.SenderStaffId)
_, err := rmsg.ReplyText(fmt.Sprintf("=====已重置与👉%s👈的对话模式,可以开始新的对话=====", rmsg.SenderNick), rmsg.SenderStaffId)
if err != nil {
logger.Warning(fmt.Errorf("send message error: %v", err))
}
case "余额":
cacheMsg := public.UserService.GetUserMode("system_balance")
if cacheMsg == "" {
rst, err := public.GetBalance()
if err != nil {
logger.Warning(fmt.Errorf("get balance error: %v", err))
return err
}
t1 := time.Unix(int64(rst.Grants.Data[0].EffectiveAt), 0)
t2 := time.Unix(int64(rst.Grants.Data[0].ExpiresAt), 0)
cacheMsg = fmt.Sprintf("💵 已用: 💲%v\n💵 剩余: 💲%v\n⏳ 有效时间: 从 %v 到 %v\n", fmt.Sprintf("%.2f", rst.TotalUsed), fmt.Sprintf("%.2f", rst.TotalAvailable), t1.Format("2006-01-02 15:04:05"), t2.Format("2006-01-02 15:04:05"))
}
_, err := rmsg.ReplyText(cacheMsg, rmsg.SenderStaffId)
if err != nil {
logger.Warning(fmt.Errorf("send message error: %v", err))
}
default:
if public.FirstCheck(rmsg) {
return Do("串聊", rmsg)
} else {
return Do("单聊", rmsg)
}
}
return nil
}
func Do(mode string, rmsg public.ReceiveMsg) error {
// 先把模式注入
public.UserService.SetUserMode(rmsg.SenderStaffId, mode)
switch mode {
case "单聊":
reply, err := SingleQa(rmsg.Text.Content, rmsg.SenderStaffId)
if err != nil {
logger.Info(fmt.Errorf("gpt request error: %v", err))
if strings.Contains(fmt.Sprintf("%v", err), "maximum text length exceeded") {
public.UserService.ClearUserSessionContext(rmsg.SenderStaffId)
_, err = rmsg.ReplyText(fmt.Sprintf("请求openai失败了,错误信息:%v,看起来是超过最大对话限制了,已自动重置您的对话", err), rmsg.SenderStaffId)
if err != nil {
logger.Warning(fmt.Errorf("send message error: %v", err))
return err
}
} else {
_, err = rmsg.ReplyText(fmt.Sprintf("请求openai失败了,错误信息:%v", err), rmsg.SenderStaffId)
if err != nil {
logger.Warning(fmt.Errorf("send message error: %v", err))
return err
}
}
}
if reply == "" {
logger.Warning(fmt.Errorf("get gpt result falied: %v", err))
return nil
} else {
reply = strings.TrimSpace(reply)
reply = strings.Trim(reply, "\n")
// 回复@我的用户
// fmt.Println("单聊结果是:", reply)
_, err = rmsg.ReplyText(reply, rmsg.SenderStaffId)
if err != nil {
logger.Warning(fmt.Errorf("send message error: %v", err))
return err
}
}
case "串聊":
cli, reply, err := ContextQa(rmsg.Text.Content, rmsg.SenderStaffId)
if err != nil {
logger.Info(fmt.Sprintf("gpt request error: %v", err))
if strings.Contains(fmt.Sprintf("%v", err), "maximum text length exceeded") {
public.UserService.ClearUserSessionContext(rmsg.SenderStaffId)
_, err = rmsg.ReplyText(fmt.Sprintf("请求openai失败了,错误信息:%v,看起来是超过最大对话限制了,已自动重置您的对话", err), rmsg.SenderStaffId)
if err != nil {
logger.Warning(fmt.Errorf("send message error: %v", err))
return err
}
} else {
_, err = rmsg.ReplyText(fmt.Sprintf("请求openai失败了,错误信息:%v", err), rmsg.SenderStaffId)
if err != nil {
logger.Warning(fmt.Errorf("send message error: %v", err))
return err
}
}
}
if reply == "" {
logger.Warning(fmt.Errorf("get gpt result falied: %v", err))
return nil
} else {
reply = strings.TrimSpace(reply)
reply = strings.Trim(reply, "\n")
// 回复@我的用户
_, err = rmsg.ReplyText(reply, rmsg.SenderStaffId)
if err != nil {
logger.Warning(fmt.Errorf("send message error: %v", err))
return err
}
_ = cli.ChatContext.SaveConversation(rmsg.SenderStaffId)
}
default:
}
return nil
}
func SingleQa(question, userId string) (answer string, err error) {
chat := chatgpt.New(public.Config.ApiKey, public.Config.HttpProxy, userId, public.Config.SessionTimeout)
defer chat.Close()
return chat.ChatWithContext(question)
}
func ContextQa(question, userId string) (chat *chatgpt.ChatGPT, answer string, err error) {
chat = chatgpt.New(public.Config.ApiKey, public.Config.HttpProxy, userId, public.Config.SessionTimeout)
if public.UserService.GetUserSessionContext(userId) != "" {
err = chat.ChatContext.LoadConversation(userId)
if err != nil {
fmt.Printf("load station failed: %v\n", err)
}
}
answer, err = chat.ChatWithContext(question)
return
}