diff --git a/pkg/apis/notify/config.go b/pkg/apis/notify/config.go index 30fc7802a9..f8b7f66610 100644 --- a/pkg/apis/notify/config.go +++ b/pkg/apis/notify/config.go @@ -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 { diff --git a/pkg/apis/notify/robot.go b/pkg/apis/notify/robot.go index b80051b2cd..3e42d9cbdc 100644 --- a/pkg/apis/notify/robot.go +++ b/pkg/apis/notify/robot.go @@ -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 { diff --git a/pkg/mcclient/options/notify/robot.go b/pkg/mcclient/options/notify/robot.go index 5c9555e9b6..83c1979d2c 100644 --- a/pkg/mcclient/options/notify/robot.go +++ b/pkg/mcclient/options/notify/robot.go @@ -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 } diff --git a/pkg/notify/models/notification.go b/pkg/notify/models/notification.go index 50b4215230..cd597385c7 100644 --- a/pkg/notify/models/notification.go +++ b/pkg/notify/models/notification.go @@ -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(), diff --git a/pkg/notify/models/robot.go b/pkg/notify/models/robot.go index 7268ec5aec..5582d02b1d 100644 --- a/pkg/notify/models/robot.go +++ b/pkg/notify/models/robot.go @@ -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 } diff --git a/pkg/notify/tasks/notifications_send_task.go b/pkg/notify/tasks/notifications_send_task.go index 342e07dc4a..664adf2b70 100644 --- a/pkg/notify/tasks/notifications_send_task.go +++ b/pkg/notify/tasks/notifications_send_task.go @@ -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