From 739c9d770b4d480a46d8c812b2932158cb2d8f48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Fri, 12 Jan 2024 16:43:30 +0100 Subject: [PATCH] Print available bots for telegram botfather --- Cargo.lock | 29 +++++++++++++++++++++++++++++ Cargo.toml | 1 + src/bot.rs | 22 +++++++++++++++++++++- src/main.rs | 8 ++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 28b7cfc..6e7abc7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -204,6 +204,7 @@ dependencies = [ "rust-i18n", "serde", "serde_yaml 0.9.30", + "strum", "teloxide", "thiserror", "tokio", @@ -1497,6 +1498,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rustversion" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4" + [[package]] name = "ryu" version = "1.0.16" @@ -1685,6 +1692,28 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "strum" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "290d54ea6f91c969195bdbcd7442c8c2a2ba87da8bf60a7ee86a235d4bc1e125" +dependencies = [ + "strum_macros", +] + +[[package]] +name = "strum_macros" +version = "0.25.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23dc1fa9ac9c169a78ba62f0b841814b7abae11bdd047b9c58f893439e309ea0" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "rustversion", + "syn 2.0.48", +] + [[package]] name = "syn" version = "1.0.109" diff --git a/Cargo.toml b/Cargo.toml index 8da5488..93fd47c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ rrule = "0.11.0" rust-i18n = "2.3.0" serde = { version = "1.0.145", features = ["derive"] } serde_yaml = "0.9.13" +strum = { version = "0.25.0", features = ["derive"] } teloxide = { version = "0.12.2", features = ["macros", "throttle"] } thiserror = "1.0.37" tokio = { version = "1.21.2", features = ["macros"] } diff --git a/src/bot.rs b/src/bot.rs index 1148eac..1fd0ef0 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -6,6 +6,9 @@ use diesel::Connection; use diesel::ExpressionMethods; use diesel::QueryDsl; use diesel::RunQueryDsl; +use strum::Display; +use strum::EnumIter; +use strum::IntoEnumIterator; use teloxide::adaptors::Throttle; use teloxide::dispatching::dialogue; use teloxide::dispatching::dialogue::InMemStorage; @@ -28,7 +31,7 @@ use crate::db::ChatInfo; use crate::db::DbChat; use crate::{schema, Database}; -#[derive(BotCommands, Clone)] +#[derive(BotCommands, Clone, EnumIter, Display)] #[command(rename_rule = "lowercase")] pub enum Command { #[command()] @@ -37,6 +40,23 @@ pub enum Command { RemindDaysAhead, } +impl Command { + pub fn print_commands() { + for command in Command::iter() { + let description = match command { + Command::SetCalendar => { + "Specify an URL to an ical file from which this bot will poll for events" + } + Command::SetLocale => "Choose between the languages \"de\" and \"en\"", + Command::RemindDaysAhead => { + "Choose how many days ahead of the event you'd like to be reminded" + } + }; + println!("{} - {}", command.to_string().to_lowercase(), description); + } + } +} + pub async fn spawn(bot: Throttle, db: Database) { Dispatcher::builder(bot, build_handler_chain()) .dependencies(deps![db, InMemStorage::<()>::new()]) diff --git a/src/main.rs b/src/main.rs index a71a4a0..310bc44 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ mod db; mod error; mod schema; +use std::env::args; use std::time::Duration; use std::{env, fs::File, io::BufReader, sync::Arc}; @@ -27,6 +28,7 @@ use teloxide::types::ChatId; use teloxide::{adaptors::throttle::Limits, Bot}; use tokio::time::sleep; +use crate::bot::Command; use crate::db::DbChat; #[macro_use] @@ -74,6 +76,12 @@ pub type Database = Arc>; #[tokio::main(flavor = "current_thread")] async fn main() { + if let Some(arg1) = args().nth(1) { + if arg1 == "commands" { + Command::print_commands(); + return; + } + } pretty_env_logger::init(); let config = Config::load().unwrap(); info!("Connecting to database {}", config.data_path);