|
| 1 | +use anyhow::Context; |
| 2 | +use async_trait::async_trait; |
| 3 | +use telegrama_rs::{ClientOptions, FormattingOptions, Response, Telegrama}; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + alerts::AlertTrait, |
| 7 | + shared::{ |
| 8 | + alert::{Alert, Severity}, |
| 9 | + block_explorer::BlockExplorer, |
| 10 | + config::TelegramAlertConfig, |
| 11 | + }, |
| 12 | +}; |
| 13 | + |
| 14 | +pub struct TelegramAlert { |
| 15 | + pub block_explorer: BlockExplorer, |
| 16 | + pub telegram_token: String, |
| 17 | + pub telegram_chat_id: String, |
| 18 | + pub network_id: String, |
| 19 | + pub minimum_severity: Option<Severity>, |
| 20 | +} |
| 21 | + |
| 22 | +#[async_trait] |
| 23 | +impl AlertTrait for TelegramAlert { |
| 24 | + async fn send_alerts(&self, alert: Alert) -> Result<Option<String>, String> { |
| 25 | + if alert.severity < self.minimum_severity.clone().unwrap_or(Severity::Low) { |
| 26 | + return Ok(None); |
| 27 | + } |
| 28 | + |
| 29 | + let block_explorer = self.block_explorer.clone(); |
| 30 | + |
| 31 | + let title = alert.title.clone(); |
| 32 | + let description = alert.description.clone(); |
| 33 | + let metadata = alert.metadata.clone(); |
| 34 | + |
| 35 | + let message = format!( |
| 36 | + "*{title} - {network}*\n\ |
| 37 | + {description}.\n\ |
| 38 | + *Block*: {height}\n\ |
| 39 | + *Transaction*: {transaction}", |
| 40 | + title = title, |
| 41 | + description = description, |
| 42 | + height = metadata |
| 43 | + .clone() |
| 44 | + .block_height |
| 45 | + .map(|h| format!("<{}|{}>", block_explorer.get_block_url(h), h)) |
| 46 | + .unwrap_or_else(|| "N/A".to_string()), |
| 47 | + transaction = metadata |
| 48 | + .clone() |
| 49 | + .tx_id |
| 50 | + .map(|tx| format!("<{}|{}>", block_explorer.get_tx_url(&tx), tx)) |
| 51 | + .unwrap_or_else(|| "N/A".to_string()), |
| 52 | + network = self.network_id, |
| 53 | + ); |
| 54 | + |
| 55 | + let res = self.send_telegram_message(message).await; |
| 56 | + |
| 57 | + match res { |
| 58 | + Ok(response) if response.ok => return Ok(Some(alert.check_id)), |
| 59 | + Ok(response) => { |
| 60 | + return Err(format!( |
| 61 | + "Failed to send telegram alert, status: {}", |
| 62 | + response.description.unwrap_or_default() |
| 63 | + )); |
| 64 | + } |
| 65 | + Err(err) => { |
| 66 | + return Err(format!("Failed to send telegram alert: {}", err)); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + async fn send_resolve(&self, alert: Alert, date: &str) -> Result<(), String> { |
| 72 | + let title = alert.title.clone(); |
| 73 | + |
| 74 | + let message = format!( |
| 75 | + "*{title} - {network}*\n\ |
| 76 | + Issue from {data} was resolved.", |
| 77 | + title = title, |
| 78 | + data = date, |
| 79 | + network = self.network_id, |
| 80 | + ); |
| 81 | + |
| 82 | + let res = self.send_telegram_message(message).await; |
| 83 | + |
| 84 | + match res { |
| 85 | + Ok(response) if response.ok => return Ok(()), |
| 86 | + Ok(response) => { |
| 87 | + return Err(format!( |
| 88 | + "Failed to send slack alert, status: {}", |
| 89 | + response.description.unwrap_or_default() |
| 90 | + )); |
| 91 | + } |
| 92 | + Err(err) => { |
| 93 | + return Err(format!("Failed to send slack alert: {}", err)); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + fn get_block_explorer(&self) -> BlockExplorer { |
| 99 | + self.block_explorer.clone() |
| 100 | + } |
| 101 | + |
| 102 | + fn get_id(&self) -> String { |
| 103 | + "telegram".to_string() |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +impl TelegramAlert { |
| 108 | + pub fn new( |
| 109 | + block_explorer: BlockExplorer, |
| 110 | + telegram_config: TelegramAlertConfig, |
| 111 | + network_id: String, |
| 112 | + ) -> Self { |
| 113 | + Self { |
| 114 | + block_explorer, |
| 115 | + telegram_token: telegram_config.telegram_token, |
| 116 | + telegram_chat_id: telegram_config.telegram_chat_id, |
| 117 | + network_id, |
| 118 | + minimum_severity: telegram_config.minimum_severity, |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + async fn send_telegram_message(&self, message: String) -> anyhow::Result<Response> { |
| 123 | + Telegrama::configure(|config| { |
| 124 | + config.set_bot_token(&self.telegram_token); |
| 125 | + config.set_chat_id(&self.telegram_chat_id); |
| 126 | + |
| 127 | + config.set_default_parse_mode("MarkdownV2"); // or "HTML" |
| 128 | + config.set_disable_web_page_preview(true); |
| 129 | + |
| 130 | + let formatting = FormattingOptions { |
| 131 | + escape_markdown: true, |
| 132 | + obfuscate_emails: true, |
| 133 | + escape_html: false, |
| 134 | + truncate: Some(4096), |
| 135 | + }; |
| 136 | + config.set_formatting_options(formatting); |
| 137 | + |
| 138 | + let client_options = ClientOptions { |
| 139 | + timeout: 30, |
| 140 | + retry_count: 3, |
| 141 | + retry_delay: 1, |
| 142 | + }; |
| 143 | + config.set_client_options(client_options); |
| 144 | + }); |
| 145 | + |
| 146 | + Telegrama::send_message( |
| 147 | + message, |
| 148 | + &[ |
| 149 | + ("parse_mode", "MarkdownV2"), |
| 150 | + ("disable_web_page_preview", "true"), |
| 151 | + ], |
| 152 | + ) |
| 153 | + .context("sending telegram message") |
| 154 | + } |
| 155 | +} |
0 commit comments