feat: add telegram notifier

This commit is contained in:
əlemi 2024-01-02 04:55:02 +01:00
parent 9659bee4e1
commit 3c0ba7e35d
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 34 additions and 19 deletions

View file

@ -25,5 +25,5 @@ teloxide = { version = "0.12.2", features = ["macros"], optional = true }
toml = "0.8.8"
[features]
default = []
default = ["telegram"]
telegram = ["dep:teloxide"]

View file

@ -1,24 +1,39 @@
use teloxide::prelude::*;
lazy_static::lazy_static! {
static ref BOT : Bot = Bot::from_env();
static ref CHAT_ID : RwLock<ChatId> = RwLock::new(ChatId(0)); // TODO ewwwwwww
use crate::model::Page;
use super::NotificationProcessor;
pub struct TGNotifier {
bot: Bot,
chat_id: i64,
}
async fn suggestion_inner(payload: Suggestion) -> (StatusCode, Json<Acknowledgement>) {
let message = format!(
"[<code>{}</code>] <i>{}</i> | {}",
html_escape::encode_text(payload.author.as_deref().unwrap_or("anon")),
html_escape::encode_text(payload.contact.as_deref().unwrap_or("N/A")),
html_escape::encode_text(&payload.body)
);
match BOT
.send_message(*CHAT_ID.read().await, message)
.parse_mode(teloxide::types::ParseMode::Html)
.await
{
Ok(x) => (StatusCode::OK, Json(Acknowledgement::Sent(x.text().unwrap_or("").into()))),
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, Json(Acknowledgement::Refused(e.to_string()))),
impl TGNotifier {
pub fn new(token: &str, chat_id: i64) -> Self {
TGNotifier {
chat_id,
bot: Bot::new(token),
}
}
}
#[async_trait::async_trait]
impl NotificationProcessor<Page> for TGNotifier {
async fn process(&self, notification: &Page) {
let message = format!(
"[<code>{}</code>] <i>{}</i> | {}",
html_escape::encode_text(&notification.author),
html_escape::encode_text(notification.contact.as_deref().unwrap_or("N/A")),
html_escape::encode_text(&notification.body)
);
if let Err(e) = self.bot
.send_message(ChatId(self.chat_id), message)
.parse_mode(teloxide::types::ParseMode::Html)
.await
{
tracing::error!("could not send message on telegram: {}", e);
}
}
}