39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
|
package notifer
|
|||
|
|
|||
|
import (
|
|||
|
"encoding/json"
|
|||
|
"fmt"
|
|||
|
"io"
|
|||
|
"log"
|
|||
|
"net/http"
|
|||
|
"strings"
|
|||
|
)
|
|||
|
|
|||
|
const webhookURL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=30155eca-29d2-4e65-84bf-f31e2dac2074"
|
|||
|
|
|||
|
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(webhookURL, "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))
|
|||
|
|
|||
|
}
|