telegram: Use labadoor-open and labadoor-common::run_bin()

This commit is contained in:
George Kaklamanos 2023-11-28 16:59:56 +02:00
parent 0ab168b7f7
commit 6ece32b40e
Signed by: gkaklas
GPG key ID: C0CAB8A6BDC9399D
4 changed files with 28 additions and 13 deletions

View file

@ -7,6 +7,7 @@ license = "AGPL-3.0-or-later"
[dependencies] [dependencies]
teloxide = { version = "0.12.2", features = ["macros"] } teloxide = { version = "0.12.2", features = ["macros"] }
tokio = { version = "1.34.0", features = ["rt-multi-thread", "macros"] } tokio = { version = "1.34.0", features = ["rt-multi-thread", "macros"] }
labadoor-common = { path = "../labadoor-common"}
[target.armv7-unknown-linux-musleabihf.dependencies] [target.armv7-unknown-linux-musleabihf.dependencies]
openssl-sys = { version = "0.9.67", features = ["vendored"] } openssl-sys = { version = "0.9.67", features = ["vendored"] }

View file

@ -1,6 +1,7 @@
use teloxide::{prelude::*, utils::command::BotCommands}; use teloxide::{prelude::*, utils::command::BotCommands};
pub struct TelegramArgs { pub struct TelegramArgs {
pub trigger: Vec<String>,
pub token: String, pub token: String,
} }
@ -20,22 +21,26 @@ enum Command {
Start, Start,
} }
fn open(param: i64) { fn open(trigger: Vec<String>, param: i64) -> Result<String, String> {
use std::process::Command; let a = labadoor_common::OpenBinaryArgs {
use std::io::{self, Write}; method: "telegram".to_string(),
let mut cmd = Command::new("/usr/local/bin/doorlock"); identifier: param.to_string(),
cmd.arg("telegram").arg(param.to_string()); resource_shortcut: 1,
let out = cmd.output().expect("Could not run command"); };
io::stdout().write_all(&out.stdout).unwrap(); labadoor_common::run_open(a, trigger)
} }
async fn answer(bot: Bot, message: Message, command: Command) -> ResponseResult<()> { async fn answer(
bot: Bot,
trigger: Vec<String>,
message: Message,
command: Command,
) -> ResponseResult<()> {
match command { match command {
Command::Ping => bot.send_message(message.chat.id, "Pong!").await?, Command::Ping => bot.send_message(message.chat.id, "Pong!").await?,
Command::Open => { Command::Open => match open(trigger, message.chat.id.0) {
open(message.chat.id.0); Ok(msg) | Err(msg) => bot.send_message(message.chat.id, msg).await?,
bot.send_message(message.chat.id, "Open Sesame!").await? },
}
Command::Register | Command::Start => { Command::Register | Command::Start => {
let msg = format!("Your Telegram ID is: {}", message.chat.id); let msg = format!("Your Telegram ID is: {}", message.chat.id);
bot.send_message(message.chat.id, msg).await? bot.send_message(message.chat.id, msg).await?
@ -48,5 +53,11 @@ async fn answer(bot: Bot, message: Message, command: Command) -> ResponseResult<
#[tokio::main] #[tokio::main]
pub async fn telegram(args: TelegramArgs) { pub async fn telegram(args: TelegramArgs) {
let bot = Bot::new(args.token); let bot = Bot::new(args.token);
Command::repl(bot, answer).await; let handler = Update::filter_message()
.branch(dptree::entry().filter_command::<Command>().endpoint(answer));
Dispatcher::builder(bot, handler)
.dependencies(dptree::deps![args.trigger])
.build()
.dispatch()
.await;
} }

View file

@ -1,4 +1,5 @@
[telegram] [telegram]
trigger = [ "/usr/bin/labadoor", "open" ]
token = "" token = ""
[matrix] [matrix]

View file

@ -24,6 +24,8 @@ pub enum Command {
#[cfg(feature = "telegram")] #[cfg(feature = "telegram")]
#[derive(Serialize, Deserialize, Parser, Debug)] #[derive(Serialize, Deserialize, Parser, Debug)]
pub struct Telegram { pub struct Telegram {
#[clap(skip)]
pub trigger: Option<Vec<String>>,
#[clap(short, long)] #[clap(short, long)]
pub token: Option<String>, pub token: Option<String>,
} }