webhook_target.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package model
  2. import (
  3. "time"
  4. "github.com/uptrace/bun"
  5. )
  6. // WebhookTestResult Webhook 测试结果
  7. type WebhookTestResult struct {
  8. Success bool `json:"success"`
  9. Message *string `json:"message,omitempty"`
  10. Timestamp string `json:"timestamp,omitempty"`
  11. }
  12. // WebhookTarget Webhook 目标模型
  13. type WebhookTarget struct {
  14. bun.BaseModel `bun:"table:webhook_targets,alias:wt"`
  15. ID int `bun:"id,pk,autoincrement" json:"id"`
  16. Name string `bun:"name,notnull" json:"name"`
  17. ProviderType string `bun:"provider_type,notnull" json:"providerType"` // wechat, feishu, dingtalk, telegram, custom
  18. // 通用配置
  19. WebhookUrl *string `bun:"webhook_url" json:"webhookUrl"`
  20. // Telegram 特有配置
  21. TelegramBotToken *string `bun:"telegram_bot_token" json:"telegramBotToken"`
  22. TelegramChatId *string `bun:"telegram_chat_id" json:"telegramChatId"`
  23. // 钉钉签名配置
  24. DingtalkSecret *string `bun:"dingtalk_secret" json:"dingtalkSecret"`
  25. // 自定义 Webhook 配置
  26. CustomTemplate map[string]interface{} `bun:"custom_template,type:jsonb" json:"customTemplate"`
  27. CustomHeaders map[string]interface{} `bun:"custom_headers,type:jsonb" json:"customHeaders"`
  28. // 代理配置
  29. ProxyUrl *string `bun:"proxy_url" json:"proxyUrl"`
  30. ProxyFallbackToDirect bool `bun:"proxy_fallback_to_direct,default:false" json:"proxyFallbackToDirect"`
  31. // 元数据
  32. IsEnabled bool `bun:"is_enabled,notnull,default:true" json:"isEnabled"`
  33. LastTestAt *time.Time `bun:"last_test_at" json:"lastTestAt"`
  34. LastTestResult *WebhookTestResult `bun:"last_test_result,type:jsonb" json:"lastTestResult"`
  35. CreatedAt time.Time `bun:"created_at,notnull,default:current_timestamp" json:"createdAt"`
  36. UpdatedAt time.Time `bun:"updated_at,notnull,default:current_timestamp" json:"updatedAt"`
  37. // 关联
  38. Bindings []NotificationTargetBinding `bun:"rel:has-many,join:id=target_id" json:"bindings,omitempty"`
  39. }
  40. // IsActive 检查 Webhook 目标是否处于活跃状态
  41. func (w *WebhookTarget) IsActive() bool {
  42. return w.IsEnabled
  43. }
  44. // IsTelegram 检查是否为 Telegram 类型
  45. func (w *WebhookTarget) IsTelegram() bool {
  46. return w.ProviderType == string(WebhookProviderTypeTelegram)
  47. }
  48. // IsCustom 检查是否为自定义类型
  49. func (w *WebhookTarget) IsCustom() bool {
  50. return w.ProviderType == string(WebhookProviderTypeCustom)
  51. }