Buttons: add inline support

This commit is contained in:
Maybe Waffle 2021-10-16 11:07:55 +03:00
parent 776e8be073
commit 31e133493b

View file

@ -2,7 +2,10 @@ use std::error::Error;
use teloxide::{ use teloxide::{
payloads::SendMessageSetters, payloads::SendMessageSetters,
prelude::*, prelude::*,
types::{InlineKeyboardButton, InlineKeyboardMarkup}, types::{
InlineKeyboardButton, InlineKeyboardMarkup, InlineQueryResultArticle, InputMessageContent,
InputMessageContentText,
},
utils::command::BotCommand, utils::command::BotCommand,
}; };
@ -68,17 +71,43 @@ async fn message_handler(
Ok(()) Ok(())
} }
async fn inline_query_handler(
cx: UpdateWithCx<AutoSend<Bot>, InlineQuery>,
) -> Result<(), Box<dyn Error + Send + Sync>> {
let UpdateWithCx { requester: bot, update: query } = cx;
let choose_debian_version = InlineQueryResultArticle::new(
"0",
"Chose debian version",
InputMessageContent::Text(InputMessageContentText::new("Debian versions:")),
)
.reply_markup(make_keyboard());
bot.answer_inline_query(query.id, vec![choose_debian_version.into()]).await?;
Ok(())
}
/// When it receives a callback from a button it edits the message with all /// When it receives a callback from a button it edits the message with all
/// those buttons writing a text with the selected Debian version. /// those buttons writing a text with the selected Debian version.
async fn callback_handler( 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 UpdateWithCx { requester: bot, update: query } = cx; let UpdateWithCx { requester: bot, update: query } = cx;
if let Some(version) = query.data {
let message = query.message.unwrap();
bot.edit_message_text(message.chat.id, message.id, format!("You chose: {}", version)) if let Some(version) = query.data {
.await?; let text = format!("You chose: {}", version);
match query.message {
Some(Message { id, chat, .. }) => {
bot.edit_message_text(chat.id, id, text).await?;
}
None => {
if let Some(id) = query.inline_message_id {
bot.edit_message_text_inline(dbg!(id), text).await?;
}
}
}
log::info!("You chose: {}", version); log::info!("You chose: {}", version);
} }
@ -104,6 +133,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
callback_handler(cx).await.log_on_error().await; callback_handler(cx).await.log_on_error().await;
}) })
}) })
.inline_queries_handler(|rx: DispatcherHandlerRx<AutoSend<Bot>, InlineQuery>| {
UnboundedReceiverStream::new(rx).for_each_concurrent(None, |cx| async move {
inline_query_handler(cx).await.log_on_error().await;
})
})
.dispatch() .dispatch()
.await; .await;