Skip to main content

buttons/
buttons.rs

1use std::error::Error;
2use teloxide::{
3    payloads::SendMessageSetters,
4    prelude::*,
5    sugar::bot::BotMessagesExt,
6    types::{
7        BotFullInfo, InlineKeyboardButton, InlineKeyboardMarkup, InlineQueryResultArticle,
8        InputMessageContent, InputMessageContentText,
9    },
10    utils::command::BotCommands,
11};
12
13/// These commands are supported:
14#[derive(BotCommands)]
15#[command(rename_rule = "lowercase")]
16enum Command {
17    /// Display this text
18    Help,
19    /// Start
20    Start,
21}
22
23#[tokio::main]
24async fn main() -> Result<(), Box<dyn Error>> {
25    pretty_env_logger::init();
26    log::info!("Starting buttons bot...");
27
28    let bot = Bot::from_env();
29
30    let handler = dptree::entry()
31        .branch(Update::filter_message().endpoint(message_handler))
32        .branch(Update::filter_callback_query().endpoint(callback_handler))
33        .branch(Update::filter_inline_query().endpoint(inline_query_handler));
34
35    Dispatcher::builder(bot, handler).enable_ctrlc_handler().build().dispatch().await;
36    Ok(())
37}
38
39/// Creates a keyboard made by buttons in a big column.
40fn make_keyboard() -> InlineKeyboardMarkup {
41    let mut keyboard: Vec<Vec<InlineKeyboardButton>> = vec![];
42
43    let debian_versions = [
44        "Buzz", "Rex", "Bo", "Hamm", "Slink", "Potato", "Woody", "Sarge", "Etch", "Lenny",
45        "Squeeze", "Wheezy", "Jessie", "Stretch", "Buster", "Bullseye",
46    ];
47
48    for versions in debian_versions.chunks(3) {
49        let row = versions
50            .iter()
51            .map(|&version| InlineKeyboardButton::callback(version.to_owned(), version.to_owned()))
52            .collect();
53
54        keyboard.push(row);
55    }
56
57    InlineKeyboardMarkup::new(keyboard)
58}
59
60/// Parse the text wrote on Telegram and check if that text is a valid command
61/// or not, then match the command. If the command is `/start` it writes a
62/// markup with the `InlineKeyboardMarkup`.
63async fn message_handler(
64    bot: Bot,
65    msg: Message,
66    me: BotFullInfo,
67) -> Result<(), Box<dyn Error + Send + Sync>> {
68    if let Some(text) = msg.text() {
69        match BotCommands::parse(text, me.username().unwrap_or_default()) {
70            Ok(Command::Help) => {
71                // Just send the description of all commands.
72                bot.send_message(Command::descriptions().to_string()).chat_id(msg.chat.id).await?;
73            }
74            Ok(Command::Start) => {
75                // Create a list of buttons and send them.
76                let keyboard = make_keyboard();
77                bot.send_message("Debian versions:")
78                    .chat_id(msg.chat.id)
79                    .reply_markup(keyboard)
80                    .await?;
81            }
82
83            Err(_) => {
84                bot.send_message("Command not found!").chat_id(msg.chat.id).await?;
85            }
86        }
87    }
88
89    Ok(())
90}
91
92async fn inline_query_handler(
93    bot: Bot,
94    q: InlineQuery,
95) -> Result<(), Box<dyn Error + Send + Sync>> {
96    let choose_debian_version = InlineQueryResultArticle::new(
97        "0",
98        "Chose debian version",
99        InputMessageContent::Text(InputMessageContentText::new("Debian versions:")),
100    )
101    .reply_markup(make_keyboard());
102
103    bot.answer_inline_query(q.id, vec![choose_debian_version.into()]).await?;
104
105    Ok(())
106}
107
108/// When it receives a callback from a button it edits the message with all
109/// those buttons writing a text with the selected Debian version.
110///
111/// **IMPORTANT**: do not send privacy-sensitive data this way!!!
112/// Anyone can read data stored in the callback button.
113async fn callback_handler(bot: Bot, q: CallbackQuery) -> Result<(), Box<dyn Error + Send + Sync>> {
114    if let Some(ref version) = q.data {
115        let text = format!("You chose: {version}");
116
117        // Tell telegram that we've seen this query, to remove 🕑 icons from the
118        // clients. You could also use `answer_callback_query`'s optional
119        // parameters to tweak what happens on the client side.
120        bot.answer_callback_query(q.id.clone()).await?;
121
122        // Edit text of the message to which the buttons were attached
123        if let Some(message) = q.regular_message() {
124            bot.edit_text(message, text).await?;
125        } else if let Some(id) = q.inline_message_id {
126            bot.edit_message_text_inline(id).text(text).await?;
127        }
128
129        log::info!("You chose: {version}");
130    }
131
132    Ok(())
133}