Skip to content

Commit

Permalink
refactor: clean code for part:judgeMsgType
Browse files Browse the repository at this point in the history
  • Loading branch information
Leizhenpeng committed Mar 12, 2023
1 parent a3988b4 commit 8b392f8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
22 changes: 18 additions & 4 deletions code/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
"start-feishubot/initialization"
"start-feishubot/services"
"start-feishubot/services/openai"
Expand Down Expand Up @@ -101,18 +102,31 @@ func CommonProcessClearCache(cardMsg CardMsg, session services.SessionServiceCac
return nil, nil, false
}

func judgeMsgType(event *larkim.P2MessageReceiveV1) (string, error) {
msgType := event.Event.Message.MessageType

switch *msgType {
case "text", "image", "audio":
return *msgType, nil
default:
return "", fmt.Errorf("unknown message type: %v", *msgType)
}

}

func (m MessageHandler) msgReceivedHandler(ctx context.Context, event *larkim.P2MessageReceiveV1) error {
handlerType := judgeChatType(event)
if handlerType == "otherChat" {
fmt.Println("unknown chat type")
return nil
}
msgType := judgeMsgType(event)
if msgType != "text" && msgType != "audio" {
fmt.Println("unknown msg type")
fmt.Println(larkcore.Prettify(event.Event.Message))

msgType, err := judgeMsgType(event)
if err != nil {
fmt.Printf("error getting message type: %v\n", err)
return nil
}
//fmt.Println(larkcore.Prettify(event.Event.Message))

content := event.Event.Message.Content
msgId := event.Event.Message.MessageId
Expand Down
15 changes: 0 additions & 15 deletions code/handlers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,3 @@ func judgeChatType(event *larkim.P2MessageReceiveV1) HandlerType {
}
return "otherChat"
}

func judgeMsgType(event *larkim.P2MessageReceiveV1) string {
msgType := event.Event.Message.MessageType
if *msgType == "text" {
return "text"
}
if *msgType == "image" {
return "image"
}
if *msgType == "audio" {
return "audio"
}

return ""
}

0 comments on commit 8b392f8

Please sign in to comment.