66 "fmt"
77 "start-feishubot/initialization"
88 "start-feishubot/services"
9+ "start-feishubot/services/openai"
910 "strings"
1011
1112 larkcard "github.com/larksuite/oapi-sdk-go/v3/card"
@@ -26,7 +27,7 @@ func chain(data *ActionInfo, actions ...Action) bool {
2627type MessageHandler struct {
2728 sessionCache services.SessionServiceCacheInterface
2829 msgCache services.MsgCacheInterface
29- gpt * services .ChatGPT
30+ gpt * openai .ChatGPT
3031 config initialization.Config
3132}
3233
@@ -36,6 +37,7 @@ func (m MessageHandler) cardHandler(_ context.Context,
3637 actionValue := cardAction .Action .Value
3738 actionValueJson , _ := json .Marshal (actionValue )
3839 json .Unmarshal (actionValueJson , & cardMsg )
40+ //fmt.Println("cardMsg: ", cardMsg)
3941 if cardMsg .Kind == ClearCardKind {
4042 newCard , err , done := CommonProcessClearCache (cardMsg , m .sessionCache )
4143 if done {
@@ -44,15 +46,28 @@ func (m MessageHandler) cardHandler(_ context.Context,
4446 return nil , nil
4547 }
4648 if cardMsg .Kind == PicResolutionKind {
47- CommonProcessPicResolution (cardMsg , cardAction , m .sessionCache )
49+ //todo: 暂时不允许 以图搜图 模式下的 再来一张
50+ //CommonProcessPicResolution(cardMsg, cardAction, m.sessionCache)
4851 return nil , nil
4952 }
50- if cardMsg .Kind == PicMoreKind {
53+ if cardMsg .Kind == PicTextMoreKind {
5154 go func () {
5255 m .CommonProcessPicMore (cardMsg )
5356 }()
5457 }
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
5569
70+ }
5671 return nil , nil
5772
5873}
@@ -63,7 +78,7 @@ func (m MessageHandler) CommonProcessPicMore(msg CardMsg) {
6378 //fmt.Println("msg: ", msg)
6479 question := msg .Value .(string )
6580 bs64 , _ := m .gpt .GenerateOneImage (question , resolution )
66- replayImageByBase64 (context .Background (), bs64 , & msg .MsgId ,
81+ replayImageCardByBase64 (context .Background (), bs64 , & msg .MsgId ,
6782 & msg .SessionId , question )
6883}
6984
@@ -100,18 +115,60 @@ func CommonProcessClearCache(cardMsg CardMsg, session services.SessionServiceCac
100115 return nil , nil , false
101116}
102117
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+
103159func (m MessageHandler ) msgReceivedHandler (ctx context.Context , event * larkim.P2MessageReceiveV1 ) error {
104160 handlerType := judgeChatType (event )
105161 if handlerType == "otherChat" {
106162 fmt .Println ("unknown chat type" )
107163 return nil
108164 }
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 )
112170 return nil
113171 }
114- //fmt.Println(larkcore.Prettify(event.Event.Message))
115172
116173 content := event .Event .Message .Content
117174 msgId := event .Event .Message .MessageId
@@ -130,6 +187,7 @@ func (m MessageHandler) msgReceivedHandler(ctx context.Context, event *larkim.P2
130187 chatId : chatId ,
131188 qParsed : strings .Trim (parseContent (* content ), " " ),
132189 fileKey : parseFileKey (* content ),
190+ imageKey : parseImageKey (* content ),
133191 sessionId : sessionId ,
134192 mention : mention ,
135193 }
@@ -142,11 +200,11 @@ func (m MessageHandler) msgReceivedHandler(ctx context.Context, event *larkim.P2
142200 & ProcessedUniqueAction {}, //避免重复处理
143201 & ProcessMentionAction {}, //判断机器人是否应该被调用
144202 & AudioAction {}, //语音处理
203+ & PicAction {}, //图片处理
145204 & EmptyAction {}, //空消息处理
146205 & ClearAction {}, //清除消息处理
147206 & HelpAction {}, //帮助处理
148207 & RolePlayAction {}, //角色扮演处理
149- & PicAction {}, //图片处理
150208 & MessageAction {}, //消息处理
151209
152210 }
@@ -156,7 +214,7 @@ func (m MessageHandler) msgReceivedHandler(ctx context.Context, event *larkim.P2
156214
157215var _ MessageHandlerInterface = (* MessageHandler )(nil )
158216
159- func NewMessageHandler (gpt * services .ChatGPT ,
217+ func NewMessageHandler (gpt * openai .ChatGPT ,
160218 config initialization.Config ) MessageHandlerInterface {
161219 return & MessageHandler {
162220 sessionCache : services .GetSessionCache (),
0 commit comments