forked from eryajf/chatgpt-dingtalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (72 loc) · 2.47 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
package main
import (
"fmt"
"path/filepath"
"strings"
"github.com/eryajf/chatgpt-dingtalk/pkg/process"
"github.com/eryajf/chatgpt-dingtalk/public"
"github.com/eryajf/chatgpt-dingtalk/public/logger"
"github.com/xgfone/ship/v5"
)
func init() {
public.InitSvc()
}
func main() {
Start()
}
var Welcome string = `Commands:
=================================
🙋 单聊 👉 单独聊天
📣 串聊 👉 带上下文聊天
🔃 重置 👉 重置带上下文聊天
💵 余额 👉 查询剩余额度
🚀 帮助 👉 显示帮助信息
🌈 模板 👉 内置的prompt
🎨 图片 👉 根据prompt生成图片
=================================
🚜 例:@我发送 空 或 帮助 将返回此帮助信息
💪 Power By https://github.com/eryajf/chatgpt-dingtalk
`
func Start() {
app := ship.Default()
app.Route("/").POST(func(c *ship.Context) error {
var msgObj public.ReceiveMsg
err := c.Bind(&msgObj)
if err != nil {
return ship.ErrBadRequest.New(fmt.Errorf("bind to receivemsg failed : %v", err))
}
if msgObj.Text.Content == "" || msgObj.ChatbotUserID == "" {
logger.Warning("从钉钉回调过来的内容为空,根据过往的经验,或许重新创建一下机器人,能解决这个问题")
return ship.ErrBadRequest.New(fmt.Errorf("从钉钉回调过来的内容为空,根据过往的经验,或许重新创建一下机器人,能解决这个问题"))
}
// 打印钉钉回调过来的请求明细
logger.Info(fmt.Sprintf("dingtalk callback parameters: %#v", msgObj))
// TODO: 校验请求
if len(msgObj.Text.Content) == 1 || strings.TrimSpace(msgObj.Text.Content) == "帮助" {
// 欢迎信息
_, err := msgObj.ReplyToDingtalk(string(public.TEXT), Welcome, msgObj.SenderStaffId)
if err != nil {
logger.Warning(fmt.Errorf("send message error: %v", err))
return ship.ErrBadRequest.New(fmt.Errorf("send message error: %v", err))
}
} else {
// 除去帮助之外的逻辑分流在这里处理
switch {
case strings.HasPrefix(strings.TrimSpace(msgObj.Text.Content), "#图片"):
return process.ImageGenerate(&msgObj)
default:
msgObj.Text.Content = process.GeneratePrompt(strings.TrimSpace(msgObj.Text.Content))
return process.ProcessRequest(&msgObj)
}
}
return nil
})
// 解析生成后的图片
app.Route("/images/:filename").GET(func(c *ship.Context) error {
filename := c.Param("filename")
root := "./images/"
return c.File(filepath.Join(root, filename))
})
// 启动服务器
ship.StartServer(":8090", app)
}