lark_custom.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. AppID string `json:"app_id" title:"App ID"`
  11. AppSecret string `json:"app_secret" title:"App Secret"`
  12. OpenID string `json:"open_id" title:"Open ID"`
  13. UserID string `json:"user_id" title:"User ID"`
  14. UnionID string `json:"union_id" title:"Union ID"`
  15. Email string `json:"email" title:"Email"`
  16. ChatID string `json:"chat_id" title:"Chat ID"`
  17. }
  18. func init() {
  19. RegisterExternalNotifier("lark_custom", func(ctx context.Context, n *model.ExternalNotify, msg *ExternalMessage) error {
  20. larkCustomConfig := &LarkCustom{}
  21. err := map2struct.WeakDecode(n.Config, larkCustomConfig)
  22. if err != nil {
  23. return err
  24. }
  25. if larkCustomConfig.AppID == "" || larkCustomConfig.AppSecret == "" {
  26. return ErrInvalidNotifierConfig
  27. }
  28. larkCustomAppService := lark.NewCustomAppService(larkCustomConfig.AppID, larkCustomConfig.AppSecret)
  29. larkCustomAppService.AddReceivers(
  30. lark.OpenID(larkCustomConfig.OpenID),
  31. lark.UserID(larkCustomConfig.UserID),
  32. lark.UnionID(larkCustomConfig.UnionID),
  33. lark.Email(larkCustomConfig.Email),
  34. lark.ChatID(larkCustomConfig.ChatID),
  35. )
  36. return larkCustomAppService.Send(ctx, msg.GetTitle(n.Language), msg.GetContent(n.Language))
  37. })
  38. }