aiweek-reconstruction/notifer/wechat.go

38 lines
997 B
Go
Raw Normal View History

2024-05-18 01:46:35 +08:00
package notifer
import (
2024-05-20 01:00:09 +08:00
"aiweek/option"
2024-05-18 01:46:35 +08:00
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
)
func SendWechat(repContent string) {
// 替换为你的企业微信机器人Webhook URL
jsonData, err := json.Marshal(repContent)
if err != nil {
fmt.Println("转换为JSON时出错:", err)
return
}
repContent = string(jsonData)
// 创建消息体
repContent = fmt.Sprintf(`{"msgtype": "text", "text": {"content": %s}}`, repContent)
fmt.Println(repContent)
// 发送HTTP POST请求到企业微信机器人Webhook
2024-05-20 01:00:09 +08:00
resp, err := http.Post(option.WECHAT_WEBHOOK, "application/json", strings.NewReader(repContent))
2024-05-18 01:46:35 +08:00
if err != nil {
log.Printf("发送企业微信机器人失败,错误是: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("发送企业微信机器人失败,错误是: %v", err)
}
log.Printf("发送企业微信机器人完成,状态码为:%v返回体为%v", resp.StatusCode, string(body))
}