Skip to content

Commit

Permalink
Beta68 (#200)
Browse files Browse the repository at this point in the history
* beta68

* beta68

* beta68
  • Loading branch information
Hoshinonyaruko authored Dec 1, 2023
1 parent 0659948 commit af61caf
Showing 1 changed file with 57 additions and 62 deletions.
119 changes: 57 additions & 62 deletions handlers/send_group_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/hoshinonyaruko/gensokyo/images"
"github.com/hoshinonyaruko/gensokyo/mylog"
"github.com/hoshinonyaruko/gensokyo/silk"
"github.com/hoshinonyaruko/gensokyo/url"
"github.com/tencent-connect/botgo/dto"
"github.com/tencent-connect/botgo/openapi"
)
Expand Down Expand Up @@ -481,15 +480,15 @@ func generateGroupMessage(id string, foundItems map[string][]string, messageText
}
}
// 将图片链接缩短 避免 url not allow
if config.GetLotusValue() {
// 连接到另一个gensokyo
newURL = url.GenerateShortURL(newURL)
} else {
// 自己是主节点
newURL = url.GenerateShortURL(newURL)
// 使用getBaseURL函数来获取baseUrl并与newURL组合
newURL = url.GetBaseURL() + "/url/" + newURL
}
// if config.GetLotusValue() {
// // 连接到另一个gensokyo
// newURL = url.GenerateShortURL(newURL)
// } else {
// // 自己是主节点
// newURL = url.GenerateShortURL(newURL)
// // 使用getBaseURL函数来获取baseUrl并与newURL组合
// newURL = url.GetBaseURL() + "/url/" + newURL
// }
newpiclink = newURL
} else {
newpiclink = "http://" + imageURLs[0]
Expand Down Expand Up @@ -541,68 +540,64 @@ func generateGroupMessage(id string, foundItems map[string][]string, messageText
}
}
} else if imageURLs, ok := foundItems["url_record"]; ok && len(imageURLs) > 0 {
var newpiclink string
if config.GetUrlPicTransfer() {
// 从URL下载图片
resp, err := http.Get("http://" + imageURLs[0])
if err != nil {
mylog.Printf("Error downloading the record: %v", err)
return &dto.MessageToCreate{
Content: "错误: 下载语音失败",
MsgID: id,
MsgSeq: msgseq,
MsgType: 0, // 默认文本类型
}
// 从URL下载语音
resp, err := http.Get("http://" + imageURLs[0])
if err != nil {
mylog.Printf("Error downloading the record: %v", err)
return &dto.MessageToCreate{
Content: "错误: 下载语音失败",
MsgID: id,
MsgSeq: msgseq,
MsgType: 0, // 默认文本类型
}
defer resp.Body.Close()
}
defer resp.Body.Close()

// 读取图片数据
recordData, err := io.ReadAll(resp.Body)
if err != nil {
mylog.Printf("Error reading the record data: %v", err)
return &dto.MessageToCreate{
Content: "错误: 读取语音数据失败",
MsgID: id,
MsgSeq: msgseq,
MsgType: 0,
}
// 读取语音数据
recordData, err := io.ReadAll(resp.Body)
if err != nil {
mylog.Printf("Error reading the record data: %v", err)
return &dto.MessageToCreate{
Content: "错误: 读取语音数据失败",
MsgID: id,
MsgSeq: msgseq,
MsgType: 0,
}

// 转换为base64
base64Encoded := base64.StdEncoding.EncodeToString(recordData)

// 上传图片并获取新的URL
newURL, err := images.UploadBase64RecordToServer(base64Encoded)
}
//判断并转码
if !silk.IsAMRorSILK(recordData) {
mt, ok := silk.CheckAudio(bytes.NewReader(recordData))
if !ok {
mylog.Errorf("voice type error: " + mt)
return nil
}
recordData = silk.EncoderSilk(recordData)
mylog.Errorf("音频转码ing")
if err != nil {
mylog.Printf("Error uploading base64 encoded image: %v", err)
return &dto.MessageToCreate{
Content: "错误: 上传图片失败",
MsgID: id,
MsgSeq: msgseq,
MsgType: 0,
}
return nil
}
// 将图片链接缩短 避免 url not allow
if config.GetLotusValue() {
// 连接到另一个gensokyo
newURL = url.GenerateShortURL(newURL)
} else {
// 自己是主节点
newURL = url.GenerateShortURL(newURL)
// 使用getBaseURL函数来获取baseUrl并与newURL组合
newURL = url.GetBaseURL() + "/url/" + newURL
}
// 转换为base64
base64Encoded := base64.StdEncoding.EncodeToString(recordData)

// 上传语音并获取新的URL
newURL, err := images.UploadBase64RecordToServer(base64Encoded)
if err != nil {
mylog.Printf("Error uploading base64 encoded image: %v", err)
return &dto.MessageToCreate{
Content: "错误: 上传图片失败",
MsgID: id,
MsgSeq: msgseq,
MsgType: 0,
}
newpiclink = newURL
} else {
newpiclink = "http://" + imageURLs[0]
}

// 发链接图片
// 发链接语音
return &dto.RichMediaMessage{
EventID: id,
FileType: 3, // 3代表语音
URL: newpiclink, // 新语音链接
Content: "", // 这个字段文档没有了
FileType: 3, // 3代表语音
URL: newURL, // 新语音链接
Content: "", // 这个字段文档没有了
SrvSendMsg: false,
}
} else if base64_image, ok := foundItems["base64_image"]; ok && len(base64_image) > 0 {
Expand Down

0 comments on commit af61caf

Please sign in to comment.