aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/actions/telegram.rs
diff options
context:
space:
mode:
authorLibravatar sommerfeld <sommerfeld@sommerfeld.dev>2024-04-21 16:04:38 +0100
committerLibravatar sommerfeld <sommerfeld@sommerfeld.dev>2024-04-21 16:04:38 +0100
commit1ab6ecba6f509b7b76865d65c77ecebc51efd2d3 (patch)
treea9b92e15769d483560d5799569b14c985b9c3ea5 /src/actions/telegram.rs
downloadsentrum-0.1.0.tar.gz
sentrum-0.1.0.tar.bz2
sentrum-0.1.0.zip
Initial commitv0.1.0
Diffstat (limited to 'src/actions/telegram.rs')
-rw-r--r--src/actions/telegram.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/actions/telegram.rs b/src/actions/telegram.rs
new file mode 100644
index 0000000..b489864
--- /dev/null
+++ b/src/actions/telegram.rs
@@ -0,0 +1,56 @@
+use super::Action;
+use crate::message::MessageConfig;
+use crate::message::MessageParams;
+use anyhow::Result;
+use async_trait::async_trait;
+use serde::Deserialize;
+use teloxide::requests::Requester;
+use teloxide::types::UserId;
+use teloxide::Bot;
+
+#[derive(Deserialize, Debug)]
+pub struct TelegramConfig {
+ bot_token: String,
+ user_id: u64,
+}
+
+impl TelegramConfig {
+ pub fn bot_token(&self) -> &str {
+ &self.bot_token
+ }
+
+ pub fn user_id(&self) -> u64 {
+ self.user_id
+ }
+}
+
+pub struct TelegramAction<'a> {
+ message_config: &'a MessageConfig,
+ bot: Bot,
+ user_id: UserId,
+}
+
+impl<'a> TelegramAction<'a> {
+ pub fn new(
+ message_config: &'a MessageConfig,
+ telegram_config: &'a TelegramConfig,
+ ) -> Result<Self> {
+ Ok(Self {
+ message_config,
+ bot: Bot::new(telegram_config.bot_token()),
+ user_id: UserId(telegram_config.user_id()),
+ })
+ }
+}
+
+#[async_trait]
+impl Action<'_> for TelegramAction<'_> {
+ async fn run(&self, params: Option<&MessageParams<'_, '_>>) -> Result<()> {
+ let subject = self.message_config.subject(params)?;
+ let body = self.message_config.body(params)?;
+ self.bot
+ .send_message(self.user_id, format!("{}\n{}", subject, body))
+ .await?;
+ Ok(())
+ }
+}