qq_bot/handler/xibao/xibao.go

128 lines
4.4 KiB
Go

package xibao
import (
"fmt"
"strings"
"git.lxtend.com/lixiangwuxian/qqbot/constants"
"git.lxtend.com/lixiangwuxian/qqbot/handler"
"git.lxtend.com/lixiangwuxian/qqbot/model"
"git.lxtend.com/lixiangwuxian/qqbot/qq_message"
"git.lxtend.com/lixiangwuxian/qqbot/service/xibao"
"git.lxtend.com/lixiangwuxian/qqbot/util"
"github.com/google/uuid"
)
func init() {
handler.RegisterHandler("喜报", xiBao, constants.LEVEL_USER)
handler.RegisterHelpInform("喜报 [内容]", "喜报/悲报", "生成喜报图片,支持换行或多消息发送")
handler.RegisterHandler("悲报", beiBao, constants.LEVEL_USER)
handler.RegisterHelpInform("悲报 [内容]", "喜报/悲报", "生成悲报图片,支持换行或多消息发送")
}
func xiBao(msg model.Message) (reply *model.Reply) {
fileName := uuid.New().String()
filePath := util.GenTempFilePath(fmt.Sprintf("%s.png", fileName))
if len(msg.StructuredMsg) == 1 {
if msg.StructuredMsg[0].GetMessageType() != qq_message.TypeText {
return nil
} else if msg.StructuredMsg[0].(*qq_message.TextMessage).Data.Text == "喜报" {
handler.RegisterLiveHandler(msg.GroupInfo.GroupId, msg.UserId, xiBaoTemp)
return nil
}
msg.StructuredMsg[0].(*qq_message.TextMessage).Data.Text = strings.TrimPrefix(msg.StructuredMsg[0].(*qq_message.TextMessage).Data.Text, "喜报 ")
xibao.GenerateCongratulationImageNew(msg.StructuredMsg[0], "./resource/xibao_background.png", filePath, true)
}
if len(msg.StructuredMsg) == 2 {
xibao.GenerateCongratulationImageNew(msg.StructuredMsg[1], "./resource/xibao_background.png", filePath, true)
}
imageMsg := qq_message.ImageMessage{
Type: "image",
Data: qq_message.ImageMessageData{
File: "file://" + filePath,
},
}
return &model.Reply{
ReplyMsg: imageMsg,
ReferOriginMsg: true,
FromMsg: msg,
}
}
func xiBaoTemp(msg model.Message) (reply *model.Reply, isTrigger bool) {
handler.UnRegisterLiveHandler(msg.GroupInfo.GroupId, msg.UserId)
fileName := uuid.New().String()
filePath := util.GenTempFilePath(fmt.Sprintf("%s.png", fileName))
var contentMsg qq_message.QQMessage
if len(msg.StructuredMsg) > 0 {
contentMsg = msg.StructuredMsg[0]
} else {
contentMsg = qq_message.NewTextMessage().ParseMessage(msg.RawMsg)
}
xibao.GenerateCongratulationImageNew(contentMsg, "./resource/xibao_background.png", filePath, true)
imageMsg := qq_message.ImageMessage{
Type: "image",
Data: qq_message.ImageMessageData{
File: "file://" + filePath,
},
}
return &model.Reply{
ReplyMsg: imageMsg,
ReferOriginMsg: true,
FromMsg: msg,
}, true
}
func beiBao(msg model.Message) (reply *model.Reply) {
fileName := uuid.New().String()
filePath := util.GenTempFilePath(fmt.Sprintf("%s.png", fileName))
if len(msg.StructuredMsg) == 1 {
if msg.StructuredMsg[0].GetMessageType() != "text" {
return nil
} else if msg.StructuredMsg[0].(*qq_message.TextMessage).Data.Text == "悲报" {
handler.RegisterLiveHandler(msg.GroupInfo.GroupId, msg.UserId, beiBaoTemp)
return nil
}
msg.StructuredMsg[0].(*qq_message.TextMessage).Data.Text = strings.TrimPrefix(msg.StructuredMsg[0].(*qq_message.TextMessage).Data.Text, "悲报 ")
xibao.GenerateCongratulationImageNew(msg.StructuredMsg[0], "./resource/beibao_background.png", filePath, false)
}
if len(msg.StructuredMsg) == 2 {
xibao.GenerateCongratulationImageNew(msg.StructuredMsg[1], "./resource/beibao_background.png", filePath, false)
}
imageMsg := qq_message.ImageMessage{
Type: "image",
Data: qq_message.ImageMessageData{
File: "file://" + filePath,
},
}
return &model.Reply{
ReplyMsg: imageMsg,
ReferOriginMsg: true,
FromMsg: msg,
}
}
func beiBaoTemp(msg model.Message) (reply *model.Reply, isTrigger bool) {
handler.UnRegisterLiveHandler(msg.GroupInfo.GroupId, msg.UserId)
fileName := uuid.New().String()
filePath := util.GenTempFilePath(fmt.Sprintf("%s.png", fileName))
var contentMsg qq_message.QQMessage
if len(msg.StructuredMsg) > 0 {
contentMsg = msg.StructuredMsg[0]
} else {
contentMsg = qq_message.NewTextMessage().ParseMessage(msg.RawMsg)
}
xibao.GenerateCongratulationImageNew(contentMsg, "./resource/beibao_background.png", filePath, false)
imageMsg := qq_message.ImageMessage{
Type: "image",
Data: qq_message.ImageMessageData{
File: "file://" + filePath,
},
}
return &model.Reply{
ReplyMsg: imageMsg,
ReferOriginMsg: true,
FromMsg: msg,
}, true
}