workwx_robot.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/util/httputils"
  22. api "yunion.io/x/onecloud/pkg/apis/notify"
  23. "yunion.io/x/onecloud/pkg/notify/models"
  24. )
  25. type SWorkwxRobotSender struct {
  26. config map[string]api.SNotifyConfigContent
  27. }
  28. func (workwxRobotSender *SWorkwxRobotSender) GetSenderType() string {
  29. return api.WORKWX_ROBOT
  30. }
  31. func (workwxRobotSender *SWorkwxRobotSender) Send(ctx context.Context, args api.SendParams) error {
  32. errs := []error{}
  33. content := fmt.Sprintf("# %s\n\n%s", args.Title, args.Message)
  34. mid := map[string]interface{}{
  35. "msgtype": "markdown",
  36. "markdown": map[string]interface{}{
  37. "content": content,
  38. },
  39. }
  40. req, err := sendRequest(ctx, args.Receivers.Contact, httputils.POST, nil, nil, jsonutils.Marshal(mid))
  41. if err != nil {
  42. return errors.Wrap(err, "sendRequest")
  43. }
  44. errCode, err := req.GetString("errcode")
  45. if err != nil {
  46. return errors.Wrap(err, "req.GetString")
  47. }
  48. if errCode != "0" {
  49. errs = append(errs, errors.Errorf("%s", req.PrettyString()))
  50. }
  51. return errors.NewAggregate(errs)
  52. }
  53. func (workwxRobotSender *SWorkwxRobotSender) ValidateConfig(ctx context.Context, config api.NotifyConfig) (string, error) {
  54. return "", cloudprovider.ErrNotImplemented
  55. }
  56. func (workwxRobotSender *SWorkwxRobotSender) ContactByMobile(ctx context.Context, mobile, domainId string) (string, error) {
  57. return "", cloudprovider.ErrNotImplemented
  58. }
  59. func (workwxRobotSender *SWorkwxRobotSender) IsPersonal() bool {
  60. return true
  61. }
  62. func (workwxRobotSender *SWorkwxRobotSender) IsRobot() bool {
  63. return true
  64. }
  65. func (workwxRobotSender *SWorkwxRobotSender) IsValid() bool {
  66. return len(workwxRobotSender.config) > 0
  67. }
  68. func (workwxRobotSender *SWorkwxRobotSender) IsPullType() bool {
  69. return true
  70. }
  71. func (workwxRobotSender *SWorkwxRobotSender) IsSystemConfigContactType() bool {
  72. return true
  73. }
  74. func (workwxRobotSender *SWorkwxRobotSender) GetAccessToken(ctx context.Context, key string) error {
  75. return nil
  76. }
  77. func (workwxRobotSender *SWorkwxRobotSender) RegisterConfig(config models.SConfig) {
  78. }
  79. func init() {
  80. models.Register(&SWorkwxRobotSender{
  81. config: map[string]api.SNotifyConfigContent{},
  82. })
  83. }