规则链

This commit is contained in:
XM-GO
2023-04-18 16:29:26 +08:00
parent db5a45590a
commit 5dfa341083
46 changed files with 980 additions and 381 deletions

View File

@@ -1,6 +1,8 @@
package nodes
import (
"encoding/json"
"github.com/XM-GO/PandaKit/httpclient"
"github.com/sirupsen/logrus"
"pandax/pkg/rule_engine/message"
)
@@ -11,7 +13,7 @@ type externalWechatNode struct {
MsgType string `json:"msgType" yaml:"msgType"`
Content string `json:"content" yaml:"content"`
IsAtAll bool `json:"isAtAll" yaml:"isAtAll"`
atMobiles []string `json:"atMobiles" yaml:"atMobiles"`
AtMobiles []string `json:"atMobiles" yaml:"atMobiles"`
}
type externalWechatNodeFactory struct{}
@@ -29,5 +31,29 @@ func (f externalWechatNodeFactory) Create(id string, meta Metadata) (Node, error
func (n *externalWechatNode) Handle(msg message.Message) error {
logrus.Infof("%s handle message '%s'", n.Name(), msg.GetType())
successLabelNode := n.GetLinkedNode("Success")
failureLabelNode := n.GetLinkedNode("Failure")
template, err := ParseTemplate(n.Content, msg.GetAllMap())
sendData := map[string]interface{}{
"msgtype": "text",
"text": map[string]interface{}{"content": template},
}
if n.IsAtAll {
sendData["text"].(map[string]interface{})["mentioned_mobile_list"] = []string{"@all"}
} else {
sendData["text"].(map[string]interface{})["mentioned_mobile_list"] = n.AtMobiles
}
marshal, _ := json.Marshal(sendData)
postJson := httpclient.NewRequest(n.WebHook).Header("Content-Type", "application/json").PostJson(string(marshal))
if postJson.StatusCode != 200 {
if failureLabelNode != nil {
return failureLabelNode.Handle(msg)
} else {
return err
}
}
if successLabelNode != nil {
return successLabelNode.Handle(msg)
}
return nil
}