mirror of
https://git.alemi.dev/guestbook.rs.git
synced 2024-12-18 18:54:51 +01:00
feat: add telegram notifier
This commit is contained in:
parent
9659bee4e1
commit
3c0ba7e35d
2 changed files with 34 additions and 19 deletions
|
@ -25,5 +25,5 @@ teloxide = { version = "0.12.2", features = ["macros"], optional = true }
|
|||
toml = "0.8.8"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
default = ["telegram"]
|
||||
telegram = ["dep:teloxide"]
|
||||
|
|
|
@ -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(¬ification.author),
|
||||
html_escape::encode_text(notification.contact.as_deref().unwrap_or("N/A")),
|
||||
html_escape::encode_text(¬ification.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue