Skip to content

Commit

Permalink
refactor: split event action into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
Leizhenpeng committed Mar 27, 2023
1 parent c918df3 commit 5751762
Show file tree
Hide file tree
Showing 5 changed files with 315 additions and 292 deletions.
292 changes: 0 additions & 292 deletions code/handlers/event_action.go

This file was deleted.

62 changes: 62 additions & 0 deletions code/handlers/event_audio_action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package handlers

import (
"context"
"fmt"
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
"os"
"start-feishubot/initialization"
"start-feishubot/utils/audio"
)

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, fmt.Sprintf("🤖️:语音转换失败,请稍后再试~\n错误信息: %v", err), a.info.msgId)
return false
}

replyMsg(*a.ctx, fmt.Sprintf("🤖️:%s", text), a.info.msgId)
//fmt.Println("text: ", text)
a.info.qParsed = text
return true
}

return true

}
Loading

0 comments on commit 5751762

Please sign in to comment.