diff --git a/labadoor-telegram/Cargo.toml b/labadoor-telegram/Cargo.toml index 9c45f95..baa1e8e 100644 --- a/labadoor-telegram/Cargo.toml +++ b/labadoor-telegram/Cargo.toml @@ -7,6 +7,7 @@ license = "AGPL-3.0-or-later" [dependencies] teloxide = { version = "0.12.2", features = ["macros"] } tokio = { version = "1.34.0", features = ["rt-multi-thread", "macros"] } +labadoor-common = { path = "../labadoor-common"} [target.armv7-unknown-linux-musleabihf.dependencies] openssl-sys = { version = "0.9.67", features = ["vendored"] } diff --git a/labadoor-telegram/src/lib.rs b/labadoor-telegram/src/lib.rs index ab1bad3..97e875e 100644 --- a/labadoor-telegram/src/lib.rs +++ b/labadoor-telegram/src/lib.rs @@ -1,6 +1,7 @@ use teloxide::{prelude::*, utils::command::BotCommands}; pub struct TelegramArgs { + pub trigger: Vec, pub token: String, } @@ -20,22 +21,26 @@ enum Command { Start, } -fn open(param: i64) { - use std::process::Command; - use std::io::{self, Write}; - let mut cmd = Command::new("/usr/local/bin/doorlock"); - cmd.arg("telegram").arg(param.to_string()); - let out = cmd.output().expect("Could not run command"); - io::stdout().write_all(&out.stdout).unwrap(); +fn open(trigger: Vec, param: i64) -> Result { + let a = labadoor_common::OpenBinaryArgs { + method: "telegram".to_string(), + identifier: param.to_string(), + resource_shortcut: 1, + }; + labadoor_common::run_open(a, trigger) } -async fn answer(bot: Bot, message: Message, command: Command) -> ResponseResult<()> { +async fn answer( + bot: Bot, + trigger: Vec, + message: Message, + command: Command, +) -> ResponseResult<()> { match command { Command::Ping => bot.send_message(message.chat.id, "Pong!").await?, - Command::Open => { - open(message.chat.id.0); - bot.send_message(message.chat.id, "Open Sesame!").await? - } + Command::Open => match open(trigger, message.chat.id.0) { + Ok(msg) | Err(msg) => bot.send_message(message.chat.id, msg).await?, + }, Command::Register | Command::Start => { let msg = format!("Your Telegram ID is: {}", message.chat.id); bot.send_message(message.chat.id, msg).await? @@ -48,5 +53,11 @@ async fn answer(bot: Bot, message: Message, command: Command) -> ResponseResult< #[tokio::main] pub async fn telegram(args: TelegramArgs) { let bot = Bot::new(args.token); - Command::repl(bot, answer).await; + let handler = Update::filter_message() + .branch(dptree::entry().filter_command::().endpoint(answer)); + Dispatcher::builder(bot, handler) + .dependencies(dptree::deps![args.trigger]) + .build() + .dispatch() + .await; } diff --git a/labadoor/config.toml b/labadoor/config.toml index 64c95c8..e7c7fd9 100644 --- a/labadoor/config.toml +++ b/labadoor/config.toml @@ -1,4 +1,5 @@ [telegram] +trigger = [ "/usr/bin/labadoor", "open" ] token = "" [matrix] diff --git a/labadoor/src/cli.rs b/labadoor/src/cli.rs index 51f55c6..64e73fc 100644 --- a/labadoor/src/cli.rs +++ b/labadoor/src/cli.rs @@ -24,6 +24,8 @@ pub enum Command { #[cfg(feature = "telegram")] #[derive(Serialize, Deserialize, Parser, Debug)] pub struct Telegram { + #[clap(skip)] + pub trigger: Option>, #[clap(short, long)] pub token: Option, }