Skip to content

Commit

Permalink
Add support to inline callback query (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrielFR authored Nov 7, 2024
1 parent b8b5c68 commit 848064a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
26 changes: 22 additions & 4 deletions lib/grammers-client/src/client/bots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,22 @@ impl Client {
InlineResultIter::new(self, bot.into(), query)
}

/// Edits an inline message sent by a bot.
///
/// Similar to [`Client::send_message`], advanced formatting can be achieved with the
/// options offered by [`InputMessage`].
///
/// [`InputMessage`]: crate::InputMessage
pub async fn edit_inline_message<M: Into<InputMessage>>(
&self,
message_id: tl::enums::InputBotInlineMessageId,
input_message: M,
) -> Result<bool, InvocationError> {
let message: InputMessage = input_message.into();
let entities = parse_mention_entities(self, message.entities);
let dc_id = message_id.dc_id();
let result = self
.invoke_in_dc(
let result = if message.media.is_some() {
let dc_id = message_id.dc_id();
self.invoke_in_dc(
&tl::functions::messages::EditInlineBotMessage {
id: message_id,
message: Some(message.text),
Expand All @@ -174,7 +180,19 @@ impl Client {
},
dc_id,
)
.await?;
.await?
} else {
self.invoke(&tl::functions::messages::EditInlineBotMessage {
id: message_id,
message: Some(message.text),
media: None,
entities,
no_webpage: !message.link_preview,
reply_markup: message.reply_markup,
invert_media: message.invert_media,
})
.await?
};
Ok(result)
}
}
48 changes: 43 additions & 5 deletions lib/grammers-client/src/types/callback_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct CallbackQuery {
pub raw: tl::types::UpdateBotCallbackQuery,
pub(crate) client: Client,
pub(crate) chats: Arc<types::ChatMap>,
pub(crate) inline_msg_id: Option<tl::enums::InputBotInlineMessageId>,
}

/// A callback query answer builder.
Expand All @@ -44,6 +45,30 @@ impl CallbackQuery {
raw: query,
client: client.clone(),
chats: chats.clone(),
inline_msg_id: None,
}
}

pub fn from_inline_raw(
client: &Client,
query: tl::types::UpdateInlineBotCallbackQuery,
chats: &Arc<types::ChatMap>,
) -> Self {
Self {
raw: tl::types::UpdateBotCallbackQuery {
query_id: query.query_id,
user_id: query.user_id,
peer: tl::enums::Peer::User(tl::types::PeerUser {
user_id: query.user_id,
}),
msg_id: 0,
chat_instance: query.chat_instance,
data: query.data,
game_short_name: query.game_short_name,
},
client: client.clone(),
chats: chats.clone(),
inline_msg_id: Some(query.msg_id),
}
}

Expand Down Expand Up @@ -76,6 +101,11 @@ impl CallbackQuery {
self.raw.data.as_deref().unwrap()
}

/// Whether the callback query was generated from an inline message.
pub fn is_from_inline(&self) -> bool {
self.inline_msg_id.is_some()
}

/// Load the `Message` that contains the pressed inline button.
pub async fn load_message(&self) -> Result<types::Message, InvocationError> {
Ok(self
Expand Down Expand Up @@ -142,11 +172,19 @@ impl<'a> Answer<'a> {
pub async fn edit<M: Into<InputMessage>>(self, new_message: M) -> Result<(), InvocationError> {
self.query.client.invoke(&self.request).await?;
let chat = self.query.chat();
let msg_id = self.query.raw.msg_id;
self.query
.client
.edit_message(chat, msg_id, new_message)
.await
if let Some(ref msg_id) = self.query.inline_msg_id {
self.query
.client
.edit_inline_message(msg_id.clone(), new_message)
.await
.map(drop)
} else {
let msg_id = self.query.raw.msg_id;
self.query
.client
.edit_message(chat, msg_id, new_message)
.await
}
}

/// [`Self::send`] the answer, and also respond in the chat where the button was clicked.
Expand Down
5 changes: 5 additions & 0 deletions lib/grammers-client/src/types/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ impl Update {
CallbackQuery::from_raw(client, query, chats),
)),

// InlineCallbackQuery
tl::enums::Update::InlineBotCallbackQuery(query) => Some(Self::CallbackQuery(
CallbackQuery::from_inline_raw(client, query, chats),
)),

// InlineQuery
tl::enums::Update::BotInlineQuery(query) => Some(Self::InlineQuery(
InlineQuery::from_raw(client, query, chats),
Expand Down

0 comments on commit 848064a

Please sign in to comment.