openzeppelin_monitor/models/core/
trigger.rs

1use crate::{
2	models::{core::ScriptLanguage, SecretValue},
3	utils::HttpRetryConfig,
4};
5use email_address::EmailAddress;
6use serde::{Deserialize, Serialize};
7
8/// Configuration for actions to take when monitored conditions are met.
9#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
10#[serde(deny_unknown_fields)]
11pub struct Trigger {
12	/// Unique name identifying this trigger
13	pub name: String,
14
15	/// Type of trigger (Email, Slack, Webhook, Telegram, Discord, Script)
16	pub trigger_type: TriggerType,
17
18	/// Configuration specific to the trigger type
19	pub config: TriggerTypeConfig,
20}
21
22/// Supported trigger action types
23#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
24#[serde(rename_all = "lowercase")]
25#[serde(deny_unknown_fields)]
26pub enum TriggerType {
27	/// Send notification to Slack
28	Slack,
29	/// Send notification to email
30	Email,
31	/// Make HTTP request to webhook
32	Webhook,
33	/// Send notification to Telegram
34	Telegram,
35	/// Send notification to Discord
36	Discord,
37	/// Execute local script
38	Script,
39}
40
41/// Notification message fields
42#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
43#[serde(deny_unknown_fields)]
44pub struct NotificationMessage {
45	/// Notification title or subject
46	pub title: String,
47	/// Message template
48	pub body: String,
49}
50
51/// Type-specific configuration for triggers
52#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
53#[serde(deny_unknown_fields)]
54#[serde(untagged)]
55pub enum TriggerTypeConfig {
56	/// Slack notification configuration
57	Slack {
58		/// Slack webhook URL
59		slack_url: SecretValue,
60		/// Notification message
61		message: NotificationMessage,
62		/// Retry policy for HTTP requests
63		#[serde(default)]
64		retry_policy: HttpRetryConfig,
65	},
66	/// Email notification configuration
67	Email {
68		/// SMTP host
69		host: String,
70		/// SMTP port (default 465)
71		port: Option<u16>,
72		/// SMTP username
73		username: SecretValue,
74		/// SMTP password
75		password: SecretValue,
76		/// Notification message
77		message: NotificationMessage,
78		/// Email sender
79		sender: EmailAddress,
80		/// Email recipients
81		recipients: Vec<EmailAddress>,
82	},
83	/// Webhook configuration
84	Webhook {
85		/// Webhook endpoint URL
86		url: SecretValue,
87		/// HTTP method to use
88		method: Option<String>,
89		/// Secret
90		secret: Option<SecretValue>,
91		/// Optional HTTP headers
92		headers: Option<std::collections::HashMap<String, String>>,
93		/// Notification message
94		message: NotificationMessage,
95		/// Retry policy for HTTP requests
96		#[serde(default)]
97		retry_policy: HttpRetryConfig,
98	},
99	/// Telegram notification configuration
100	Telegram {
101		/// Telegram bot token
102		token: SecretValue,
103		/// Telegram chat ID
104		chat_id: String,
105		/// Disable web preview
106		disable_web_preview: Option<bool>,
107		/// Notification message
108		message: NotificationMessage,
109		/// Retry policy for HTTP requests
110		#[serde(default)]
111		retry_policy: HttpRetryConfig,
112	},
113	/// Discord notification configuration
114	Discord {
115		/// Discord webhook URL
116		discord_url: SecretValue,
117		/// Notification message
118		message: NotificationMessage,
119		/// Retry policy for HTTP requests
120		#[serde(default)]
121		retry_policy: HttpRetryConfig,
122	},
123	/// Script execution configuration
124	Script {
125		/// Language of the script
126		language: ScriptLanguage,
127		/// Path to script file
128		script_path: String,
129		/// Command line arguments
130		#[serde(default)]
131		arguments: Option<Vec<String>>,
132		/// Timeout in milliseconds
133		timeout_ms: u32,
134	},
135}
136
137impl TriggerTypeConfig {
138	/// Get the retry policy for the trigger type, if applicable.
139	pub fn get_retry_policy(&self) -> Option<HttpRetryConfig> {
140		match self {
141			Self::Slack { retry_policy, .. } => Some(retry_policy.clone()),
142			Self::Discord { retry_policy, .. } => Some(retry_policy.clone()),
143			Self::Webhook { retry_policy, .. } => Some(retry_policy.clone()),
144			Self::Telegram { retry_policy, .. } => Some(retry_policy.clone()),
145			_ => None,
146		}
147	}
148}