123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- package wxrobot
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "github.com/SongZihuan/web-watcher/src/config"
- "io"
- "net/http"
- )
- const (
- msgtypetext = "text"
- msgtypemarkdown = "markdown"
- )
- const atall = "@all"
- type WebhookText struct {
- Content string `json:"content"`
- MentionedList []string `json:"mentioned_list"`
- MentionedMobileList []string `json:"mentioned_mobile_list"`
- }
- type WebhookMarkdown struct {
- Content string `json:"content"`
- }
- type ReqWebhookMsg struct {
- MsgType string `json:"msgtype"`
- Text *WebhookText `json:"text,omitempty"`
- Markdown *WebhookMarkdown `json:"markdown,omitempty"`
- }
- type RespWebhookMsg struct {
- ErrCode int `json:"errcode"`
- ErrMsg string `json:"errmsg"`
- }
- func Send(msg string, atAll bool) error {
- if msg == "" {
- return nil
- }
- return send(fmt.Sprintf("【%s 消息提醒】\n%s", config.GetConfig().SystemName, msg), atAll)
- }
- func send(msg string, atAll bool) error {
- if !config.IsReady() {
- panic("config is not ready")
- }
- webhook := config.GetConfig().API.Webhook
- if webhook == "" || msg == "" {
- return nil
- }
- if len([]byte(msg)) >= 2048 {
- return fmt.Errorf("msg too long")
- }
- data := ReqWebhookMsg{
- MsgType: msgtypetext,
- Text: &WebhookText{
- Content: msg,
- },
- }
- if atAll {
- data.Text.MentionedMobileList = []string{atall}
- }
- webhookData, err := json.Marshal(data)
- if err != nil {
- return fmt.Errorf("json marshal error: %s", err.Error())
- }
- resp, err := http.Post(webhook, "application/json", bytes.NewBuffer(webhookData))
- if err != nil {
- return fmt.Errorf("http post error: %s", err.Error())
- }
- defer func() {
- _ = resp.Body.Close()
- }()
- respData, err := io.ReadAll(resp.Body)
- if err != nil {
- return fmt.Errorf("read response body error: %s", err.Error())
- }
- var respWebhook RespWebhookMsg
- err = json.Unmarshal(respData, &respWebhook)
- if err != nil {
- return fmt.Errorf("json unmarshal response body error: %s", err.Error())
- }
- if respWebhook.ErrCode != 0 {
- return fmt.Errorf("send message error [code: %d]: %s", respWebhook.ErrCode, respWebhook.ErrMsg)
- }
- return nil
- }
|