mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
86 lines
2.5 KiB
Go
86 lines
2.5 KiB
Go
package nodes
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/PandaXGO/PandaKit/httpclient"
|
|
"net/url"
|
|
"pandax/pkg/rule_engine/message"
|
|
"time"
|
|
)
|
|
|
|
type externalDingNode struct {
|
|
bareNode
|
|
WebHook string `json:"webHook" yaml:"webHook"`
|
|
Secret string `json:"secret"`
|
|
MsgType string `json:"msgType" yaml:"msgType"`
|
|
Content string `json:"content" yaml:"content"`
|
|
IsAtAll bool `json:"isAtAll" yaml:"isAtAll"`
|
|
AtMobiles []string `json:"atMobiles" yaml:"atMobiles"`
|
|
}
|
|
|
|
type externalDingNodeFactory struct{}
|
|
|
|
func (f externalDingNodeFactory) Name() string { return "DingNode" }
|
|
func (f externalDingNodeFactory) Category() string { return NODE_CATEGORY_EXTERNAL }
|
|
func (f externalDingNodeFactory) Labels() []string { return []string{"Success", "Failure"} }
|
|
func (f externalDingNodeFactory) Create(id string, meta Properties) (Node, error) {
|
|
node := &externalDingNode{
|
|
bareNode: newBareNode(f.Name(), id, meta, f.Labels()),
|
|
}
|
|
return decodePath(meta, node)
|
|
}
|
|
|
|
func (n *externalDingNode) Handle(msg *message.Message) error {
|
|
n.Debug(msg, message.DEBUGIN, "")
|
|
|
|
successLabelNode := n.GetLinkedNode("Success")
|
|
failureLabelNode := n.GetLinkedNode("Failure")
|
|
//获取消息
|
|
template, err := ParseTemplate(n.Content, msg.GetAllMap())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
sendData := map[string]interface{}{
|
|
"msgtype": "text",
|
|
"text": map[string]string{"content": template},
|
|
}
|
|
if n.IsAtAll {
|
|
sendData["at"] = map[string]interface{}{"isAtAll": n.IsAtAll}
|
|
} else {
|
|
sendData["at"] = map[string]interface{}{"atMobiles": n.AtMobiles}
|
|
}
|
|
marshal, _ := json.Marshal(sendData)
|
|
|
|
timestamp := time.Now().UnixMilli()
|
|
sign := getSign(timestamp, n.Secret)
|
|
|
|
url := fmt.Sprintf("%s×tamp=%d&sign=%s", n.WebHook, timestamp, sign)
|
|
|
|
postJson := httpclient.NewRequest(url).Header("Content-Type", "application/json").PostJson(string(marshal))
|
|
if postJson.StatusCode != 200 {
|
|
n.Debug(msg, message.DEBUGOUT, "钉钉机器人hook接口请求失败")
|
|
if failureLabelNode != nil {
|
|
return failureLabelNode.Handle(msg)
|
|
} else {
|
|
return err
|
|
}
|
|
}
|
|
if successLabelNode != nil {
|
|
n.Debug(msg, message.DEBUGOUT, "")
|
|
return successLabelNode.Handle(msg)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func getSign(timestamp int64, secret string) string {
|
|
stringToSign := fmt.Sprintf("%d\n%s", timestamp, secret)
|
|
hash := hmac.New(sha256.New, []byte(secret))
|
|
hash.Write([]byte(stringToSign))
|
|
signData := hash.Sum(nil)
|
|
return url.QueryEscape(base64.StdEncoding.EncodeToString(signData))
|
|
}
|