|
@@ -0,0 +1,42 @@
|
|
|
+package utils
|
|
|
+
|
|
|
+import (
|
|
|
+ "bytes"
|
|
|
+ "encoding/json"
|
|
|
+ "github.com/gogf/gf/v2/frame/g"
|
|
|
+ "github.com/gogf/gf/v2/os/gctx"
|
|
|
+ "net/http"
|
|
|
+)
|
|
|
+
|
|
|
+func SendMsgByWXURL(msg string, whs []string) {
|
|
|
+ for _, url := range whs {
|
|
|
+ if ok := SendBot(url, msg); !ok {
|
|
|
+ g.Log().Info(gctx.New(), "企业微信机器人提醒失败--:", url, msg)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func SendBot(webhookURL, msg string) (b bool) {
|
|
|
+ // 构造请求体
|
|
|
+ payload := map[string]interface{}{
|
|
|
+ "msgtype": "text",
|
|
|
+ "text": map[string]string{
|
|
|
+ "content": msg,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ // 转换为 JSON 字符串
|
|
|
+ payloadBytes, err := json.Marshal(payload)
|
|
|
+ if err != nil {
|
|
|
+ g.Log().Info(gctx.New(), "Error :", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 发送 POST 请求
|
|
|
+ resp, err := http.Post(webhookURL, "application/json", bytes.NewReader(payloadBytes))
|
|
|
+ if err != nil {
|
|
|
+ g.Log().Info(gctx.New(), "Error :", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+ b = true
|
|
|
+ return
|
|
|
+}
|