mirror of
https://git.alemi.dev/guestbook.rs.git
synced 2024-12-19 02:54:52 +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"
|
toml = "0.8.8"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = ["telegram"]
|
||||||
telegram = ["dep:teloxide"]
|
telegram = ["dep:teloxide"]
|
||||||
|
|
|
@ -1,24 +1,39 @@
|
||||||
use teloxide::prelude::*;
|
use teloxide::prelude::*;
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
use crate::model::Page;
|
||||||
static ref BOT : Bot = Bot::from_env();
|
|
||||||
static ref CHAT_ID : RwLock<ChatId> = RwLock::new(ChatId(0)); // TODO ewwwwwww
|
use super::NotificationProcessor;
|
||||||
|
|
||||||
|
pub struct TGNotifier {
|
||||||
|
bot: Bot,
|
||||||
|
chat_id: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn suggestion_inner(payload: Suggestion) -> (StatusCode, Json<Acknowledgement>) {
|
impl TGNotifier {
|
||||||
let message = format!(
|
pub fn new(token: &str, chat_id: i64) -> Self {
|
||||||
"[<code>{}</code>] <i>{}</i> | {}",
|
TGNotifier {
|
||||||
html_escape::encode_text(payload.author.as_deref().unwrap_or("anon")),
|
chat_id,
|
||||||
html_escape::encode_text(payload.contact.as_deref().unwrap_or("N/A")),
|
bot: Bot::new(token),
|
||||||
html_escape::encode_text(&payload.body)
|
}
|
||||||
);
|
}
|
||||||
|
}
|
||||||
match BOT
|
|
||||||
.send_message(*CHAT_ID.read().await, message)
|
#[async_trait::async_trait]
|
||||||
.parse_mode(teloxide::types::ParseMode::Html)
|
impl NotificationProcessor<Page> for TGNotifier {
|
||||||
.await
|
async fn process(&self, notification: &Page) {
|
||||||
{
|
let message = format!(
|
||||||
Ok(x) => (StatusCode::OK, Json(Acknowledgement::Sent(x.text().unwrap_or("").into()))),
|
"[<code>{}</code>] <i>{}</i> | {}",
|
||||||
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, Json(Acknowledgement::Refused(e.to_string()))),
|
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