dingtalk_robot.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package sender
  15. import (
  16. "context"
  17. "crypto/hmac"
  18. "crypto/sha256"
  19. "encoding/base64"
  20. "fmt"
  21. "net/url"
  22. "strings"
  23. "time"
  24. "yunion.io/x/cloudmux/pkg/cloudprovider"
  25. "yunion.io/x/jsonutils"
  26. "yunion.io/x/pkg/errors"
  27. "yunion.io/x/pkg/util/httputils"
  28. api "yunion.io/x/onecloud/pkg/apis/notify"
  29. "yunion.io/x/onecloud/pkg/httperrors"
  30. "yunion.io/x/onecloud/pkg/notify/models"
  31. )
  32. type SDingTalkRobotSender struct {
  33. config map[string]api.SNotifyConfigContent
  34. }
  35. func (dingRobotSender *SDingTalkRobotSender) GetSenderType() string {
  36. return api.DINGTALK_ROBOT
  37. }
  38. func (dingRobotSender *SDingTalkRobotSender) Send(ctx context.Context, args api.SendParams) error {
  39. var atStr strings.Builder
  40. title, msg := args.Title, args.Message
  41. urlAddr, err := url.Parse(args.Receivers.Contact)
  42. if err != nil {
  43. return errors.Wrapf(err, "invalid url %s", args.Receivers.Contact)
  44. }
  45. query := urlAddr.Query()
  46. token := query.Get("access_token")
  47. if len(token) == 0 {
  48. return httperrors.NewMissingParameterError("access_token")
  49. }
  50. sign := query.Get("sign")
  51. if len(sign) > 0 {
  52. timestamp := time.Now().UnixNano() / 1e6
  53. stringToSign := fmt.Sprintf("%d\n%s", timestamp, sign)
  54. h := hmac.New(sha256.New, []byte(sign))
  55. h.Write([]byte(stringToSign))
  56. signature := h.Sum(nil)
  57. encodedSignature := base64.StdEncoding.EncodeToString(signature)
  58. sign = url.QueryEscape(encodedSignature)
  59. query.Set("timestamp", fmt.Sprintf("%d", timestamp))
  60. query.Set("sign", sign)
  61. }
  62. urlAddr.RawQuery = query.Encode()
  63. processText := fmt.Sprintf("### %s\n%s%s", title, msg, atStr.String())
  64. request := map[string]interface{}{
  65. "msgtype": "markdown",
  66. "markdown": map[string]interface{}{
  67. "title": title,
  68. "text": processText,
  69. },
  70. }
  71. _, resp, err := httputils.JSONRequest(nil, ctx, httputils.POST, urlAddr.String(), nil, jsonutils.Marshal(request), true)
  72. if err != nil {
  73. return err
  74. }
  75. ret := struct {
  76. Errcode int
  77. Errmsg string
  78. }{}
  79. err = resp.Unmarshal(&ret)
  80. if err != nil {
  81. return errors.Wrapf(err, "Unmarshal")
  82. }
  83. if ret.Errcode == 310000 {
  84. if strings.Contains(ret.Errmsg, "whitelist") {
  85. return errors.Wrap(ErrIPWhiteList, ret.Errmsg)
  86. } else {
  87. return errors.Errorf("%s", resp.String())
  88. }
  89. }
  90. if ret.Errcode == 300001 && strings.Contains(ret.Errmsg, "token") {
  91. return ErrNoSuchWebhook
  92. }
  93. return nil
  94. }
  95. func (dingRobotSender *SDingTalkRobotSender) ValidateConfig(ctx context.Context, config api.NotifyConfig) (string, error) {
  96. return "", cloudprovider.ErrNotImplemented
  97. }
  98. func (dingRobotSender *SDingTalkRobotSender) ContactByMobile(ctx context.Context, mobile, domainId string) (string, error) {
  99. return "", cloudprovider.ErrNotImplemented
  100. }
  101. func (dingRobotSender *SDingTalkRobotSender) IsPersonal() bool {
  102. return true
  103. }
  104. func (dingRobotSender *SDingTalkRobotSender) IsRobot() bool {
  105. return true
  106. }
  107. func (dingRobotSender *SDingTalkRobotSender) IsValid() bool {
  108. return len(dingRobotSender.config) > 0
  109. }
  110. func (dingRobotSender *SDingTalkRobotSender) IsPullType() bool {
  111. return true
  112. }
  113. func (dingRobotSender *SDingTalkRobotSender) IsSystemConfigContactType() bool {
  114. return true
  115. }
  116. func (dingRobotSender *SDingTalkRobotSender) GetAccessToken(ctx context.Context, key string) error {
  117. return nil
  118. }
  119. func (dingRobotSender *SDingTalkRobotSender) RegisterConfig(config models.SConfig) {
  120. }
  121. func init() {
  122. models.Register(&SDingTalkRobotSender{
  123. config: map[string]api.SNotifyConfigContent{},
  124. })
  125. }