modifyudeskstate/exceldeal/notify.go

57 lines
1.1 KiB
Go
Raw Normal View History

2024-07-21 02:13:35 +08:00
package exceldeal
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
)
var webhookURL = getWebhook()
type webHook struct {
WebHookURL string `json:"webhook"`
}
func getWebhook() string {
wb, err := os.ReadFile("./wechatwebhook.json")
fmt.Println(string(wb))
if err != nil {
log.Printf("打开webhook文件失败:%v\n", err)
return ""
}
var webhook webHook
err = json.Unmarshal(wb, &webhook)
if err != nil {
log.Printf("webhook config 反序列化失败: %v\n", err)
return ""
}
hook := webhook.WebHookURL
return hook
}
func Send(repContent string) {
// 替换为你的企业微信机器人Webhook URL
// 创建消息体
requestBody := []byte(`{
"msgtype": "text",
"text": {
"content": "` + repContent + `"
}
}`)
// 发送HTTP POST请求到企业微信机器人Webhook
resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(requestBody))
if err != nil {
log.Fatalf("发送消息失败: %v", err)
return
}
defer resp.Body.Close()
log.Println("发送企业微信机器人成功!")
}