-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_wechat.go
68 lines (61 loc) · 1.67 KB
/
open_wechat.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
package main
import (
"fmt"
"github.com/silenceper/wechat/v2"
"github.com/silenceper/wechat/v2/cache"
"github.com/silenceper/wechat/v2/officialaccount/config"
"github.com/silenceper/wechat/v2/officialaccount/message"
"github.com/sirupsen/logrus"
"net/http"
)
type OpenWechatHandler struct {
}
func (h OpenWechatHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
wc := wechat.NewWechat()
memory := cache.NewMemory()
cfg := &config.Config{
AppID: GlobalConfig.OpenWechat.AppID,
AppSecret: GlobalConfig.OpenWechat.AppSecret,
Token: GlobalConfig.OpenWechat.Token,
EncodingAESKey: GlobalConfig.OpenWechat.EncodingAESKey,
Cache: memory,
}
officialAccount := wc.GetOfficialAccount(cfg)
// 传入request和responseWriter
server := officialAccount.GetServer(r, w)
// 设置接收消息的处理方法
server.SetMessageHandler(handleMessage)
// 处理消息接收以及回复
err := server.Serve()
if err != nil {
fmt.Println(err)
return
}
// 发送回复的消息
server.Send()
}
func handleMessage(msg message.MixMessage) *message.Reply {
data := SendMsgData{
MessageType: "private",
UserId: string(msg.FromUserName),
GroupId: "",
Message: make([]Message, 0, 5),
AutoEscape: false,
ReceivedMsg: msg.Content,
}
err := data.AddAIPrompts("private")
if err != nil {
logrus.Errorf("add AI prompts error: %s", err)
return nil
}
err = data.AIChat("private")
if err != nil {
logrus.Errorf("AI chat error: %s", err)
return nil
}
openWechatReply := message.Reply{
MsgType: message.MsgTypeText,
MsgData: message.NewText(fmt.Sprintf("%v", data.Message[0].Data["text"])),
}
return &openWechatReply
}