fix(notify): add option to use template for webhook

This commit is contained in:
马鸿飞
2023-08-08 17:37:51 +08:00
parent 3ccd834770
commit 16a9b53621
6 changed files with 68 additions and 45 deletions
+6 -5
View File
@@ -98,11 +98,12 @@ type ConfigManagerGetTypesOutput struct {
}
type SsNotification struct {
ContactType string
Topic string
Message string
Event SNotifyEvent
AdvanceDays int
ContactType string
Topic string
Message string
Event SNotifyEvent
AdvanceDays int
RobotUseTemplate bool
}
type SBatchSendParams struct {
+6 -4
View File
@@ -16,6 +16,7 @@ package notify
import (
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/tristate"
"yunion.io/x/onecloud/pkg/apis"
)
@@ -32,10 +33,11 @@ type RobotCreateInput struct {
Address string `json:"address"`
// description: Language preference
// example: zh_CN
Lang string `json:"lang"`
Header jsonutils.JSONObject `json:"header"`
Body jsonutils.JSONObject `json:"body"`
MsgKey string `json:"msg_key"`
Lang string `json:"lang"`
Header jsonutils.JSONObject `json:"header"`
Body jsonutils.JSONObject `json:"body"`
MsgKey string `json:"msg_key"`
UseTemplate tristate.TriState `json:"use_template"`
}
type RobotDetails struct {
+35 -20
View File
@@ -17,6 +17,7 @@ package notify
import (
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/tristate"
"yunion.io/x/onecloud/pkg/mcclient/options"
)
@@ -33,28 +34,36 @@ func (rl *RobotListOptions) Params() (jsonutils.JSONObject, error) {
}
type RobotCreateOptions struct {
NAME string
Type string `choices:"feishu|dingtalk|workwx|webhook"`
Address string
Lang string
Header string
Body string
MsgKey string
NAME string
Type string `choices:"feishu|dingtalk|workwx|webhook"`
Address string
Lang string
Header string
Body string
MsgKey string
UseTemplate bool `help:"just for webhook"`
}
func (rc *RobotCreateOptions) Params() (jsonutils.JSONObject, error) {
dict := jsonutils.NewDict()
jsonutils.Update(&dict, rc)
header, err := jsonutils.Parse([]byte(rc.Header))
if err != nil {
return nil, errors.Wrap(err, "parse header")
if len(rc.Header) > 0 {
header, err := jsonutils.Parse([]byte(rc.Header))
if err != nil {
return nil, errors.Wrap(err, "parse header")
}
dict.Set("header", header)
}
dict.Set("header", header)
body, err := jsonutils.Parse([]byte(rc.Body))
if err != nil {
return nil, errors.Wrap(err, "parse body")
if len(rc.Body) > 0 {
body, err := jsonutils.Parse([]byte(rc.Body))
if err != nil {
return nil, errors.Wrap(err, "parse body")
}
dict.Set("body", body)
}
dict.Set("body", body)
dict.Set("use_template", jsonutils.Marshal(rc.UseTemplate))
return dict, nil
}
@@ -76,11 +85,12 @@ type RobotUpdateOptions struct {
}
type SrobotUpdateOptions struct {
Address string
Lang string
Header *string
Body *string
MsgKey string
Address string
Lang string
Header *string
Body *string
MsgKey string
UseTemplate tristate.TriState
}
func (ru *RobotUpdateOptions) Params() (jsonutils.JSONObject, error) {
@@ -100,5 +110,10 @@ func (ru *RobotUpdateOptions) Params() (jsonutils.JSONObject, error) {
}
dict.Set("body", body)
}
if ru.UseTemplate.IsFalse() {
dict.Set("use_template", jsonutils.JSONFalse)
} else if ru.UseTemplate.IsTrue() {
dict.Set("use_template", jsonutils.JSONTrue)
}
return dict, nil
}
+10 -8
View File
@@ -532,7 +532,7 @@ func (n *SNotification) receiveDetails(userCred mcclient.TokenCredential, scope
func (n *SNotification) getMoreDetails(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, out api.NotificationDetails) (api.NotificationDetails, error) {
// get title adn content
lang := getLangSuffix(ctx)
nn, err := n.Notification()
nn, err := n.Notification(false)
if err != nil {
return out, err
}
@@ -552,7 +552,7 @@ func (n *SNotification) getMoreDetails(ctx context.Context, userCred mcclient.To
return out, nil
}
func (n *SNotification) Notification() (api.SsNotification, error) {
func (n *SNotification) Notification(robotUseTemplate bool) (api.SsNotification, error) {
if n.EventId == "" {
return api.SsNotification{
ContactType: n.ContactType,
@@ -566,11 +566,12 @@ func (n *SNotification) Notification() (api.SsNotification, error) {
}
e, _ := parseEvent(event.Event)
return api.SsNotification{
ContactType: n.ContactType,
Topic: n.Topic,
Message: event.Message,
Event: e,
AdvanceDays: event.AdvanceDays,
ContactType: n.ContactType,
Topic: n.Topic,
Message: event.Message,
Event: e,
AdvanceDays: event.AdvanceDays,
RobotUseTemplate: robotUseTemplate,
}, nil
}
@@ -727,7 +728,8 @@ func (n *SNotification) GetTemplate(ctx context.Context, topicId, lang string, n
webhookMsg.Set("action", jsonutils.NewString(aStr))
webhookMsg.Set("result", jsonutils.NewString(resultStr))
webhookMsg.Set("resource_details", msg)
if no.ContactType == api.WEBHOOK || no.ContactType == api.WEBHOOK_ROBOT {
if (no.ContactType == api.WEBHOOK || no.ContactType == api.WEBHOOK_ROBOT) && !no.RobotUseTemplate {
return api.SendParams{
Title: no.Event.StringWithDeli("_"),
Message: webhookMsg.String(),
+8 -6
View File
@@ -60,12 +60,13 @@ type SRobot struct {
db.SSharableVirtualResourceBase
db.SEnabledResourceBase
Type string `width:"16" nullable:"false" create:"required" get:"user" list:"user" index:"true"`
Address string `nullable:"false" create:"required" update:"user" get:"user" list:"user"`
Lang string `width:"16" nullable:"false" create:"required" update:"user" get:"user" list:"user"`
Header jsonutils.JSONObject `length:"long" charset:"utf8" nullable:"true" list:"user" create:"optional" update:"user"`
Body jsonutils.JSONObject `length:"long" charset:"utf8" nullable:"true" list:"user" create:"optional" update:"user"`
MsgKey string `width:"16" nullable:"true" update:"user" get:"user" list:"user"`
Type string `width:"16" nullable:"false" create:"required" get:"user" list:"user" index:"true"`
Address string `nullable:"false" create:"required" update:"user" get:"user" list:"user"`
Lang string `width:"16" nullable:"false" create:"required" update:"user" get:"user" list:"user"`
Header jsonutils.JSONObject `length:"long" charset:"utf8" nullable:"true" list:"user" create:"optional" update:"user"`
Body jsonutils.JSONObject `length:"long" charset:"utf8" nullable:"true" list:"user" create:"optional" update:"user"`
MsgKey string `width:"16" nullable:"true" update:"user" get:"user" list:"user"`
UseTemplate tristate.TriState `default:"false" list:"domain" update:"user" create:"admin_optional"`
}
var RobotList = []string{api.FEISHU_ROBOT, api.DINGTALK_ROBOT, api.WORKWX_ROBOT, api.WEBHOOK, api.WEBHOOK_ROBOT}
@@ -109,6 +110,7 @@ func (rm *SRobotManager) ValidateCreateData(ctx context.Context, userCred mcclie
}
return input, errors.Wrap(err, "robot validate")
}
return input, nil
}
+3 -2
View File
@@ -100,7 +100,7 @@ func (self *NotificationSendTask) OnInit(ctx context.Context, obj db.IStandalone
rn.AfterSend(ctx, false, reason)
failedRecord = append(failedRecord, fmt.Sprintf("%s: %s", rn.ReceiverID, reason))
}
robotUseTemplate := false
for i := range rns {
receiver, err := rns[i].Receiver()
if err != nil {
@@ -119,6 +119,7 @@ func (self *NotificationSendTask) OnInit(ctx context.Context, obj db.IStandalone
if receiver.IsRobot() {
robot := receiver.(*models.SRobot)
notification.ContactType = fmt.Sprintf("%s-robot", robot.Type)
robotUseTemplate = robot.UseTemplate.Bool()
}
enabled, err := receiver.IsEnabledContactType(notification.ContactType)
if err != nil {
@@ -166,7 +167,7 @@ func (self *NotificationSendTask) OnInit(ctx context.Context, obj db.IStandalone
}
}
nn, err := notification.Notification()
nn, err := notification.Notification(robotUseTemplate)
if err != nil {
self.taskFailed(ctx, notification, errors.Wrapf(err, "Notification").Error(), true)
return