lark_custom.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package notification
  2. import (
  3. "context"
  4. "github.com/0xJacky/Nginx-UI/model"
  5. "github.com/nikoksr/notify/service/lark"
  6. "github.com/uozi-tech/cosy/map2struct"
  7. )
  8. // @external_notifier(Lark Custom)
  9. type LarkCustom struct {
  10. Domain string `json:"domain" title:"Domain"`
  11. AppID string `json:"app_id" title:"App ID"`
  12. AppSecret string `json:"app_secret" title:"App Secret"`
  13. OpenID string `json:"open_id" title:"Open ID"`
  14. UserID string `json:"user_id" title:"User ID"`
  15. UnionID string `json:"union_id" title:"Union ID"`
  16. Email string `json:"email" title:"Email"`
  17. ChatID string `json:"chat_id" title:"Chat ID"`
  18. }
  19. func init() {
  20. RegisterExternalNotifier("lark_custom", func(ctx context.Context, n *model.ExternalNotify, msg *ExternalMessage) error {
  21. larkCustomConfig := &LarkCustom{}
  22. err := map2struct.WeakDecode(n.Config, larkCustomConfig)
  23. if err != nil {
  24. return err
  25. }
  26. if larkCustomConfig.AppID == "" || larkCustomConfig.AppSecret == "" {
  27. return ErrInvalidNotifierConfig
  28. }
  29. larkCustomAppService := lark.NewCustomAppService(larkCustomConfig.AppID, larkCustomConfig.AppSecret)
  30. larkCustomAppService.AddReceivers(
  31. lark.OpenID(larkCustomConfig.OpenID),
  32. lark.UserID(larkCustomConfig.UserID),
  33. lark.UnionID(larkCustomConfig.UnionID),
  34. lark.Email(larkCustomConfig.Email),
  35. lark.ChatID(larkCustomConfig.ChatID),
  36. )
  37. if larkCustomConfig.Domain != "" {
  38. larkCustomAppService.AddReceivers(
  39. lark.Domain(larkCustomConfig.Domain),
  40. )
  41. }
  42. return larkCustomAppService.Send(ctx, msg.GetTitle(n.Language), msg.GetContent(n.Language))
  43. })
  44. }