From 89168ff11d4adb4ca51fe14e7d54fb2d69d392a8 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 29 Jul 2021 20:02:47 +0200 Subject: refactor: manage callbacks with tokio-stream --- Cargo.toml | 3 +++ src/commands.rs | 29 +++++++++++++++++++++++++---- src/main.rs | 20 +++++++++++++++++++- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4f5f5e5..9de3470 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ edition = "2018" [dependencies] teloxide = { version = "0.4", features = ["auto-send", "macros"] } + log = "0.4.8" pretty_env_logger = "0.4.0" tokio = { version = "1.3", features = ["rt-multi-thread", "macros"] } @@ -12,3 +13,5 @@ thirtyfour = "0.26.0" config = "0.10.1" serde = { version = "1.0.104", features = ["derive"] } + +tokio-stream = "0.1.6" diff --git a/src/commands.rs b/src/commands.rs index 1aee098..e498916 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,5 +1,7 @@ use std::error::Error; +use teloxide::payloads::SendMessageSetters; use teloxide::prelude::{AutoSend, Bot, Message, UpdateWithCx}; +use teloxide::types::{InlineKeyboardButton, InlineKeyboardMarkup}; use teloxide::utils::command::BotCommand; #[derive(BotCommand)] @@ -7,14 +9,33 @@ use teloxide::utils::command::BotCommand; pub enum Command { #[command(description = "Display this text")] Help, + #[command(description = "Reserve a classroom for tomorrow", parse_with = "split")] + Room, } pub async fn handler( cx: UpdateWithCx, Message>, - command: Command, ) -> Result<(), Box> { - match command { - Command::Help => cx.answer(Command::descriptions()).await?, - }; + let txt = cx.update.text(); + if txt.is_none() { + return Ok(()); + } + + if let Ok(command) = BotCommand::parse(txt.unwrap(), "unict-reservation") { + match command { + Command::Help => { + cx.answer(Command::descriptions()).await?; + } + Command::Room => { + let url_button = + InlineKeyboardButton::callback("hello".to_string(), "hello_call".to_string()); + let keyboard = InlineKeyboardMarkup::default().append_row(vec![url_button]); + cx.answer("Where?").reply_markup(keyboard).await?; + } + } + } else { + cx.reply_to("Command not found!").await?; + } + Ok(()) } diff --git a/src/main.rs b/src/main.rs index 0ac5a4d..220b7ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,8 @@ mod config; use crate::config::Config; +use tokio_stream::wrappers::UnboundedReceiverStream; + #[tokio::main] async fn main() -> Result<(), Box> { teloxide::enable_logging!(); @@ -23,7 +25,23 @@ async fn main() -> Result<(), Box> { panic!("You can't connect: `{}`, credentials are {:?}", e, config); } }; - teloxide::commands_repl(bot, "unict-reservation", commands::handler).await; + + Dispatcher::new(bot) + .messages_handler(|rx: DispatcherHandlerRx, Message>| { + UnboundedReceiverStream::new(rx).for_each_concurrent(None, |cx| async move { + commands::handler(cx).await.log_on_error().await; + }) + }) + .callback_queries_handler(|rx: DispatcherHandlerRx, CallbackQuery>| { + UnboundedReceiverStream::new(rx).for_each_concurrent(None, |cx| async move { + let data = &cx.update.data; + if let Some(d) = data { + println!("{}", d); + } + }) + }) + .dispatch() + .await; log::info!("Closing bot... Goodbye!"); Ok(()) -- cgit v1.2.3-18-g5258