Files
waf-platform/EdgeAPI/internal/senders/mediasenders/media_dingtalk.go
2026-02-04 20:27:13 +08:00

73 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//go:build plus
package mediasenders
import (
"bytes"
"errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/utils/string"
"io"
"net/http"
"strings"
"time"
)
// DingTalkMedia 钉钉群机器人媒介
type DingTalkMedia struct {
WebHookURL string `yaml:"webHookURL" json:"webHookURL"`
}
// 获取新对象
func NewDingTalkMedia() *DingTalkMedia {
return &DingTalkMedia{}
}
func (this *DingTalkMedia) Send(user string, subject string, body string, productName string, datetime string) (resp []byte, err error) {
if len(this.WebHookURL) == 0 {
return nil, errors.New("webHook url should not be empty")
}
content := maps.Map{
"msgtype": "text",
"text": maps.Map{
"content": "标题:" + subject + "\n内容" + body,
},
}
if len(user) > 0 {
mobiles := []string{}
for _, u := range strings.Split(user, ",") {
u = strings.TrimSpace(u)
if len(u) > 0 {
mobiles = append(mobiles, u)
}
}
content["at"] = maps.Map{
"atMobiles": mobiles,
}
}
reader := bytes.NewBufferString(stringutil.JSONEncode(content))
req, err := http.NewRequest(http.MethodPost, this.WebHookURL, reader)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
client := utils.SharedHttpClient(5 * time.Second)
response, err := client.Do(req)
if err != nil {
return nil, err
}
defer response.Body.Close()
resp, err = io.ReadAll(response.Body)
return
}
// RequireUser 是否需要用户标识
func (this *DingTalkMedia) RequireUser() bool {
return false
}