kefu/wechathook/routers/wechat39825.go

256 lines
7.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package routers
import (
"encoding/json"
"errors"
"github.com/gin-gonic/gin"
"github.com/tidwall/gjson"
"log"
"net/url"
"strings"
"time"
"wechathook/models"
"wechathook/service"
)
var currentWxid string
var currentWxNickname string
var contactList map[string]string
func init() {
//当前微信信息
wechatinfo := Wechat39825Info()
currentWxid = gjson.Get(wechatinfo, "data.wxid").String()
currentWxNickname = gjson.Get(wechatinfo, "data.name").String()
contacts := gjson.Get(Wechat39825ContactList(), "data").Array()
contactList = make(map[string]string, 0)
for _, contact := range contacts {
nickname := contact.Get("nickname").String()
wxid := contact.Get("wxid").String()
contactList[wxid] = nickname
}
}
func Wechat39825(c *gin.Context) {
// 声明一个map变量来存储JSON数据
var jsonData map[string]interface{}
// 解析JSON数据
if err := c.BindJSON(&jsonData); err != nil {
// 如果解析失败,返回错误信息
c.JSON(400, gin.H{"error": err.Error()})
return
}
// 将JSON数据转换为字符串
jsonBytes, _ := json.Marshal(jsonData)
jsonString := string(jsonBytes)
messageType := gjson.Get(jsonString, "type").Int()
log.Println("收到数据:", jsonString)
//好友请求
if messageType == 37 {
return
}
if messageType == 1 {
content := gjson.Get(jsonString, "content").String()
//if messageType == 3 {
// content, _ = BaiduOCR(gjson.Get(jsonString, "base64Img").String())
//}
displayFullContent := gjson.Get(jsonString, "displayFullContent").String()
fromUser := gjson.Get(jsonString, "fromUser").String()
if currentWxid == fromUser {
return
}
//微信账号白名单
wechatWhiteList := models.FindSetting("wechatWhiteList").Value
reply := ""
//群聊里@
if strings.Contains(fromUser, "@chatroom") {
if !strings.Contains(displayFullContent, "在群聊中@了") && !strings.Contains(wechatWhiteList, fromUser) {
return
}
wxidContent := strings.Split(content, ":\n")
wxid := wxidContent[0]
content := strings.Trim(wxidContent[1], "\n")
content = strings.Replace(content, "@"+currentWxNickname, "", -1)
log.Println("解析数据:", fromUser, displayFullContent, wxid, content)
if models.FindSettingDefault("groupKeywordStatus", "yes") != "no" {
reply = service.GetReplyFromLocal(content)
}
if reply == "" && models.FindSettingDefault("groupAIStatus", "yes") != "no" {
reply, _ = service.GetReplyByAPI(fromUser, contactList[fromUser], "", content)
}
if reply != "" {
SendWechat39825AtMsg(wxid, fromUser, reply)
images := service.ExtractMarkdownImagePaths(reply)
if len(images) != 0 {
filePath, _ := service.DownloadImageToDailyDir(images[0].Path, "./resource", images[0].Desc)
if filePath != "" {
SendWechat39825AtMsg(wxid, fromUser, service.GetRootPath()+"\\"+filePath)
}
}
}
return
}
//私聊
//if wechatWhiteList != "" && !strings.Contains(wechatWhiteList, fromUser) {
// return
//}
if models.FindSettingDefault("personKeywordStatus", "yes") != "no" {
reply = service.GetReplyFromLocal(content)
}
if reply == "" && models.FindSettingDefault("personAIStatus", "yes") != "no" {
reply, _ = service.GetReplyByAPI(fromUser, contactList[fromUser], "", content)
}
if reply != "" {
SendWechat39825Msg(fromUser, reply)
images := service.ExtractMarkdownImagePaths(reply)
if len(images) != 0 {
filePath, _ := service.DownloadImageToDailyDir(images[0].Path, "./resource", images[0].Desc)
if filePath != "" {
SendWechat39825Msg(fromUser, service.GetRootPath()+"\\"+filePath)
}
}
}
}
c.JSON(200, gin.H{
"message": "pong",
})
}
// 发送微信消息
func SendWechat39825AtMsg(wxids, chatRoomId, msg string) (string, error) {
if msg == "" {
return "", errors.New("回复内容为空!")
}
if service.IsWindowsPath(msg) {
SendWechat39825File(chatRoomId, msg)
return "", nil
}
sleepTime, _ := service.GetRandomValue(models.FindSettingDefault("delayTime", "0"))
time.Sleep(time.Duration(sleepTime) * time.Second)
url := "http://127.0.0.1:19088/api/sendAtText"
data := map[string]interface{}{
"wxids": wxids,
"chatRoomId": chatRoomId,
"msg": msg,
}
respBytes, err := service.PostJSON(url, data)
respString := string(respBytes)
log.Println("发送信息:", respString, msg)
if err != nil {
return "", err
}
return respString, nil
}
// 发送微信消息
func SendWechat39825Msg(wxid, msg string) (string, error) {
if msg == "" {
return "", errors.New("回复内容为空!")
}
if service.IsWindowsPath(msg) {
SendWechat39825File(wxid, msg)
return "", nil
}
sleepTime, _ := service.GetRandomValue(models.FindSettingDefault("delayTime", "0"))
time.Sleep(time.Duration(sleepTime) * time.Second)
url := "http://127.0.0.1:19088/api/sendTextMsg"
data := map[string]interface{}{
"wxid": wxid,
"msg": msg,
}
respBytes, err := service.PostJSON(url, data)
respString := string(respBytes)
log.Println("发送信息:", respString, msg)
if err != nil {
return "", err
}
return respString, nil
}
// 发送微信文件
func SendWechat39825File(wxid, filePath string) (string, error) {
if filePath == "" {
return "", errors.New("回复内容为空!")
}
sleepTime, _ := service.GetRandomValue(models.FindSettingDefault("delayTime", "0"))
time.Sleep(time.Duration(sleepTime) * time.Second)
url := "http://127.0.0.1:19088/api/sendFileMsg"
data := map[string]interface{}{
"wxid": wxid,
"filePath": filePath,
}
if service.IsImagePath(filePath) {
url = "http://127.0.0.1:19088/api/sendImagesMsg"
data = map[string]interface{}{
"wxid": wxid,
"imagePath": filePath,
}
}
respBytes, err := service.PostJSON(url, data)
respString := string(respBytes)
log.Println("发送信息:", respString, filePath)
if err != nil {
return "", err
}
return respString, nil
}
func Wechat39825Info() string {
url := "http://127.0.0.1:19088/api/userInfo"
data := map[string]interface{}{}
respBytes, _ := service.PostJSON(url, data)
respString := string(respBytes)
log.Println("当前微信信息:", respString)
return respString
}
func Wechat39825ContactList() string {
url := "http://127.0.0.1:19088/api/getContactList"
data := map[string]interface{}{}
respBytes, _ := service.PostJSON(url, data)
respString := string(respBytes)
log.Println("当前微信联系人:", respString)
return respString
}
func Wechat39825Hook(c *gin.Context) {
url := "http://127.0.0.1:19088/api/hookSyncMsg"
data := map[string]interface{}{
"port": "19099",
"ip": "127.0.0.1",
"url": "http://localhost:8089/wechat39825",
"timeout": "3000",
"enableHttp": true,
}
respBytes, _ := service.PostJSON(url, data)
respString := string(respBytes)
log.Println("发送Hook信息", respString)
c.Writer.Write(respBytes)
}
func Wechat39825Contacts(c *gin.Context) {
resp := Wechat39825ContactList()
c.Writer.Write([]byte(resp))
}
func Wechat39825Remark(c *gin.Context) {
url := "http://127.0.0.1:19088/api/getDBInfo"
data := map[string]interface{}{}
respBytes, _ := service.PostJSON(url, data)
respString := string(respBytes)
log.Println("当前微信数据库信息:", respString)
}
func BaiduOCR(imgbase64 string) (string, error) {
b := service.BaiduOCR{
API_KEY: "NrtkiynNnqkQOwm6isip58gS",
SECRET_KEY: "CO9594vqWsXVdhQEfXSNlDeHu5Jk7lWE",
}
r, err := b.OCR(url.QueryEscape(imgbase64))
log.Println(r, err)
return r, err
}