Buttons: use message.chat.id, instead of passing it via callback data

This commit is contained in:
Maybe Waffle 2021-10-16 10:12:21 +03:00
parent ef7eda54fe
commit 51fcc5151c

View file

@ -18,7 +18,7 @@ enum Command {
} }
/// Creates a keyboard made by buttons in a big column. /// Creates a keyboard made by buttons in a big column.
fn make_keyboard(chat_id: i64) -> InlineKeyboardMarkup { fn make_keyboard() -> InlineKeyboardMarkup {
let mut keyboard_array: Vec<Vec<InlineKeyboardButton>> = vec![]; let mut keyboard_array: Vec<Vec<InlineKeyboardButton>> = vec![];
// The column is made by the list of Debian versions. // The column is made by the list of Debian versions.
let debian_versions = vec![ let debian_versions = vec![
@ -29,8 +29,8 @@ fn make_keyboard(chat_id: i64) -> InlineKeyboardMarkup {
for version in debian_versions { for version in debian_versions {
// Match each button with the chat id and the Debian version. // Match each button with the chat id and the Debian version.
keyboard_array.push(vec![InlineKeyboardButton::callback( keyboard_array.push(vec![InlineKeyboardButton::callback(
version.into(), version.to_owned(),
format!("{}_{}", chat_id, version), version.to_owned(),
)]); )]);
} }
@ -52,7 +52,7 @@ async fn message_handler(
cx.answer(Command::descriptions()).await?; cx.answer(Command::descriptions()).await?;
} }
Command::Start => { Command::Start => {
let keyboard = make_keyboard(cx.chat_id()); let keyboard = make_keyboard();
// Create a list of buttons using callbacks to receive the response. // Create a list of buttons using callbacks to receive the response.
cx.answer("Debian versions:").reply_markup(keyboard).await?; cx.answer("Debian versions:").reply_markup(keyboard).await?;
} }
@ -70,15 +70,11 @@ async fn callback_handler(
cx: UpdateWithCx<AutoSend<Bot>, CallbackQuery>, cx: UpdateWithCx<AutoSend<Bot>, CallbackQuery>,
) -> Result<(), Box<dyn Error + Send + Sync>> { ) -> Result<(), Box<dyn Error + Send + Sync>> {
let data = &cx.update.data; let data = &cx.update.data;
if let Some(text) = data { if let Some(version) = data {
let callback: Vec<&str> = text.split('_').collect(); let message = cx.update.message.clone().unwrap();
let chat_id = callback[0];
let version = callback[1];
let message_id = cx.update.message.clone().unwrap().id;
let _ = cx let _ = cx
.requester .requester
.edit_message_text(chat_id.to_string(), message_id, format!("You chose: {}", version)) .edit_message_text(message.chat.id, message.id, format!("You chose: {}", version))
.await; .await;
log::info!("You chose: {}", version); log::info!("You chose: {}", version);
} }