Skip to content

Commit

Permalink
service msg
Browse files Browse the repository at this point in the history
  • Loading branch information
lz1998 committed Nov 14, 2020
1 parent 957916d commit 17e2d0e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion service/bot/mirai2raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func MiraiMsgToRawMsg(messageChain []message.IMessageElement) string {
case *message.VoiceElement:
result += fmt.Sprintf(`<voice url="%s"/>`, html.EscapeString(elem.Url))
case *message.ServiceElement:
result += fmt.Sprintf(`<service id="%d" content="%s" res_id="%d" sub_type="%s"/>`, elem.Id, html.EscapeString(elem.Content), elem.ResId, elem.SubType)
result += fmt.Sprintf(`<service id="%d" content="%s" res_id="%s" sub_type="%s"/>`, elem.Id, html.EscapeString(elem.Content), elem.ResId, elem.SubType)
case *message.LightAppElement:
result += fmt.Sprintf(`<light_app content="%s"/>`, html.EscapeString(elem.Content))
case *message.ShortVideoElement:
Expand Down
38 changes: 35 additions & 3 deletions service/bot/proto2mirai.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bot

import (
"html"
"strconv"
"strings"

Expand Down Expand Up @@ -45,6 +44,8 @@ func ProtoMsgToMiraiMsg(msgList []*onebot.Message, notConvertText bool) []messag
messageChain = append(messageChain, ProtoShareToMiraiShare(protoMsg.Data))
case "light_app":
messageChain = append(messageChain, ProtoLightAppToMiraiLightApp(protoMsg.Data))
case "service":
messageChain = append(messageChain, ProtoServiceToMiraiService(protoMsg.Data))
default:
log.Errorf("不支持的消息类型 %+v", protoMsg)
}
Expand Down Expand Up @@ -73,7 +74,6 @@ func ProtoImageToMiraiImage(data map[string]string) message.IMessageElement {
log.Warnf("imageUrl不存在")
return EmptyText()
}
url = html.UnescapeString(url)
log.Infof("下载图片: %+v", url)
b, err := util.GetBytes(url)
if err != nil {
Expand All @@ -92,7 +92,6 @@ func ProtoVoiceToMiraiVoice(data map[string]string) message.IMessageElement {
log.Warnf("recordUrl不存在")
return EmptyText()
}
url = html.UnescapeString(url)
b, err := util.GetBytes(url)
if err != nil {
log.Errorf("下载语音失败")
Expand Down Expand Up @@ -163,3 +162,36 @@ func ProtoLightAppToMiraiLightApp(data map[string]string) message.IMessageElemen
}
return message.NewLightApp(content)
}

func ProtoServiceToMiraiService(data map[string]string) message.IMessageElement {
subType, ok := data["sub_type"]
if !ok || subType == "" {
log.Warnf("service sub_type不存在")
return EmptyText()
}

content, ok := data["content"]
if !ok {
log.Warnf("service content不存在")
return EmptyText()
}

id, ok := data["id"]
if !ok {
id = ""
}
resId, err := strconv.ParseInt(id, 10, 64)
if err != nil || resId == 0 {
if subType == "xml" {
resId = 60 // xml默认60
} else {
resId = 1 // json默认1
}
}

return &message.ServiceElement{
Id: int32(resId),
Content: content,
SubType: subType,
}
}
2 changes: 2 additions & 0 deletions service/bot/raw2mirai.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func RawMsgToMiraiMsg(str string) []message.IMessageElement {
elemList = append(elemList, ProtoTextToMiraiText(attrMap))
case "light_app":
elemList = append(elemList, ProtoLightAppToMiraiLightApp(attrMap))
case "service":
elemList = append(elemList, ProtoServiceToMiraiService(attrMap))
default:
log.Warnf("不支持的类型 %s", code)
elemList = append(elemList, message.NewText(code))
Expand Down

0 comments on commit 17e2d0e

Please sign in to comment.