labadoor-telegram: Initial commit

This commit is contained in:
George Kaklamanos 2021-11-27 23:57:48 +02:00
parent cb847c1071
commit 756979b00c
No known key found for this signature in database
GPG key ID: 95E488F0473F734D
3 changed files with 68 additions and 0 deletions

View file

@ -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"] }

View file

@ -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<AutoSend<Bot>, Message>,
command: Command,
) -> Result<(), Box<dyn Error + Send + Sync>> {
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;
}