diff --git a/pkg/apis/notify/config.go b/pkg/apis/notify/config.go index 0b2ddacd30..a56969db5b 100644 --- a/pkg/apis/notify/config.go +++ b/pkg/apis/notify/config.go @@ -149,6 +149,9 @@ type SendParams struct { Event string Receivers SNotifyReceiver EmailMsg *SEmailMessage + Header jsonutils.JSONObject + Body jsonutils.JSONObject + MsgKey string DomainId string RemoteTemplateParam SRemoteTemplateParam } diff --git a/pkg/apis/notify/robot.go b/pkg/apis/notify/robot.go index e3cc0dcf1f..b80051b2cd 100644 --- a/pkg/apis/notify/robot.go +++ b/pkg/apis/notify/robot.go @@ -14,7 +14,11 @@ package notify -import "yunion.io/x/onecloud/pkg/apis" +import ( + "yunion.io/x/jsonutils" + + "yunion.io/x/onecloud/pkg/apis" +) type RobotCreateInput struct { apis.SharableVirtualResourceCreateInput @@ -28,7 +32,10 @@ type RobotCreateInput struct { Address string `json:"address"` // description: Language preference // example: zh_CN - Lang string `json:"lang"` + Lang string `json:"lang"` + Header jsonutils.JSONObject `json:"header"` + Body jsonutils.JSONObject `json:"body"` + MsgKey string `json:"msg_key"` } type RobotDetails struct { @@ -54,5 +61,8 @@ type RobotUpdateInput struct { Address string `json:"address"` // description: Language preference // example: en - Lang string `json:"lang"` + Lang string `json:"lang"` + Header jsonutils.JSONObject `json:"header"` + Body jsonutils.JSONObject `json:"body"` + MsgKey string `json:"msg_key"` } diff --git a/pkg/mcclient/options/notify/robot.go b/pkg/mcclient/options/notify/robot.go index 554075d1de..5043db9c74 100644 --- a/pkg/mcclient/options/notify/robot.go +++ b/pkg/mcclient/options/notify/robot.go @@ -16,6 +16,7 @@ package notify import ( "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" "yunion.io/x/onecloud/pkg/mcclient/options" ) @@ -36,10 +37,25 @@ type RobotCreateOptions struct { Type string `choices:"feishu|dingtalk|workwx|webhook"` Address string Lang string + Header string + Body string + MsgKey string } func (rc *RobotCreateOptions) Params() (jsonutils.JSONObject, error) { - return jsonutils.Marshal(rc), nil + dict := jsonutils.NewDict() + jsonutils.Update(&dict, rc) + header, err := jsonutils.Parse([]byte(rc.Header)) + if err != nil { + return nil, errors.Wrap(err, "parse header") + } + dict.Set("header", header) + body, err := jsonutils.Parse([]byte(rc.Body)) + if err != nil { + return nil, errors.Wrap(err, "parse body") + } + dict.Set("body", body) + return dict, nil } type RobotOptions struct { @@ -62,8 +78,23 @@ type RobotUpdateOptions struct { type SrobotUpdateOptions struct { Address string Lang string + Header string + Body string + MsgKey string } func (ru *RobotUpdateOptions) Params() (jsonutils.JSONObject, error) { - return jsonutils.Marshal(ru.SrobotUpdateOptions), nil + dict := jsonutils.NewDict() + jsonutils.Update(&dict, ru) + header, err := jsonutils.Parse([]byte(ru.Header)) + if err != nil { + return nil, errors.Wrap(err, "parse header") + } + dict.Set("header", header) + body, err := jsonutils.Parse([]byte(ru.Body)) + if err != nil { + return nil, errors.Wrap(err, "parse body") + } + dict.Set("body", body) + return dict, nil } diff --git a/pkg/notify/models/robot.go b/pkg/notify/models/robot.go index 5950af996e..9bef262f06 100644 --- a/pkg/notify/models/robot.go +++ b/pkg/notify/models/robot.go @@ -59,9 +59,12 @@ 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"` + 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"` } var RobotList = []string{api.FEISHU_ROBOT, api.DINGTALK_ROBOT, api.WORKWX_ROBOT, api.WEBHOOK, api.WEBHOOK_ROBOT} @@ -93,6 +96,9 @@ func (rm *SRobotManager) ValidateCreateData(ctx context.Context, userCred mcclie Contact: input.Address, DomainId: input.ProjectDomainId, }, + Header: input.Header, + Body: input.Body, + MsgKey: input.MsgKey, Title: "Validate", Message: "This is a verification message, please ignore.", }) diff --git a/pkg/notify/sender/webhook.go b/pkg/notify/sender/webhook.go index c28939a343..c1f65ed6a6 100644 --- a/pkg/notify/sender/webhook.go +++ b/pkg/notify/sender/webhook.go @@ -19,7 +19,7 @@ import ( "yunion.io/x/cloudmux/pkg/cloudprovider" "yunion.io/x/jsonutils" - "yunion.io/x/log" + "yunion.io/x/pkg/errors" "yunion.io/x/pkg/util/httputils" api "yunion.io/x/onecloud/pkg/apis/notify" @@ -35,20 +35,37 @@ func (self *SWebhookSender) GetSenderType() string { } func (self *SWebhookSender) Send(args api.SendParams) error { - log.Infoln("this is in webhookSend ") body, err := jsonutils.ParseString(args.Message) if err != nil { - log.Errorf("unable to parse %q: %v", args.Message, err) + return errors.Wrapf(err, "unable to parse %q", args.Message) } if _, ok := body.(*jsonutils.JSONString); err != nil || ok { dict := jsonutils.NewDict() - dict.Set("Msg", jsonutils.NewString(args.Message)) + if len(args.MsgKey) == 0 { + dict.Set("Msg", jsonutils.NewString(args.Message)) + } else { + dict.Set(args.MsgKey, jsonutils.NewString(args.Message)) + } + if args.Body != nil { + jsonutils.Update(dict, args.Body) + } body = dict } event := strings.ToUpper(args.Event) header := http.Header{} + if args.Header != nil { + resmap, _ := args.Header.GetMap() + for k, v := range resmap { + vStr, err := v.GetString() + if err != nil { + continue + } + header.Set(k, vStr) + } + } + header.Set(EVENT_HEADER, event) - _, _, err = httputils.JSONRequest(cli, ctx, httputils.POST, args.Receivers.Contact, header, body, true) + _, _, err = httputils.JSONRequest(cli, ctx, httputils.POST, args.Receivers.Contact, header, body, false) return err } diff --git a/pkg/notify/tasks/notifications_send_task.go b/pkg/notify/tasks/notifications_send_task.go index 28bdb4aae2..0b4fb08f8c 100644 --- a/pkg/notify/tasks/notifications_send_task.go +++ b/pkg/notify/tasks/notifications_send_task.go @@ -271,6 +271,9 @@ func (notificationSendTask *NotificationSendTask) batchSend(ctx context.Context, robot := receivers[i].receiver.(*models.SRobot) driver := models.GetDriver(fmt.Sprintf("%s-robot", robot.Type)) params.Receivers.Contact = robot.Address + params.Header = robot.Header + params.Body = robot.Body + params.MsgKey = robot.MsgKey err = driver.Send(params) if err != nil { fails = append(fails, FailedReceiverSpec{ReceiverSpec: receivers[i], Reason: err.Error()})