You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package handle
import (
"WechatGateWay/api"
"WechatGateWay/global"
"WechatGateWay/third_part"
"WechatGateWay/utils"
"encoding/xml"
"fmt"
"log"
"net/http"
)
// cmdExec 根据白名单命令来执行
func cmdExec(msgContent MsgContent, timestamp, nonce string, w http.ResponseWriter) (replyContent string) {
var cmdString string
switch msgContent.Content {
case "last":
cmdString = global.HistoryCmds[len(global.HistoryCmds)-1]
case "history":
cmdString = utils.SlicePrint(global.HistoryCmds)
case "删除图片":
//通过文本消息删除上一张图片
lastImg := GetLastWechatLog(msgContent.FromUsername, "image")
err := utils.DeleteFile(lastImg.Content)
if err != nil {
log.Println("删除图片失败: ", lastImg.Content)
return
} else {
log.Println("图片已删除: ", lastImg.Content)
return
}
case "删除文本":
//通过文本消息删除上一条文本消息
lastText := GetLastWechatLog(msgContent.FromUsername, "text")
textMd5 := utils.Md5String(lastText.Content)
go RecordWechatBlockWords(lastText.Content, textMd5, msgContent.FromUsername)
return
case "虚拟货币", "ETH", "eth", "BTC", "btc", "trx", "TRX", "BNB", "bnb":
priceBTC := api.GetBinanceLatestPrice("BTCUSDT")
priceETH := api.GetBinanceLatestPrice("ETHUSDT")
priceTRX := api.GetBinanceLatestPrice("TRXUSDT")
priceBNB := api.GetBinanceLatestPrice("BNBUSDT")
replyContent = fmt.Sprintf("BTC价格: %f \nETH价格: %f \nTRX价格: %f \nBNB价格: %f", priceBTC, priceETH, priceTRX, priceBNB)
replyImageDraw(msgContent, timestamp, nonce, w)
return
case "gold", "黄金":
goldPrice := api.GetCMBGoldLatestPrice("AU100G")
replyContent = fmt.Sprintf("黄金价格: %f", goldPrice)
return
case "ip", "百度统计":
todayPvCount, todayVisitorCount, todayIpCount := api.GetLatestBaiduReportInfo("sre.ink")
replyContent = fmt.Sprintf("今日百度统计: PV %d, VC %d IP %d", todayPvCount, todayVisitorCount, todayIpCount)
return
default:
cmdString = msgContent.Content
}
global.HistoryCmds = append(global.HistoryCmds, msgContent.Content)
replyContent, err := utils.CMDShellTrick(cmdString)
if err != nil {
replyContent = "/:,@!执行失败:\n" + msgContent.Content
} else {
replyContent = "/::D执行成功\n" + replyContent
//aiAnswer = "执行命令:" + msgContent.Content
}
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("收到命令消息")
replyContent = cmdExec(msgContent, timestamp, nonce, w)
} else {
log.Println("ai msg")
replyContent = aiReply(msgContent)
}
//文本消息审核
if WordsBlocked(utils.Md5String(replyContent)) {
replyContent = "***(过于文明,无法显示)"
}
//构造回复消息
replyMsg, _ := xml.Marshal(ReplyTextMsg{
ToUsername: msgContent.FromUsername,
FromUsername: msgContent.ToUsername,
CreateTime: msgContent.CreateTime,
MsgType: "text",
Content: replyContent,
})
encryptMsg, cryptErr := global.WxCrypt.EncryptMsg(string(replyMsg), timestamp, nonce)
if cryptErr != nil {
log.Println("回复加密出错", cryptErr)
return
} else {
log.Println(string(encryptMsg))
l, err := w.Write(encryptMsg)
if err != nil {
log.Println("返回消息失败")
return
} else {
go RecordWechatLog(msgContent.FromUsername, msgContent.ToUsername, msgContent.CreateTime, msgContent.MsgType, replyContent)
log.Println("成功写入", l)
}
}
}