Skip to content

Commit dff0a9a

Browse files
committed
Refactor: Update message reply functionality to use common message and enable republication
1 parent 16a3a42 commit dff0a9a

13 files changed

+238
-577
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ go.work
2727

2828
config.yaml
2929

30+
confighi
3031

3132

3233
/code/target/

code/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ require (
3131
github.com/google/pprof v0.0.0-20230309165930-d61513b1440d // indirect
3232
github.com/hashicorp/hcl v1.0.0 // indirect
3333
github.com/json-iterator/go v1.1.12 // indirect
34+
github.com/larksuite/oapi-sdk-go v1.1.48 // indirect
3435
github.com/leodido/go-urn v1.2.1 // indirect
3536
github.com/magiconair/properties v1.8.7 // indirect
3637
github.com/mattn/go-isatty v0.0.17 // indirect

code/go.sum

Lines changed: 41 additions & 0 deletions
Large diffs are not rendered by default.

code/handlers/card_ai_mode_action.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import (
99
larkcard "github.com/larksuite/oapi-sdk-go/v3/card"
1010
)
1111

12+
//这个文件是一个处理飞书卡片消息的处理程序。它包含一个名为NewAIModeCardHandler的函数,
13+
//该函数返回一个CardHandlerFunc,用于处理选择AI模式的卡片操作。如果卡片消息的类型是AIModeChooseKind,
14+
//则会调用CommonProcessAIMode函数进行处理。CommonProcessAIMode函数会将选择的AI模式存储在会话缓存中,
15+
//并回复消息告知用户已选择的AI模式。这个文件还导入了一些服务和包,包括openai和larkcard。
16+
1217
// AIModeChooseKind is the kind of card action for choosing AI mode
1318
func NewAIModeCardHandler(cardMsg CardMsg,
1419
m MessageHandler) CardHandlerFunc {

code/handlers/card_common_action.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
larkcard "github.com/larksuite/oapi-sdk-go/v3/card"
99
)
1010

11+
// 这个文件的作用是实现了一个处理卡片消息的处理器。
12+
1113
type CardHandlerMeta func(cardMsg CardMsg, m MessageHandler) CardHandlerFunc
1214

1315
type CardHandlerFunc func(ctx context.Context, cardAction *larkcard.CardAction) (
@@ -16,6 +18,9 @@ type CardHandlerFunc func(ctx context.Context, cardAction *larkcard.CardAction)
1618
var ErrNextHandler = fmt.Errorf("next handler")
1719

1820
func NewCardHandler(m MessageHandler) CardHandlerFunc {
21+
// 添加一个新的文本处理器
22+
textHandler := NewTextHandler()
23+
1924
handlers := []CardHandlerMeta{
2025
NewClearCardHandler,
2126
NewPicResolutionHandler,
@@ -27,6 +32,11 @@ func NewCardHandler(m MessageHandler) CardHandlerFunc {
2732
}
2833

2934
return func(ctx context.Context, cardAction *larkcard.CardAction) (interface{}, error) {
35+
// 首先尝试使用文本处理器处理消息
36+
if msg, err := textHandler(ctx, cardAction); err == nil {
37+
return msg, nil
38+
}
39+
3040
var cardMsg CardMsg
3141
actionValue := cardAction.Action.Value
3242
actionValueJson, _ := json.Marshal(actionValue)

code/handlers/common.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ import (
88
"strings"
99
)
1010

11+
//包含了一些处理消息的函数,这些函数用于解析从飞书API接收到的消息内容。
12+
13+
// msgFilter函数用于过滤掉消息中的@提醒
1114
// func sendCard
1215
func msgFilter(msg string) string {
1316
//replace @到下一个非空的字段 为 ''
1417
regex := regexp.MustCompile(`@[^ ]*`)
1518
return regex.ReplaceAllString(msg, "")
1619

1720
}
21+
22+
// parseContent函数用于解析消息内容中的文本部分
1823
func parseContent(content string) string {
1924
//"{\"text\":\"@_user_1 hahaha\"}",
2025
//only get text content hahaha
@@ -30,6 +35,7 @@ func parseContent(content string) string {
3035
return msgFilter(text)
3136
}
3237

38+
// processMessage函数用于处理消息中的换行符和引号
3339
func processMessage(msg interface{}) (string, error) {
3440
msg = strings.TrimSpace(msg.(string))
3541
msgB, err := json.Marshal(msg)
@@ -45,6 +51,8 @@ func processMessage(msg interface{}) (string, error) {
4551
return msgStr, nil
4652
}
4753

54+
//processNewLine和processQuote函数分别用于处理换行符和引号
55+
4856
func processNewLine(msg string) string {
4957
return strings.Replace(msg, "\\n", `
5058
`, -1)
@@ -54,6 +62,7 @@ func processQuote(msg string) string {
5462
return strings.Replace(msg, "\\\"", "\"", -1)
5563
}
5664

65+
// processUnicode函数用于将Unicode编码转换为对应的字符
5766
// 将字符中 \u003c 替换为 < 等等
5867
func processUnicode(msg string) string {
5968
regex := regexp.MustCompile(`\\u[0-9a-fA-F]{4}`)
@@ -65,13 +74,15 @@ func processUnicode(msg string) string {
6574
})
6675
}
6776

77+
// cleanTextBlock函数用于将消息中的特殊字符进行处理
6878
func cleanTextBlock(msg string) string {
6979
msg = processNewLine(msg)
7080
msg = processUnicode(msg)
7181
msg = processQuote(msg)
7282
return msg
7383
}
7484

85+
// parseFileKey和parseImageKey函数用于解析消息中的文件和图片的key值
7586
func parseFileKey(content string) string {
7687
var contentMap map[string]interface{}
7788
err := json.Unmarshal([]byte(content), &contentMap)
@@ -99,3 +110,73 @@ func parseImageKey(content string) string {
99110
imageKey := contentMap["image_key"].(string)
100111
return imageKey
101112
}
113+
114+
/*
115+
根据你的描述,我了解到你想要修改项目中的代码,使得在收到用户文本消息后,第一个返回的消息是普通的消息回复类型,而不是飞书的卡片消息。根据你提供的代码,我发现在 NewCardHandler 函数中,我们定义了一个处理卡片消息的处理器,这个处理器会依次调用一系列的处理函数,直到找到一个能够处理当前卡片消息的处理函数为止。因此,我们需要修改 NewCardHandler 函数,使其返回一个处理普通消息的处理器,而不是处理卡片消息的处理器。
116+
117+
具体来说,我们需要将 NewCardHandler 函数中的 handlers 数组中的处理卡片消息的处理函数全部替换成处理普通消息的处理函数。这里需要根据你的具体需求来确定使用哪些处理函数。如果你想要直接回复用户发送的文本消息,可以使用 m.SendText(ctx, open_id, text) 函数,其中 open_id 是用户的唯一标识符,text 是要回复的文本消息。如果你想要回复富文本消息,可以使用 m.SendPost(ctx, open_id, post) 函数,其中 post 是要回复的富文本消息。
118+
119+
下面是修改后的代码示例:
120+
121+
/Users/shiym/Documents/Feishu-OpenAI/code/handlers/card_common_action.go
122+
package handlers
123+
124+
import (
125+
"context"
126+
"encoding/json"
127+
"fmt"
128+
)
129+
// 这个文件的作用是实现了一个处理普通消息的处理器。
130+
131+
type MessageHandler interface {
132+
SendText(ctx context.Context, open_id, text string) error
133+
SendPost(ctx context.Context, open_id string, post interface{}) error
134+
}
135+
136+
type MessageHandlerFunc func(ctx context.Context, open_id string, text string) error
137+
138+
func NewMessageHandler(m MessageHandler) MessageHandlerFunc {
139+
return func(ctx context.Context, open_id string, text string) error {
140+
return m.SendText(ctx, open_id, text)
141+
}
142+
}
143+
func NewPostHandler(m MessageHandler) MessageHandlerFunc {
144+
return func(ctx context.Context, open_id string, post interface{}) error {
145+
return m.SendPost(ctx, open_id, post)
146+
}
147+
}
148+
149+
var ErrNextHandler = fmt.Errorf("next handler")
150+
151+
func NewCardHandler(m MessageHandler) MessageHandlerFunc {
152+
handlers := []CardHandlerMeta{
153+
NewClearCardHandler,
154+
NewPicResolutionHandler,
155+
NewPicTextMoreHandler,
156+
NewPicModeChangeHandler,
157+
NewRoleTagCardHandler,
158+
NewRoleCardHandler,
159+
NewAIModeCardHandler,
160+
}
161+
162+
return func(ctx context.Context, cardAction *larkcard.CardAction) error {
163+
var cardMsg CardMsg
164+
actionValue := cardAction.Action.Value
165+
actionValueJson, _ := json.Marshal(actionValue)
166+
if err := json.Unmarshal(actionValueJson, &cardMsg); err != nil {
167+
return err
168+
}
169+
//pp.Println(cardMsg)
170+
for _, handler := range handlers {
171+
h := handler(cardMsg, m)
172+
err := h(ctx, cardAction)
173+
if err == ErrNextHandler {
174+
continue
175+
}
176+
return err
177+
}
178+
return nil
179+
}
180+
}
181+
182+
*/

code/handlers/event_common_action.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import (
1111
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
1212
)
1313

14+
//这个文件是一个处理消息的处理程序,它定义了一些用于处理消息的操作
15+
16+
//定义了一个MsgInfo结构体,
17+
//它包含了处理程序的类型、消息类型、消息ID、聊天ID、解析后的查询字符串、文件键、图片键、会话ID和提到的用户列表。
18+
1419
type MsgInfo struct {
1520
handlerType HandlerType
1621
msgType string
@@ -28,6 +33,11 @@ type ActionInfo struct {
2833
info *MsgInfo
2934
}
3035

36+
// 定义了一个Action接口和一些实现了该接口的操作,
37+
// 例如ProcessedUniqueAction、ProcessMentionAction、EmptyAction、ClearAction、
38+
// RolePlayAction、HelpAction、BalanceAction、RoleListAction和AIModeAction。
39+
// 这些操作用于执行不同的任务,例如检查消息的唯一性、判断是否应该处理消息、
40+
// 处理空消息、清除消息、角色扮演、提供帮助、查询余额、提供角色列表和提供AI模式列表。
3141
type Action interface {
3242
Execute(a *ActionInfo) bool
3343
}
@@ -66,7 +76,7 @@ type EmptyAction struct { /*空消息*/
6676

6777
func (*EmptyAction) Execute(a *ActionInfo) bool {
6878
if len(a.info.qParsed) == 0 {
69-
sendMsg(*a.ctx, "🤖️:你想知道什么呢~", a.info.chatId)
79+
sendMsg(*a.ctx, "🤖️Bot:为什么发个空消息呢~", a.info.chatId)
7080
fmt.Println("msgId", *a.info.msgId,
7181
"message.text is empty")
7282
return false

code/handlers/event_msg_action.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ func (*MessageAction) Execute(a *ActionInfo) bool {
2727
//if new topic
2828
if len(msg) == 2 {
2929
//fmt.Println("new topic", msg[1].Content)
30-
sendNewTopicCard(*a.ctx, a.info.sessionId, a.info.msgId,
31-
completions.Content)
32-
return false
30+
31+
32+
replyMsg(*a.ctx,completions.Content, a.info.msgId)
33+
sendNewTopicCard(ctx context.Context, msgId *string, content string)
34+
sendNewTopicCard(*a.ctx, a.info.msgId,completions.Content)
35+
withMainText(content)
36+
3337
}
3438
err = replyMsg(*a.ctx, completions.Content, a.info.msgId)
3539
if err != nil {

code/handlers/handler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
1414
)
1515

16+
//这个文件是一个消息处理器,用于处理来自飞书IM的P2P消息。它包含一个MessageHandler结构体,其中包含了处理消息的各种方法。
17+
1618
// 责任链
1719
func chain(data *ActionInfo, actions ...Action) bool {
1820
for _, v := range actions {

0 commit comments

Comments
 (0)