teloxide/examples/buttons.rs

136 lines
4 KiB
Rust
Raw Normal View History

use std::error::Error;
2021-10-12 21:53:41 +02:00
use teloxide::{
payloads::SendMessageSetters,
2022-01-12 11:36:52 +01:00
prelude2::*,
2021-10-16 10:07:55 +02:00
types::{
InlineKeyboardButton, InlineKeyboardMarkup, InlineQueryResultArticle, InputMessageContent,
InputMessageContentText,
},
2021-10-12 21:53:41 +02:00
utils::command::BotCommand,
};
2021-10-12 21:53:41 +02:00
#[derive(BotCommand)]
#[command(rename = "lowercase", description = "These commands are supported:")]
2021-10-12 21:55:36 +02:00
enum Command {
2021-10-12 21:53:41 +02:00
#[command(description = "Display this text")]
Help,
#[command(description = "Start")]
Start,
}
/// Creates a keyboard made by buttons in a big column.
fn make_keyboard() -> InlineKeyboardMarkup {
let mut keyboard: Vec<Vec<InlineKeyboardButton>> = vec![];
let debian_versions = [
2021-10-12 21:53:41 +02:00
"Buzz", "Rex", "Bo", "Hamm", "Slink", "Potato", "Woody", "Sarge", "Etch", "Lenny",
"Squeeze", "Wheezy", "Jessie", "Stretch", "Buster", "Bullseye",
];
for versions in debian_versions.chunks(3) {
let row = versions
.iter()
.map(|&version| InlineKeyboardButton::callback(version.to_owned(), version.to_owned()))
.collect();
keyboard.push(row);
2021-10-12 21:53:41 +02:00
}
InlineKeyboardMarkup::new(keyboard)
2021-10-12 21:53:41 +02:00
}
/// 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`.
2021-10-12 21:56:49 +02:00
async fn message_handler(
2022-01-12 11:36:52 +01:00
m: Message,
bot: AutoSend<Bot>,
2021-10-12 21:53:41 +02:00
) -> Result<(), Box<dyn Error + Send + Sync>> {
2022-01-12 11:36:52 +01:00
match m.text() {
Some(text) => {
match BotCommand::parse(text, "buttons") {
Ok(Command::Help) => {
// Just send the description of all commands.
2022-01-12 11:36:52 +01:00
bot.send_message(m.chat.id, Command::descriptions()).await?;
}
Ok(Command::Start) => {
// Create a list of buttons and send them.
let keyboard = make_keyboard();
2022-01-12 11:36:52 +01:00
bot.send_message(m.chat.id, "Debian versions:").reply_markup(keyboard).await?;
}
Err(_) => {
2022-01-12 11:36:52 +01:00
bot.send_message(m.chat.id, "Command not found!").await?;
}
2021-10-12 21:53:41 +02:00
}
}
None => {}
2021-10-12 21:53:41 +02:00
}
Ok(())
}
2021-10-16 10:07:55 +02:00
async fn inline_query_handler(
2022-01-12 11:36:52 +01:00
q: InlineQuery,
bot: AutoSend<Bot>,
2021-10-16 10:07:55 +02:00
) -> Result<(), Box<dyn Error + Send + Sync>> {
let choose_debian_version = InlineQueryResultArticle::new(
"0",
"Chose debian version",
InputMessageContent::Text(InputMessageContentText::new("Debian versions:")),
)
.reply_markup(make_keyboard());
2022-01-12 11:36:52 +01:00
bot.answer_inline_query(q.id, vec![choose_debian_version.into()]).await?;
2021-10-16 10:07:55 +02:00
Ok(())
}
2021-10-12 21:50:37 +02:00
/// When it receives a callback from a button it edits the message with all
/// those buttons writing a text with the selected Debian version.
2022-01-12 11:36:52 +01:00
///
/// **IMPORTANT**: do not send privacy-sensitive data this way!!!
/// Any can read data stored in the callback button.
2021-10-13 09:19:27 +02:00
async fn callback_handler(
2022-01-12 11:36:52 +01:00
q: CallbackQuery,
bot: AutoSend<Bot>,
2021-10-12 21:50:37 +02:00
) -> Result<(), Box<dyn Error + Send + Sync>> {
2022-01-12 11:36:52 +01:00
if let Some(version) = q.data {
2021-10-16 10:07:55 +02:00
let text = format!("You chose: {}", version);
2022-01-12 11:36:52 +01:00
match q.message {
2021-10-16 10:07:55 +02:00
Some(Message { id, chat, .. }) => {
bot.edit_message_text(chat.id, id, text).await?;
}
None => {
2022-01-12 11:36:52 +01:00
if let Some(id) = q.inline_message_id {
bot.edit_message_text_inline(id, text).await?;
2021-10-16 10:07:55 +02:00
}
}
}
2021-10-12 21:50:37 +02:00
log::info!("You chose: {}", version);
}
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
teloxide::enable_logging!();
log::info!("Starting bot...");
let bot = Bot::from_env().auto_send();
2022-01-26 10:51:51 +01:00
let handler = dptree::entry()
.branch(Update::filter_message().endpoint(message_handler))
.branch(Update::filter_callback_query().endpoint(callback_handler))
.branch(Update::filter_inline_query().endpoint(inline_query_handler));
DispatcherBuilder::new(bot, handler).build().dispatch().await;
log::info!("Closing bot... Goodbye!");
Ok(())
}