6
6
"fmt"
7
7
"start-feishubot/initialization"
8
8
"start-feishubot/services"
9
+ "start-feishubot/services/openai"
9
10
"strings"
10
11
11
12
larkcard "github.com/larksuite/oapi-sdk-go/v3/card"
@@ -26,7 +27,7 @@ func chain(data *ActionInfo, actions ...Action) bool {
26
27
type MessageHandler struct {
27
28
sessionCache services.SessionServiceCacheInterface
28
29
msgCache services.MsgCacheInterface
29
- gpt * services .ChatGPT
30
+ gpt * openai .ChatGPT
30
31
config initialization.Config
31
32
}
32
33
@@ -36,6 +37,7 @@ func (m MessageHandler) cardHandler(_ context.Context,
36
37
actionValue := cardAction .Action .Value
37
38
actionValueJson , _ := json .Marshal (actionValue )
38
39
json .Unmarshal (actionValueJson , & cardMsg )
40
+ //fmt.Println("cardMsg: ", cardMsg)
39
41
if cardMsg .Kind == ClearCardKind {
40
42
newCard , err , done := CommonProcessClearCache (cardMsg , m .sessionCache )
41
43
if done {
@@ -44,15 +46,28 @@ func (m MessageHandler) cardHandler(_ context.Context,
44
46
return nil , nil
45
47
}
46
48
if cardMsg .Kind == PicResolutionKind {
47
- CommonProcessPicResolution (cardMsg , cardAction , m .sessionCache )
49
+ //todo: 暂时不允许 以图搜图 模式下的 再来一张
50
+ //CommonProcessPicResolution(cardMsg, cardAction, m.sessionCache)
48
51
return nil , nil
49
52
}
50
- if cardMsg .Kind == PicMoreKind {
53
+ if cardMsg .Kind == PicTextMoreKind {
51
54
go func () {
52
55
m .CommonProcessPicMore (cardMsg )
53
56
}()
54
57
}
58
+ if cardMsg .Kind == PicVarMoreKind {
59
+ go func () {
60
+ m .CommonProcessPicMore (cardMsg )
61
+ }()
62
+ }
63
+ if cardMsg .Kind == PicModeChangeKind {
64
+ newCard , err , done := CommonProcessPicModeChange (cardMsg , m .sessionCache )
65
+ if done {
66
+ return newCard , err
67
+ }
68
+ return nil , nil
55
69
70
+ }
56
71
return nil , nil
57
72
58
73
}
@@ -63,7 +78,7 @@ func (m MessageHandler) CommonProcessPicMore(msg CardMsg) {
63
78
//fmt.Println("msg: ", msg)
64
79
question := msg .Value .(string )
65
80
bs64 , _ := m .gpt .GenerateOneImage (question , resolution )
66
- replayImageByBase64 (context .Background (), bs64 , & msg .MsgId ,
81
+ replayImageCardByBase64 (context .Background (), bs64 , & msg .MsgId ,
67
82
& msg .SessionId , question )
68
83
}
69
84
@@ -100,18 +115,60 @@ func CommonProcessClearCache(cardMsg CardMsg, session services.SessionServiceCac
100
115
return nil , nil , false
101
116
}
102
117
118
+ func CommonProcessPicModeChange (cardMsg CardMsg ,
119
+ session services.SessionServiceCacheInterface ) (
120
+ interface {}, error , bool ) {
121
+ if cardMsg .Value == "1" {
122
+
123
+ sessionId := cardMsg .SessionId
124
+ session .Clear (sessionId )
125
+ session .SetMode (sessionId ,
126
+ services .ModePicCreate )
127
+ session .SetPicResolution (sessionId ,
128
+ services .Resolution256 )
129
+
130
+ newCard , _ :=
131
+ newSendCard (
132
+ withHeader ("🖼️ 已进入图片创作模式" , larkcard .TemplateBlue ),
133
+ withPicResolutionBtn (& sessionId ),
134
+ withNote ("提醒:回复文本或图片,让AI生成相关的图片。" ))
135
+ return newCard , nil , true
136
+ }
137
+ if cardMsg .Value == "0" {
138
+ newCard , _ := newSendCard (
139
+ withHeader ("️🎒 机器人提醒" , larkcard .TemplateGreen ),
140
+ withMainMd ("依旧保留此话题的上下文信息" ),
141
+ withNote ("我们可以继续探讨这个话题,期待和您聊天。如果您有其他问题或者想要讨论的话题,请告诉我哦" ),
142
+ )
143
+ return newCard , nil , true
144
+ }
145
+ return nil , nil , false
146
+ }
147
+ func judgeMsgType (event * larkim.P2MessageReceiveV1 ) (string , error ) {
148
+ msgType := event .Event .Message .MessageType
149
+
150
+ switch * msgType {
151
+ case "text" , "image" , "audio" :
152
+ return * msgType , nil
153
+ default :
154
+ return "" , fmt .Errorf ("unknown message type: %v" , * msgType )
155
+ }
156
+
157
+ }
158
+
103
159
func (m MessageHandler ) msgReceivedHandler (ctx context.Context , event * larkim.P2MessageReceiveV1 ) error {
104
160
handlerType := judgeChatType (event )
105
161
if handlerType == "otherChat" {
106
162
fmt .Println ("unknown chat type" )
107
163
return nil
108
164
}
109
- msgType := judgeMsgType (event )
110
- if msgType != "text" && msgType != "audio" {
111
- fmt .Println ("unknown msg type" )
165
+ //fmt.Println(larkcore.Prettify(event.Event.Message))
166
+
167
+ msgType , err := judgeMsgType (event )
168
+ if err != nil {
169
+ fmt .Printf ("error getting message type: %v\n " , err )
112
170
return nil
113
171
}
114
- //fmt.Println(larkcore.Prettify(event.Event.Message))
115
172
116
173
content := event .Event .Message .Content
117
174
msgId := event .Event .Message .MessageId
@@ -130,6 +187,7 @@ func (m MessageHandler) msgReceivedHandler(ctx context.Context, event *larkim.P2
130
187
chatId : chatId ,
131
188
qParsed : strings .Trim (parseContent (* content ), " " ),
132
189
fileKey : parseFileKey (* content ),
190
+ imageKey : parseImageKey (* content ),
133
191
sessionId : sessionId ,
134
192
mention : mention ,
135
193
}
@@ -142,11 +200,11 @@ func (m MessageHandler) msgReceivedHandler(ctx context.Context, event *larkim.P2
142
200
& ProcessedUniqueAction {}, //避免重复处理
143
201
& ProcessMentionAction {}, //判断机器人是否应该被调用
144
202
& AudioAction {}, //语音处理
203
+ & PicAction {}, //图片处理
145
204
& EmptyAction {}, //空消息处理
146
205
& ClearAction {}, //清除消息处理
147
206
& HelpAction {}, //帮助处理
148
207
& RolePlayAction {}, //角色扮演处理
149
- & PicAction {}, //图片处理
150
208
& MessageAction {}, //消息处理
151
209
152
210
}
@@ -156,7 +214,7 @@ func (m MessageHandler) msgReceivedHandler(ctx context.Context, event *larkim.P2
156
214
157
215
var _ MessageHandlerInterface = (* MessageHandler )(nil )
158
216
159
- func NewMessageHandler (gpt * services .ChatGPT ,
217
+ func NewMessageHandler (gpt * openai .ChatGPT ,
160
218
config initialization.Config ) MessageHandlerInterface {
161
219
return & MessageHandler {
162
220
sessionCache : services .GetSessionCache (),
0 commit comments