From 2818784c9fe34760c5a3858dc0e4df22b2c883ce Mon Sep 17 00:00:00 2001 From: dustoair <107600816+dustoair@users.noreply.github.com> Date: Mon, 12 Sep 2022 21:19:59 +0800 Subject: [PATCH] replyContent --- api/models.go | 24 ++++++++++++++++++++++ handle/replyText.go | 50 ++++++++++++++++++++++++++++----------------- main.go | 2 ++ 3 files changed, 57 insertions(+), 19 deletions(-) create mode 100644 api/models.go diff --git a/api/models.go b/api/models.go new file mode 100644 index 0000000..b1cca6f --- /dev/null +++ b/api/models.go @@ -0,0 +1,24 @@ +package api + +import ( + "WechatGateWay/global" + "github.com/jinzhu/gorm" +) + +/** +@author: sre +@date: 2022/9/12 0012 +@desc: todo +**/ + +type BinancePrice struct { + gorm.Model + Symbol string + Price float64 +} + +func GetBinanceLatestPrice(symbol string) float64 { + var binancePrice BinancePrice + global.DB.Where("symbol = ?", symbol).Last(&binancePrice) + return binancePrice.Price +} diff --git a/handle/replyText.go b/handle/replyText.go index 100024f..c342a71 100644 --- a/handle/replyText.go +++ b/handle/replyText.go @@ -1,16 +1,19 @@ package handle import ( + "WechatGateWay/api" "WechatGateWay/global" "WechatGateWay/third_part" "WechatGateWay/utils" "encoding/xml" + "fmt" "log" "net/http" ) // cmdExec 根据白名单命令来执行 -func cmdExec(msgContent MsgContent) (cmdString string) { +func cmdExec(msgContent MsgContent) (replyContent string) { + var cmdString string switch msgContent.Content { case "last": cmdString = global.HistoryCmds[len(global.HistoryCmds)-1] @@ -34,9 +37,13 @@ func cmdExec(msgContent MsgContent) (cmdString string) { go RecordWechatBlockWords(lastText.Content, textMd5, msgContent.FromUsername) return case "eth", "btc", "虚拟货币": - cmdString = "cmdString" + priceBTC := api.GetBinanceLatestPrice("BTCUSDT") + priceETH := api.GetBinanceLatestPrice("ETHUSDT") + replyContent = fmt.Sprintf("BTC: %f \n ETH: %f", priceBTC, priceETH) + return + case "gold", "黄金": - cmdString = "cmdString" + replyContent = "黄金黄金黄金黄金黄金" default: cmdString = msgContent.Content } @@ -49,14 +56,31 @@ func cmdExec(msgContent MsgContent) (cmdString string) { //aiAnswer = "执行命令:" + msgContent.Content } - return cmdString + return +} +func aiReply(msgContent MsgContent) (replyContent string) { + var replyContentAi string + if _, ok := global.EmojMap[msgContent.Content]; ok { + //系统表情消息 取字典意思丢ai回复 + replyContentAi = third_part.AiChat(global.EmojMap[msgContent.Content]) + } else { + //普通文本消息直接丢ai + replyContentAi = third_part.AiChat(msgContent.Content) + } + //对ai结果 有表情包的上表情包 + if emoj, ok := utils.MapKey(global.EmojMap, replyContentAi); ok { + replyContent = emoj + replyContentAi + } else { + replyContent = replyContentAi + } + return } func replyText(msgContent MsgContent, timestamp, nonce string, w http.ResponseWriter) { var replyContent string if global.EnableCmdExec && utils.SliceContain(global.CMDList, msgContent.Content) && msgContent.FromUsername == "QiaoYang" { //开启了命令执行功能,并且消息内容包含了命令 log.Println("收到命令消息") - cmdExec(msgContent) + replyContent = cmdExec(msgContent) //cmdString := cmdExec(msgContent) //var cmdString string //if msgContent.Content == "last" { @@ -94,20 +118,8 @@ func replyText(msgContent MsgContent, timestamp, nonce string, w http.ResponseWr // //aiAnswer = "执行命令:" + msgContent.Content //} } else { - var replyContentAi string - if _, ok := global.EmojMap[msgContent.Content]; ok { - //系统表情消息 取字典意思丢ai回复 - replyContentAi = third_part.AiChat(global.EmojMap[msgContent.Content]) - } else { - //普通文本消息直接丢ai - replyContentAi = third_part.AiChat(msgContent.Content) - } - //对ai结果 有表情包的上表情包 - if emoj, ok := utils.MapKey(global.EmojMap, replyContentAi); ok { - replyContent = emoj + replyContentAi - } else { - replyContent = replyContentAi - } + log.Println("ai msg") + replyContent = aiReply(msgContent) } //文本消息审核 diff --git a/main.go b/main.go index 6a226d9..f58a440 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "WechatGateWay/api" "WechatGateWay/global" "WechatGateWay/handle" "WechatGateWay/third_part" @@ -68,6 +69,7 @@ func InitDB() *gorm.DB { //自动建表 db.AutoMigrate(&handle.WechatLog{}) db.AutoMigrate(&handle.WechatBlockWords{}) + db.AutoMigrate(&api.BinancePrice{}) global.DB = db return db