Add Telegram notification support
This commit is contained in:
		@@ -21,9 +21,15 @@ the following fields:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### Sanity failure push notification
 | 
					### Sanity failure push notification
 | 
				
			||||||
To receive a Slack and/or Discord notification on sanity failure, define one or
 | 
					To receive a Slack, Discord and/or Telgram notification on sanity failure,
 | 
				
			||||||
both of these environment variables before running `solana-watchtower`:
 | 
					define environment variables before running `solana-watchtower`:
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
export SLACK_WEBHOOK=...
 | 
					export SLACK_WEBHOOK=...
 | 
				
			||||||
export DISCORD_WEBHOOK=...
 | 
					export DISCORD_WEBHOOK=...
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Telgram requires the following two variables:
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					export TELEGRAM_BOT_TOKEN=...
 | 
				
			||||||
 | 
					export TELEGRAM_CHAT_ID=...
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,28 +3,44 @@ use reqwest::Client;
 | 
				
			|||||||
use serde_json::json;
 | 
					use serde_json::json;
 | 
				
			||||||
use std::env;
 | 
					use std::env;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct TelegramWebHook {
 | 
				
			||||||
 | 
					    bot_token: String,
 | 
				
			||||||
 | 
					    chat_id: String,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct Notifier {
 | 
					pub struct Notifier {
 | 
				
			||||||
    client: Client,
 | 
					    client: Client,
 | 
				
			||||||
    discord_webhook: Option<String>,
 | 
					    discord_webhook: Option<String>,
 | 
				
			||||||
    slack_webhook: Option<String>,
 | 
					    slack_webhook: Option<String>,
 | 
				
			||||||
 | 
					    telegram_webhook: Option<TelegramWebHook>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Notifier {
 | 
					impl Notifier {
 | 
				
			||||||
    pub fn new() -> Self {
 | 
					    pub fn new() -> Self {
 | 
				
			||||||
        let discord_webhook = env::var("DISCORD_WEBHOOK")
 | 
					        let discord_webhook = env::var("DISCORD_WEBHOOK")
 | 
				
			||||||
            .map_err(|_| {
 | 
					            .map_err(|_| {
 | 
				
			||||||
                warn!("Discord notifications disabled");
 | 
					                info!("Discord notifications disabled");
 | 
				
			||||||
            })
 | 
					            })
 | 
				
			||||||
            .ok();
 | 
					            .ok();
 | 
				
			||||||
        let slack_webhook = env::var("SLACK_WEBHOOK")
 | 
					        let slack_webhook = env::var("SLACK_WEBHOOK")
 | 
				
			||||||
            .map_err(|_| {
 | 
					            .map_err(|_| {
 | 
				
			||||||
                warn!("Slack notifications disabled");
 | 
					                info!("Slack notifications disabled");
 | 
				
			||||||
            })
 | 
					            })
 | 
				
			||||||
            .ok();
 | 
					            .ok();
 | 
				
			||||||
 | 
					        let telegram_webhook = if let (Ok(bot_token), Ok(chat_id)) =
 | 
				
			||||||
 | 
					            (env::var("TELEGRAM_BOT_TOKEN"), env::var("TELEGRAM_CHAT_ID"))
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            Some(TelegramWebHook { bot_token, chat_id })
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            info!("Telegram notifications disabled");
 | 
				
			||||||
 | 
					            None
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Notifier {
 | 
					        Notifier {
 | 
				
			||||||
            client: Client::new(),
 | 
					            client: Client::new(),
 | 
				
			||||||
            discord_webhook,
 | 
					            discord_webhook,
 | 
				
			||||||
            slack_webhook,
 | 
					            slack_webhook,
 | 
				
			||||||
 | 
					            telegram_webhook,
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -42,5 +58,14 @@ impl Notifier {
 | 
				
			|||||||
                warn!("Failed to send Slack message: {:?}", err);
 | 
					                warn!("Failed to send Slack message: {:?}", err);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if let Some(TelegramWebHook { chat_id, bot_token }) = &self.telegram_webhook {
 | 
				
			||||||
 | 
					            let data = json!({ "chat_id": chat_id, "text": msg });
 | 
				
			||||||
 | 
					            let url = format!("https://api.telegram.org/bot{}/sendMessage", bot_token);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if let Err(err) = self.client.post(&url).json(&data).send() {
 | 
				
			||||||
 | 
					                warn!("Failed to send Telegram message: {:?}", err);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user