Skip to content

Commit

Permalink
Merge pull request #83 from Leizhenpeng/feat_support_voice
Browse files Browse the repository at this point in the history
feat: support voice talk
  • Loading branch information
Leizhenpeng authored Mar 11, 2023
2 parents b4a3709 + 59b0ebd commit 612fcc8
Show file tree
Hide file tree
Showing 11 changed files with 388 additions and 33 deletions.
1 change: 1 addition & 0 deletions code/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/google/uuid v1.3.0
github.com/larksuite/oapi-sdk-gin v1.0.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pion/opus v0.0.0-20230123082803-1052c3e89e58
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.14.0
)
Expand Down
2 changes: 2 additions & 0 deletions code/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3v
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
github.com/pion/opus v0.0.0-20230123082803-1052c3e89e58 h1:wi5XffRvL9Ghx8nRAdZyAjmLV/ccnn2xJ4w6S6fELgA=
github.com/pion/opus v0.0.0-20230123082803-1052c3e89e58/go.mod h1:m8ODxkLrcNvLY6BPvOj7yLxK1wMQWA+2jqKcsrZ293U=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
Expand Down
17 changes: 17 additions & 0 deletions code/handlers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func parseContent(content string) string {
if err != nil {
fmt.Println(err)
}
if contentMap["text"] == nil {
return ""
}
text := contentMap["text"].(string)
return msgFilter(text)
}
Expand Down Expand Up @@ -67,3 +70,17 @@ func cleanTextBlock(msg string) string {
msg = processQuote(msg)
return msg
}

func parseFileKey(content string) string {
var contentMap map[string]interface{}
err := json.Unmarshal([]byte(content), &contentMap)
if err != nil {
fmt.Println(err)
return ""
}
if contentMap["file_key"] == nil {
return ""
}
fileKey := contentMap["file_key"].(string)
return fileKey
}
56 changes: 55 additions & 1 deletion code/handlers/event_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"context"
"fmt"
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
"os"
"start-feishubot/initialization"
"start-feishubot/services"
"start-feishubot/utils"
"start-feishubot/utils/audio"
)

type MsgInfo struct {
Expand All @@ -14,6 +17,7 @@ type MsgInfo struct {
msgId *string
chatId *string
qParsed string
fileKey string
sessionId *string
mention []*larkim.MentionEvent
}
Expand Down Expand Up @@ -169,7 +173,7 @@ func (*MessageAction) Execute(a *ActionInfo) bool {
a.handler.sessionCache.SetMsg(*a.info.sessionId, msg)
//if new topic
if len(msg) == 2 {
fmt.Println("new topic", msg[1].Content)
//fmt.Println("new topic", msg[1].Content)
sendNewTopicCard(*a.ctx, a.info.sessionId, a.info.msgId,
completions.Content)
return false
Expand All @@ -182,3 +186,53 @@ func (*MessageAction) Execute(a *ActionInfo) bool {
}
return true
}

type AudioAction struct { /*语音*/
}

func (*AudioAction) Execute(a *ActionInfo) bool {
// 只有私聊才解析语音,其他不解析
if a.info.handlerType != UserHandler {
return true
}

//判断是否是语音
if a.info.msgType == "audio" {
fileKey := a.info.fileKey
//fmt.Printf("fileKey: %s \n", fileKey)
msgId := a.info.msgId
//fmt.Println("msgId: ", *msgId)
req := larkim.NewGetMessageResourceReqBuilder().MessageId(
*msgId).FileKey(fileKey).Type("file").Build()
resp, err := initialization.GetLarkClient().Im.MessageResource.Get(context.Background(), req)
//fmt.Println(resp, err)
if err != nil {
fmt.Println(err)
return true
}
f := fmt.Sprintf("%s.ogg", fileKey)
resp.WriteFile(f)
defer os.Remove(f)

//fmt.Println("f: ", f)
output := fmt.Sprintf("%s.mp3", fileKey)
// 等待转换完成
audio.OggToWavByPath(f, output)
defer os.Remove(output)
//fmt.Println("output: ", output)

text, err := a.handler.gpt.AudioToText(output)
if err != nil {
fmt.Println(err)
sendMsg(*a.ctx, "🤖️:语音转换失败,请稍后再试~", a.info.msgId)
return false
}
//删除文件
//fmt.Println("text: ", text)
a.info.qParsed = text
return true
}

return true

}
5 changes: 4 additions & 1 deletion code/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ func (m MessageHandler) msgReceivedHandler(ctx context.Context, event *larkim.P2
return nil
}
msgType := judgeMsgType(event)
if msgType != "text" {
if msgType != "text" && msgType != "audio" {
fmt.Println("unknown msg type")
return nil
}
//fmt.Println(larkcore.Prettify(event.Event.Message))

content := event.Event.Message.Content
msgId := event.Event.Message.MessageId
Expand All @@ -128,6 +129,7 @@ func (m MessageHandler) msgReceivedHandler(ctx context.Context, event *larkim.P2
msgId: msgId,
chatId: chatId,
qParsed: strings.Trim(parseContent(*content), " "),
fileKey: parseFileKey(*content),

This comment has been minimized.

Copy link
@trevorwang

trevorwang Mar 12, 2023

Contributor

考虑增加个 MessageContentHandler更好处理多种类型的消息, 弄到一半了 没想到你这个做完了哈哈

func parseMessageContent(msgType string, content string) MessageContentHandler {
	switch msgType {
	case "text":
		return parseTextContent(msgType, content)
	case "audio":
		return parseAudioContent(msgType, content)
	default:
		return &UnkownMessageContent{
			MsgType: msgType,
		}
	}
}
sessionId: sessionId,
mention: mention,
}
Expand All @@ -139,6 +141,7 @@ func (m MessageHandler) msgReceivedHandler(ctx context.Context, event *larkim.P2
actions := []Action{
&ProcessedUniqueAction{}, //避免重复处理
&ProcessMentionAction{}, //判断机器人是否应该被调用
&AudioAction{}, //语音处理
&EmptyAction{}, //空消息处理
&ClearAction{}, //清除消息处理
&HelpAction{}, //帮助处理
Expand Down
9 changes: 7 additions & 2 deletions code/handlers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package handlers

import (
"context"
"fmt"
"start-feishubot/initialization"
"start-feishubot/services"

Expand Down Expand Up @@ -62,7 +61,6 @@ func judgeCardType(cardAction *larkcard.CardAction) HandlerType {

func judgeChatType(event *larkim.P2MessageReceiveV1) HandlerType {
chatType := event.Event.Message.ChatType
fmt.Printf("chatType: %v", *chatType)
if *chatType == "group" {
return GroupHandler
}
Expand All @@ -77,5 +75,12 @@ func judgeMsgType(event *larkim.P2MessageReceiveV1) string {
if *msgType == "text" {
return "text"
}
if *msgType == "image" {
return "image"
}
if *msgType == "audio" {
return "audio"
}

return ""
}
2 changes: 1 addition & 1 deletion code/handlers/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func replayImageByBase64(ctx context.Context, base64Str string,
}
//example := "img_v2_041b28e3-5680-48c2-9af2-497ace79333g"
//imageKey := &example
fmt.Println("imageKey", *imageKey)
//fmt.Println("imageKey", *imageKey)
err = sendImageCard(ctx, *imageKey, msgId, sessionId, question)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 612fcc8

Please sign in to comment.