diff --git a/Cargo.toml b/Cargo.toml index feaede0..24f5e90 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,4 +2,5 @@ members = [ "labadoor-matrix", "labadoor-gpio", + "labadoor-telegram", ] diff --git a/labadoor-telegram/Cargo.toml b/labadoor-telegram/Cargo.toml new file mode 100644 index 0000000..59315d7 --- /dev/null +++ b/labadoor-telegram/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "labadoor-telegram" +version = "0.1.0" +edition = "2021" +license = "AGPL-3.0-or-later" + +[dependencies] +log = "0.4.14" +pretty_env_logger = "0.4.0" +teloxide = {version = "0.5.2", features = ["macros", "auto-send"]} +tokio = { version = "1.12.0", features = ["rt-multi-thread", "macros"] } + +[target.armv7-unknown-linux-musleabihf.dependencies] +openssl-sys = { version = "0.9.67", features = ["vendored"] } diff --git a/labadoor-telegram/src/main.rs b/labadoor-telegram/src/main.rs new file mode 100644 index 0000000..06cf601 --- /dev/null +++ b/labadoor-telegram/src/main.rs @@ -0,0 +1,53 @@ +use teloxide::{prelude::*, utils::command::BotCommand}; +use std::error::Error; + +#[derive(BotCommand)] +#[command(rename = "lowercase", description = "These commands are supported:")] +enum Command { + #[command(description = "Ping")] + Ping, + #[command(description = "Open")] + Open, + #[command(description = "Register")] + Register, + #[command(description = "Start")] + 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(); +} + +async fn answer( + cx: UpdateWithCx, Message>, + command: Command, +) -> Result<(), Box> { + match command { + Command::Ping => cx.answer("Pong!").await?, + Command::Open => { + open(cx.update.chat_id()); + cx.answer("Open sesame!").await? + }, + Command::Register | Command::Start => { + let msg = format!("Your Telegram ID is: {}", cx.update.chat_id()); + cx.answer(msg).await? + }, + }; + + Ok(()) +} + +#[tokio::main] +async fn main() { + teloxide::enable_logging!(); + log::info!("Starting labadoor Telegram bot..."); + let token = std::env::var("TELEGRAM_TOKEN").expect("TELEGRAM_TOKEN not set"); + let bot = Bot::new(token).auto_send(); + let bot_name: String = "Labadoor Telegram bot".to_string(); + teloxide::commands_repl(bot, bot_name, answer).await; +}