rsiot/components/cmp_telegram/
telegram_bot.rs

1use std::sync::Arc;
2
3use teloxide::{prelude::Requester, types::ChatId, Bot};
4use tokio::sync::Mutex;
5use tracing::warn;
6
7#[derive(Clone)]
8pub struct TelegramBot {
9    bot: Arc<Mutex<Bot>>,
10    chat_id: ChatId,
11}
12
13impl TelegramBot {
14    pub fn new(bot_token: String, chat_id: i64) -> Self {
15        let bot = Arc::new(Mutex::new(Bot::new(bot_token)));
16        let chat_id = ChatId(chat_id);
17        Self { bot, chat_id }
18    }
19
20    pub async fn send_message(&self, message: &str) {
21        let bot = self.bot.lock().await;
22        let res = bot.send_message(self.chat_id, message).await;
23        if let Err(err) = res {
24            warn!("cmp_telegram error: {}", err);
25        }
26    }
27}
28// " ⚠️ ❗ alarm!!"