2023-09-25 14:16:59 +08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-12-07 15:58:55 +08:00
|
|
|
"fmt"
|
2023-09-25 14:16:59 +08:00
|
|
|
"github.com/go-resty/resty/v2"
|
2023-12-07 15:58:55 +08:00
|
|
|
"go-wechat/common/current"
|
2023-10-26 10:07:08 +08:00
|
|
|
"go-wechat/config"
|
2023-09-25 14:16:59 +08:00
|
|
|
"log"
|
2023-09-26 15:12:47 +08:00
|
|
|
"time"
|
2023-09-25 14:16:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// SendMessage
|
|
|
|
// @description: 发送消息
|
|
|
|
// @param toId
|
|
|
|
// @param atId
|
|
|
|
// @param msg
|
2023-09-26 15:12:47 +08:00
|
|
|
func SendMessage(toId, atId, msg string, retryCount int) {
|
|
|
|
if retryCount > 5 {
|
|
|
|
log.Printf("重试五次失败,停止发送")
|
|
|
|
return
|
|
|
|
}
|
2023-11-13 13:32:42 +08:00
|
|
|
|
2023-09-25 14:16:59 +08:00
|
|
|
// 组装参数
|
|
|
|
param := map[string]any{
|
|
|
|
"wxid": toId, // 群或好友Id
|
|
|
|
"msg": msg, // 消息
|
|
|
|
}
|
2023-11-13 13:32:42 +08:00
|
|
|
|
|
|
|
// 接口地址
|
|
|
|
apiUrl := config.Conf.Wechat.GetURL("/api/sendTextMsg")
|
|
|
|
if atId != "" {
|
|
|
|
apiUrl = config.Conf.Wechat.GetURL("/api/sendAtText")
|
|
|
|
param = map[string]any{
|
|
|
|
"chatRoomId": toId,
|
|
|
|
"wxids": atId,
|
|
|
|
"msg": msg, // 消息
|
|
|
|
}
|
|
|
|
}
|
2023-09-25 14:16:59 +08:00
|
|
|
pbs, _ := json.Marshal(param)
|
|
|
|
|
|
|
|
res := resty.New()
|
|
|
|
resp, err := res.R().
|
|
|
|
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
|
|
|
SetBody(string(pbs)).
|
2023-11-13 13:32:42 +08:00
|
|
|
Post(apiUrl)
|
2023-09-25 14:16:59 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("发送文本消息失败: %s", err.Error())
|
2023-09-26 15:12:47 +08:00
|
|
|
// 休眠五秒后重新发送
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
SendMessage(toId, atId, msg, retryCount+1)
|
2023-09-25 14:16:59 +08:00
|
|
|
}
|
|
|
|
log.Printf("发送文本消息结果: %s", resp.String())
|
|
|
|
}
|
2023-11-03 11:59:40 +08:00
|
|
|
|
|
|
|
// SendImage
|
|
|
|
// @description: 发送图片
|
|
|
|
// @param toId string 群或者好友Id
|
|
|
|
// @param imgPath string 图片路径
|
|
|
|
// @param retryCount int 重试次数
|
|
|
|
func SendImage(toId, imgPath string, retryCount int) {
|
|
|
|
if retryCount > 5 {
|
|
|
|
log.Printf("重试五次失败,停止发送")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 组装参数
|
|
|
|
param := map[string]any{
|
|
|
|
"wxid": toId, // 群或好友Id
|
|
|
|
"imagePath": imgPath, // 图片地址
|
|
|
|
}
|
|
|
|
pbs, _ := json.Marshal(param)
|
|
|
|
|
|
|
|
res := resty.New()
|
|
|
|
resp, err := res.R().
|
|
|
|
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
|
|
|
SetBody(string(pbs)).
|
|
|
|
Post(config.Conf.Wechat.GetURL("/api/sendImagesMsg"))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("发送图片消息失败: %s", err.Error())
|
|
|
|
// 休眠五秒后重新发送
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
SendImage(toId, imgPath, retryCount+1)
|
|
|
|
}
|
|
|
|
log.Printf("发送图片消息结果: %s", resp.String())
|
|
|
|
}
|
2023-12-07 15:58:55 +08:00
|
|
|
|
|
|
|
// SendEmotion
|
|
|
|
// @description: 发送自定义表情包
|
|
|
|
// @param toId string 群或者好友Id
|
|
|
|
// @param emotionHash string 表情包hash(md5值)
|
|
|
|
// @param retryCount int 重试次数
|
|
|
|
func SendEmotion(toId, emotionHash string, retryCount int) {
|
|
|
|
if retryCount > 5 {
|
|
|
|
log.Printf("重试五次失败,停止发送")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 组装表情包本地地址
|
|
|
|
// 规则:机器人数据目录\FileStorage\CustomEmotion\表情包hash前两位\表情包hash
|
|
|
|
emotionPath := fmt.Sprintf("%sFileStorage\\CustomEmotion\\%s\\%s",
|
|
|
|
current.GetRobotInfo().CurrentDataPath, emotionHash[:2], emotionHash)
|
|
|
|
|
|
|
|
// 组装参数
|
|
|
|
param := map[string]any{
|
|
|
|
"wxid": toId, // 群或好友Id
|
|
|
|
"filePath": emotionPath, // 图片地址
|
|
|
|
}
|
|
|
|
pbs, _ := json.Marshal(param)
|
|
|
|
|
|
|
|
res := resty.New()
|
|
|
|
resp, err := res.R().
|
|
|
|
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
|
|
|
SetBody(string(pbs)).
|
2023-12-07 16:04:32 +08:00
|
|
|
Post(config.Conf.Wechat.GetURL("/api/sendCustomEmotion"))
|
2023-12-07 15:58:55 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("发送表情包消息失败: %s", err.Error())
|
|
|
|
// 休眠五秒后重新发送
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
SendImage(toId, emotionHash, retryCount+1)
|
|
|
|
}
|
|
|
|
log.Printf("发送表情包消息结果: %s", resp.String())
|
|
|
|
}
|