feishu_robot.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "fmt"
  18. "strings"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/pkg/errors"
  21. api "yunion.io/x/onecloud/pkg/apis/notify"
  22. "yunion.io/x/onecloud/pkg/monitor/notifydrivers/feishu"
  23. "yunion.io/x/onecloud/pkg/notify/models"
  24. )
  25. var ErrNoSuchWebhook = errors.Error("No such webhook")
  26. var InvalidWebhook = errors.Error("Invalid webhook")
  27. type SFeishuRobotSender struct {
  28. config map[string]api.SNotifyConfigContent
  29. }
  30. func (feishuRobotSender *SFeishuRobotSender) GetSenderType() string {
  31. return api.FEISHU_ROBOT
  32. }
  33. func (feishuRobotSender *SFeishuRobotSender) Send(ctx context.Context, args api.SendParams) error {
  34. var token string
  35. var errs []error
  36. title, msg := args.Title, args.Message
  37. webhook := args.Receivers.Contact
  38. switch {
  39. case strings.HasPrefix(webhook, ApiWebhookRobotV2SendMessage):
  40. token = webhook[len(ApiWebhookRobotV2SendMessage):]
  41. case strings.HasPrefix(webhook, feishu.ApiWebhookRobotSendMessage):
  42. token = webhook[len(feishu.ApiWebhookRobotSendMessage):]
  43. default:
  44. return errors.Wrap(InvalidWebhook, webhook)
  45. }
  46. req := feishu.WebhookRobotMsgReq{
  47. Title: title,
  48. Text: msg,
  49. }
  50. rep, err := feishu.SendWebhookRobotMessage(token, req)
  51. if err != nil {
  52. return errors.Wrap(err, "SendWebhookRobotMessage")
  53. }
  54. if !rep.Ok {
  55. if strings.Contains(rep.Error, "token") {
  56. return ErrNoSuchWebhook
  57. } else {
  58. return fmt.Errorf("SendWebhookRobotMessage failed: %s", rep.Error)
  59. }
  60. }
  61. if err != nil {
  62. if errs == nil {
  63. errs = []error{}
  64. }
  65. errs = append(errs, err)
  66. }
  67. return errors.NewAggregate(errs)
  68. }
  69. func (feishuRobotSender *SFeishuRobotSender) ValidateConfig(ctx context.Context, config api.NotifyConfig) (string, error) {
  70. return "", cloudprovider.ErrNotImplemented
  71. }
  72. func (feishuRobotSender *SFeishuRobotSender) ContactByMobile(ctx context.Context, mobile, domainId string) (string, error) {
  73. return "", cloudprovider.ErrNotImplemented
  74. }
  75. func (feishuRobotSender *SFeishuRobotSender) IsPersonal() bool {
  76. return true
  77. }
  78. func (feishuRobotSender *SFeishuRobotSender) IsRobot() bool {
  79. return true
  80. }
  81. func (feishuRobotSender *SFeishuRobotSender) IsValid() bool {
  82. return len(feishuRobotSender.config) > 0
  83. }
  84. func (feishuRobotSender *SFeishuRobotSender) IsPullType() bool {
  85. return true
  86. }
  87. func (feishuRobotSender *SFeishuRobotSender) IsSystemConfigContactType() bool {
  88. return true
  89. }
  90. func (feishuRobotSender *SFeishuRobotSender) GetAccessToken(ctx context.Context, key string) error {
  91. return nil
  92. }
  93. func (feishuRobotSender *SFeishuRobotSender) RegisterConfig(config models.SConfig) {
  94. }
  95. func init() {
  96. models.Register(&SFeishuRobotSender{
  97. config: map[string]api.SNotifyConfigContent{},
  98. })
  99. }