Skip to content

Commit

Permalink
Refactor: Update message reply functionality to use common message an…
Browse files Browse the repository at this point in the history
…d enable republication
  • Loading branch information
SKDDJ committed Nov 26, 2023
1 parent 16a3a42 commit dff0a9a
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 577 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ go.work

config.yaml

confighi


/code/target/
Expand Down
1 change: 1 addition & 0 deletions code/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/google/pprof v0.0.0-20230309165930-d61513b1440d // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/larksuite/oapi-sdk-go v1.1.48 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
Expand Down
41 changes: 41 additions & 0 deletions code/go.sum

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions code/handlers/card_ai_mode_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
larkcard "github.com/larksuite/oapi-sdk-go/v3/card"
)

//这个文件是一个处理飞书卡片消息的处理程序。它包含一个名为NewAIModeCardHandler的函数,
//该函数返回一个CardHandlerFunc,用于处理选择AI模式的卡片操作。如果卡片消息的类型是AIModeChooseKind,
//则会调用CommonProcessAIMode函数进行处理。CommonProcessAIMode函数会将选择的AI模式存储在会话缓存中,
//并回复消息告知用户已选择的AI模式。这个文件还导入了一些服务和包,包括openai和larkcard。

// AIModeChooseKind is the kind of card action for choosing AI mode
func NewAIModeCardHandler(cardMsg CardMsg,
m MessageHandler) CardHandlerFunc {
Expand Down
10 changes: 10 additions & 0 deletions code/handlers/card_common_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
larkcard "github.com/larksuite/oapi-sdk-go/v3/card"
)

// 这个文件的作用是实现了一个处理卡片消息的处理器。

type CardHandlerMeta func(cardMsg CardMsg, m MessageHandler) CardHandlerFunc

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

func NewCardHandler(m MessageHandler) CardHandlerFunc {
// 添加一个新的文本处理器
textHandler := NewTextHandler()

handlers := []CardHandlerMeta{
NewClearCardHandler,
NewPicResolutionHandler,
Expand All @@ -27,6 +32,11 @@ func NewCardHandler(m MessageHandler) CardHandlerFunc {
}

return func(ctx context.Context, cardAction *larkcard.CardAction) (interface{}, error) {
// 首先尝试使用文本处理器处理消息
if msg, err := textHandler(ctx, cardAction); err == nil {
return msg, nil
}

var cardMsg CardMsg
actionValue := cardAction.Action.Value
actionValueJson, _ := json.Marshal(actionValue)
Expand Down
81 changes: 81 additions & 0 deletions code/handlers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import (
"strings"
)

//包含了一些处理消息的函数,这些函数用于解析从飞书API接收到的消息内容。

// msgFilter函数用于过滤掉消息中的@提醒
// func sendCard
func msgFilter(msg string) string {
//replace @到下一个非空的字段 为 ''
regex := regexp.MustCompile(`@[^ ]*`)
return regex.ReplaceAllString(msg, "")

}

// parseContent函数用于解析消息内容中的文本部分
func parseContent(content string) string {
//"{\"text\":\"@_user_1 hahaha\"}",
//only get text content hahaha
Expand All @@ -30,6 +35,7 @@ func parseContent(content string) string {
return msgFilter(text)
}

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

//processNewLine和processQuote函数分别用于处理换行符和引号

func processNewLine(msg string) string {
return strings.Replace(msg, "\\n", `
`, -1)
Expand All @@ -54,6 +62,7 @@ func processQuote(msg string) string {
return strings.Replace(msg, "\\\"", "\"", -1)
}

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

// cleanTextBlock函数用于将消息中的特殊字符进行处理
func cleanTextBlock(msg string) string {
msg = processNewLine(msg)
msg = processUnicode(msg)
msg = processQuote(msg)
return msg
}

// parseFileKey和parseImageKey函数用于解析消息中的文件和图片的key值
func parseFileKey(content string) string {
var contentMap map[string]interface{}
err := json.Unmarshal([]byte(content), &contentMap)
Expand Down Expand Up @@ -99,3 +110,73 @@ func parseImageKey(content string) string {
imageKey := contentMap["image_key"].(string)
return imageKey
}

/*
根据你的描述,我了解到你想要修改项目中的代码,使得在收到用户文本消息后,第一个返回的消息是普通的消息回复类型,而不是飞书的卡片消息。根据你提供的代码,我发现在 NewCardHandler 函数中,我们定义了一个处理卡片消息的处理器,这个处理器会依次调用一系列的处理函数,直到找到一个能够处理当前卡片消息的处理函数为止。因此,我们需要修改 NewCardHandler 函数,使其返回一个处理普通消息的处理器,而不是处理卡片消息的处理器。
具体来说,我们需要将 NewCardHandler 函数中的 handlers 数组中的处理卡片消息的处理函数全部替换成处理普通消息的处理函数。这里需要根据你的具体需求来确定使用哪些处理函数。如果你想要直接回复用户发送的文本消息,可以使用 m.SendText(ctx, open_id, text) 函数,其中 open_id 是用户的唯一标识符,text 是要回复的文本消息。如果你想要回复富文本消息,可以使用 m.SendPost(ctx, open_id, post) 函数,其中 post 是要回复的富文本消息。
下面是修改后的代码示例:
/Users/shiym/Documents/Feishu-OpenAI/code/handlers/card_common_action.go
package handlers
import (
"context"
"encoding/json"
"fmt"
)
// 这个文件的作用是实现了一个处理普通消息的处理器。
type MessageHandler interface {
SendText(ctx context.Context, open_id, text string) error
SendPost(ctx context.Context, open_id string, post interface{}) error
}
type MessageHandlerFunc func(ctx context.Context, open_id string, text string) error
func NewMessageHandler(m MessageHandler) MessageHandlerFunc {
return func(ctx context.Context, open_id string, text string) error {
return m.SendText(ctx, open_id, text)
}
}
func NewPostHandler(m MessageHandler) MessageHandlerFunc {
return func(ctx context.Context, open_id string, post interface{}) error {
return m.SendPost(ctx, open_id, post)
}
}
var ErrNextHandler = fmt.Errorf("next handler")
func NewCardHandler(m MessageHandler) MessageHandlerFunc {
handlers := []CardHandlerMeta{
NewClearCardHandler,
NewPicResolutionHandler,
NewPicTextMoreHandler,
NewPicModeChangeHandler,
NewRoleTagCardHandler,
NewRoleCardHandler,
NewAIModeCardHandler,
}
return func(ctx context.Context, cardAction *larkcard.CardAction) error {
var cardMsg CardMsg
actionValue := cardAction.Action.Value
actionValueJson, _ := json.Marshal(actionValue)
if err := json.Unmarshal(actionValueJson, &cardMsg); err != nil {
return err
}
//pp.Println(cardMsg)
for _, handler := range handlers {
h := handler(cardMsg, m)
err := h(ctx, cardAction)
if err == ErrNextHandler {
continue
}
return err
}
return nil
}
}
*/
12 changes: 11 additions & 1 deletion code/handlers/event_common_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
)

//这个文件是一个处理消息的处理程序,它定义了一些用于处理消息的操作

//定义了一个MsgInfo结构体,
//它包含了处理程序的类型、消息类型、消息ID、聊天ID、解析后的查询字符串、文件键、图片键、会话ID和提到的用户列表。

type MsgInfo struct {
handlerType HandlerType
msgType string
Expand All @@ -28,6 +33,11 @@ type ActionInfo struct {
info *MsgInfo
}

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

func (*EmptyAction) Execute(a *ActionInfo) bool {
if len(a.info.qParsed) == 0 {
sendMsg(*a.ctx, "🤖️:你想知道什么呢~", a.info.chatId)
sendMsg(*a.ctx, "🤖️Bot:为什么发个空消息呢~", a.info.chatId)
fmt.Println("msgId", *a.info.msgId,
"message.text is empty")
return false
Expand Down
10 changes: 7 additions & 3 deletions code/handlers/event_msg_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ func (*MessageAction) Execute(a *ActionInfo) bool {
//if new topic
if len(msg) == 2 {
//fmt.Println("new topic", msg[1].Content)
sendNewTopicCard(*a.ctx, a.info.sessionId, a.info.msgId,
completions.Content)
return false


replyMsg(*a.ctx,completions.Content, a.info.msgId)
sendNewTopicCard(ctx context.Context, msgId *string, content string)
sendNewTopicCard(*a.ctx, a.info.msgId,completions.Content)
withMainText(content)

}
err = replyMsg(*a.ctx, completions.Content, a.info.msgId)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions code/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
)

//这个文件是一个消息处理器,用于处理来自飞书IM的P2P消息。它包含一个MessageHandler结构体,其中包含了处理消息的各种方法。

// 责任链
func chain(data *ActionInfo, actions ...Action) bool {
for _, v := range actions {
Expand Down
Loading

0 comments on commit dff0a9a

Please sign in to comment.