From e8b27ba749e6aef8a12a3d79c5b37825d8a6feb5 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 12 Oct 2021 21:53:41 +0200 Subject: [PATCH] Merge two source files into one --- examples/buttons/src/commands.rs | 63 ------------------------------ examples/buttons/src/main.rs | 67 ++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 67 deletions(-) delete mode 100644 examples/buttons/src/commands.rs diff --git a/examples/buttons/src/commands.rs b/examples/buttons/src/commands.rs deleted file mode 100644 index 12b1fd1a..00000000 --- a/examples/buttons/src/commands.rs +++ /dev/null @@ -1,63 +0,0 @@ -use std::error::Error; -use teloxide::{ - payloads::SendMessageSetters, - prelude::{AutoSend, Bot, GetChatId, Message, UpdateWithCx}, - types::{InlineKeyboardButton, InlineKeyboardMarkup}, - utils::command::BotCommand, -}; - -#[derive(BotCommand)] -#[command(rename = "lowercase", description = "These commands are supported:")] -pub enum Command { - #[command(description = "Display this text")] - Help, - #[command(description = "Start")] - Start, -} - -/// Creates a keyboard made by buttons in a big column. -fn make_keyboard(chat_id: i64) -> InlineKeyboardMarkup { - let mut keyboard_array: Vec> = vec![]; - // The column is made by the list of Debian versions. - let debian_versions = vec![ - "Buzz", "Rex", "Bo", "Hamm", "Slink", "Potato", "Woody", "Sarge", "Etch", "Lenny", - "Squeeze", "Wheezy", "Jessie", "Stretch", "Buster", "Bullseye", - ]; - - for version in debian_versions { - // Match each button with the chat id and the Debian version. - keyboard_array.push(vec![InlineKeyboardButton::callback( - version.into(), - format!("{}_{}", chat_id, version), - )]); - } - - InlineKeyboardMarkup::new(keyboard_array) -} - -/// Parse the text wrote on Telegram and check if that text is a valid command -/// or not, then match the command. If the command is `/start` it writes a -/// markup with the `InlineKeyboardMarkup`. -pub async fn handler( - cx: UpdateWithCx, Message>, -) -> Result<(), Box> { - if let Ok(command) = - BotCommand::parse(cx.update.text().expect("Error with the text"), "buttons") - { - match command { - Command::Help => { - // Just send the description of all commands. - cx.answer(Command::descriptions()).await?; - } - Command::Start => { - let keyboard = make_keyboard(cx.chat_id()); - // Create a list of buttons using callbacks to receive the response. - cx.answer("Debian versions:").reply_markup(keyboard).await?; - } - } - } else { - cx.reply_to("Command not found!").await?; - } - - Ok(()) -} diff --git a/examples/buttons/src/main.rs b/examples/buttons/src/main.rs index f5027032..c935d8e9 100644 --- a/examples/buttons/src/main.rs +++ b/examples/buttons/src/main.rs @@ -1,10 +1,69 @@ -mod commands; - use std::error::Error; -use teloxide::prelude::*; +use teloxide::{ + payloads::SendMessageSetters, + prelude::*, + types::{InlineKeyboardButton, InlineKeyboardMarkup}, + utils::command::BotCommand, +}; use tokio_stream::wrappers::UnboundedReceiverStream; +#[derive(BotCommand)] +#[command(rename = "lowercase", description = "These commands are supported:")] +pub enum Command { + #[command(description = "Display this text")] + Help, + #[command(description = "Start")] + Start, +} + +/// Creates a keyboard made by buttons in a big column. +fn make_keyboard(chat_id: i64) -> InlineKeyboardMarkup { + let mut keyboard_array: Vec> = vec![]; + // The column is made by the list of Debian versions. + let debian_versions = vec![ + "Buzz", "Rex", "Bo", "Hamm", "Slink", "Potato", "Woody", "Sarge", "Etch", "Lenny", + "Squeeze", "Wheezy", "Jessie", "Stretch", "Buster", "Bullseye", + ]; + + for version in debian_versions { + // Match each button with the chat id and the Debian version. + keyboard_array.push(vec![InlineKeyboardButton::callback( + version.into(), + format!("{}_{}", chat_id, version), + )]); + } + + InlineKeyboardMarkup::new(keyboard_array) +} + +/// Parse the text wrote on Telegram and check if that text is a valid command +/// or not, then match the command. If the command is `/start` it writes a +/// markup with the `InlineKeyboardMarkup`. +pub async fn query_handler( + cx: UpdateWithCx, Message>, +) -> Result<(), Box> { + if let Ok(command) = + BotCommand::parse(cx.update.text().expect("Error with the text"), "buttons") + { + match command { + Command::Help => { + // Just send the description of all commands. + cx.answer(Command::descriptions()).await?; + } + Command::Start => { + let keyboard = make_keyboard(cx.chat_id()); + // Create a list of buttons using callbacks to receive the response. + cx.answer("Debian versions:").reply_markup(keyboard).await?; + } + } + } else { + cx.reply_to("Command not found!").await?; + } + + Ok(()) +} + /// When it receives a callback from a button it edits the message with all /// those buttons writing a text with the selected Debian version. async fn callback_hander( @@ -37,7 +96,7 @@ async fn main() -> Result<(), Box> { 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; + query_handler(cx).await.log_on_error().await; }) }) .callback_queries_handler(|rx: DispatcherHandlerRx, CallbackQuery>| {