1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use std::error::Error;
use teloxide::{
payloads::SendMessageSetters,
prelude::*,
types::{
InlineKeyboardButton, InlineKeyboardMarkup, InlineQueryResultArticle, InputMessageContent,
InputMessageContentText, Me,
},
utils::command::BotCommands,
};
#[derive(BotCommands)]
#[command(rename_rule = "lowercase", description = "These commands are supported:")]
enum Command {
#[command(description = "Display this text")]
Help,
#[command(description = "Start")]
Start,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
pretty_env_logger::init();
log::info!("Starting buttons bot...");
let bot = Bot::from_env();
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));
Dispatcher::builder(bot, handler).enable_ctrlc_handler().build().dispatch().await;
Ok(())
}
fn make_keyboard() -> InlineKeyboardMarkup {
let mut keyboard: Vec<Vec<InlineKeyboardButton>> = vec![];
let debian_versions = [
"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);
}
InlineKeyboardMarkup::new(keyboard)
}
async fn message_handler(
bot: Bot,
msg: Message,
me: Me,
) -> Result<(), Box<dyn Error + Send + Sync>> {
if let Some(text) = msg.text() {
match BotCommands::parse(text, me.username()) {
Ok(Command::Help) => {
bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?;
}
Ok(Command::Start) => {
let keyboard = make_keyboard();
bot.send_message(msg.chat.id, "Debian versions:").reply_markup(keyboard).await?;
}
Err(_) => {
bot.send_message(msg.chat.id, "Command not found!").await?;
}
}
}
Ok(())
}
async fn inline_query_handler(
bot: Bot,
q: InlineQuery,
) -> 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());
bot.answer_inline_query(q.id, vec![choose_debian_version.into()]).await?;
Ok(())
}
async fn callback_handler(bot: Bot, q: CallbackQuery) -> Result<(), Box<dyn Error + Send + Sync>> {
if let Some(version) = q.data {
let text = format!("You chose: {version}");
bot.answer_callback_query(q.id).await?;
if let Some(Message { id, chat, .. }) = q.message {
bot.edit_message_text(chat.id, id, text).await?;
} else if let Some(id) = q.inline_message_id {
bot.edit_message_text_inline(id, text).await?;
}
log::info!("You chose: {}", version);
}
Ok(())
}