labadoor/labadoor-telegram/src/lib.rs

49 lines
1.3 KiB
Rust
Raw Normal View History

use teloxide::{prelude::*, utils::command::BotCommands};
2021-11-27 23:57:48 +02:00
#[derive(BotCommands, Clone)]
#[command(
rename_rule = "lowercase",
description = "These commands are supported:"
)]
2021-11-27 23:57:48 +02:00
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(bot: Bot, message: Message, command: Command) -> ResponseResult<()> {
2021-11-27 23:57:48 +02:00
match command {
Command::Ping => bot.send_message(message.chat.id, "Pong!").await?,
2021-11-27 23:57:48 +02:00
Command::Open => {
open(message.chat.id.0);
bot.send_message(message.chat.id, "Open Sesame!").await?
}
2021-11-27 23:57:48 +02:00
Command::Register | Command::Start => {
let msg = format!("Your Telegram ID is: {}", message.chat.id);
bot.send_message(message.chat.id, msg).await?
}
2021-11-27 23:57:48 +02:00
};
Ok(())
}
#[tokio::main]
2023-06-30 18:02:40 +03:00
pub async fn telegram(token: String) {
let bot = Bot::new(token);
Command::repl(bot, answer).await;
2021-11-27 23:57:48 +02:00
}