Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support callback button #279

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions lib/grammers-client/src/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,50 @@ impl Message {
self.raw.reply_markup.clone()
}

/// Collect all inline callback buttons. Those buttons often appeare in bot chat.
/// Buttons could be clicked by [`click_callback_button`]
pub fn callback_buttons(&self) -> Option<Vec<tl::types::KeyboardButtonCallback>> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only handle KeyboardButtonCallback, other KeyboardButton types cannot handle.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking more to return a custom type of buttons, so that .click() could be called directly on the one you need.

By the way, might be good to call this "inline buttons". The term in your code is used by the API, but I believe the other term is more common in the applications? Not sure what the bot API calls it.

let reply_markup = &self.raw.reply_markup?;

let mut rtn = Vec::new();
if let tl::enums::ReplyMarkup::ReplyInlineMarkup(markup) = reply_markup {
for row in markup.rows.iter() {
let tl::enums::KeyboardButtonRow::Row(row) = row;
for b in row.buttons.iter() {
match b {
tl::enums::KeyboardButton::Callback(callback_b) => {
rtn.push(callback_b.clone());
}
_ => (),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means the position in the returned array will not match the one users see in the application. Which would be really confusing.

}
}
}
}

Some(rtn)
}

pub async fn click_callback_button(
&self,
button: &tl::types::KeyboardButtonCallback,
) -> Result<()> {
if button.requires_password {
unimplemented!()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know how to deal with password.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's a bit of a mess. I'd take the implementation from Telethon.

} else {
let rtn = self
.client
.invoke(&tl::functions::GetBotCallbackAnswer {
game: false,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know how to deal with game flag.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise.

peer: self.chat().pack().to_input_peer(),
msg_id: self.id(),
data: Some(button.data.clone()),
password: None,
})
.await?;
}
Ok(())
}

/// The formatting entities used to format this message, such as bold, italic, with their
/// offsets and lengths.
pub fn fmt_entities(&self) -> Option<&Vec<tl::enums::MessageEntity>> {
Expand Down
Loading