aiweek-reconstruction/notifer/wechat.go

38 lines
997 B
Go
Raw Permalink Blame History

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 notifer
import (
"aiweek/option"
"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
resp, err := http.Post(option.WECHAT_WEBHOOK, "application/json", strings.NewReader(repContent))
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))
}