-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split event action into separate files
- Loading branch information
1 parent
c918df3
commit 5751762
Showing
5 changed files
with
315 additions
and
292 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
Oops, something went wrong.