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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
use std::ops::Not;
use serde::{Deserialize, Serialize};
use crate::types::{ChatAdministratorRights, Integer, PollType, True, WebAppInfo};
#[cfg(test)]
mod tests;
/// Represents a custom keyboard with reply options.
#[derive(Clone, Debug, Default, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct ReplyKeyboardMarkup {
keyboard: Vec<Vec<KeyboardButton>>,
#[serde(default, skip_serializing_if = "Not::not")]
one_time_keyboard: bool,
#[serde(default, skip_serializing_if = "Not::not")]
resize_keyboard: bool,
#[serde(default, skip_serializing_if = "Not::not")]
selective: bool,
#[serde(skip_serializing_if = "Option::is_none")]
input_field_placeholder: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
is_persistent: Option<bool>,
}
impl ReplyKeyboardMarkup {
/// Adds a row to the markup.
///
/// # Arguments
///
/// * `value` - The row to add.
pub fn add_row<T>(mut self, value: T) -> Self
where
T: IntoIterator<Item = KeyboardButton>,
{
self.keyboard.push(value.into_iter().collect());
self
}
/// Sets a new input field placeholder.
///
/// # Arguments
///
/// * `value` - The placeholder to be shown
/// in the input field
/// when the keyboard is active;
/// 1-64 characters.
pub fn with_input_field_placeholder<T>(mut self, value: T) -> Self
where
T: Into<String>,
{
self.input_field_placeholder = Some(value.into());
self
}
/// Sets a new value for an `one_time_keyboard` flag.
///
/// # Arguments
///
/// * `value` - Indicates whether to request clients to hide the keyboard
/// as soon as it's been used; default - false.
///
/// The keyboard will still be available, but clients will automatically
/// display the usual letter-keyboard in the chat – the user
/// can press a special button in the input field to see the custom keyboard again.
pub fn with_one_time_keyboard(mut self, value: bool) -> Self {
self.one_time_keyboard = value;
self
}
/// Sets a new value for an `is_persistent` flag.
///
/// # Arguments
///
/// * `value` - Indicates whether to request clients to always show
/// the keyboard when the regular keyboard is hidden.
///
/// Defaults to false, in which case the custom keyboard can be hidden
/// and opened with a keyboard icon.
pub fn with_is_persistent(mut self, value: bool) -> Self {
self.is_persistent = Some(value);
self
}
/// Sets a new value for a `resize_keyboard` flag.
///
/// # Arguments
///
/// * `value` - Indicates whether to request clients
/// to resize the keyboard vertically for optimal fit.
///
/// E.g., make the keyboard smaller if there are just two rows of buttons.
/// Defaults to false, in which case the custom keyboard
/// is always of the same height as the app's standard keyboard.
pub fn with_resize_keyboard(mut self, value: bool) -> Self {
self.resize_keyboard = value;
self
}
/// Sets a new value for a `selective` flag.
///
/// # Arguments
///
/// * `value` - Indicates whether to show the keyboard to specific users only.
///
/// Targets:
///
/// 1. users that are @mentioned in the text of the [`crate::types::Message`] object.
/// 2. if the bot message is a reply (has `reply_to_message_id`), sender of the original message.
///
/// Example: A user requests to change the bot‘s language,
/// bot replies to the request with a keyboard to select the new language.
/// Other users in the group don’t see the keyboard.
pub fn with_selective(mut self, value: bool) -> Self {
self.selective = value;
self
}
}
impl<A, B> From<A> for ReplyKeyboardMarkup
where
A: IntoIterator<Item = B>,
B: IntoIterator<Item = KeyboardButton>,
{
fn from(value: A) -> ReplyKeyboardMarkup {
Self {
keyboard: value.into_iter().map(|x| x.into_iter().collect()).collect(),
one_time_keyboard: false,
resize_keyboard: false,
selective: false,
input_field_placeholder: None,
is_persistent: None,
}
}
}
/// Represents a button of the reply keyboard.
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct KeyboardButton {
text: String,
#[serde(flatten)]
#[serde(skip_serializing_if = "Option::is_none")]
button_type: Option<KeyboardButtonType>,
}
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
#[allow(clippy::enum_variant_names)]
#[serde(rename_all = "snake_case")]
enum KeyboardButtonType {
RequestChat(KeyboardButtonRequestChat),
RequestContact(True),
RequestLocation(True),
RequestPoll(KeyboardButtonPollType),
RequestUsers(KeyboardButtonRequestUsers),
WebApp(WebAppInfo),
}
impl KeyboardButton {
/// Creates a new `KeyboardButton`.
///
/// # Arguments
///
/// * `text` - Text of the button.
///
/// If none of the optional fields are used,
/// it will be sent as a message when the button is pressed.
pub fn new<T>(text: T) -> Self
where
T: Into<String>,
{
Self {
text: text.into(),
button_type: None,
}
}
/// Changes button type to a chat request.
///
/// # Arguments
///
/// * `value` - Criteria used to request a suitable chat.
///
/// If specified, pressing the button will open a list of suitable chats.
/// Tapping on a chat will send its identifier
/// to the bot in a [`crate::types::MessageData::ChatShared`] message.
///
/// Available in private chats only.
pub fn with_request_chat(mut self, value: KeyboardButtonRequestChat) -> Self {
self.button_type = Some(KeyboardButtonType::RequestChat(value));
self
}
/// Changes button type to a contact request.
///
/// The user's phone number will be sent as a contact when the button is pressed.
///
/// Available in private chats only.
pub fn with_request_contact(mut self) -> Self {
self.button_type = Some(KeyboardButtonType::RequestContact(True));
self
}
/// Changes button type to a location request.
///
/// The user's current location will be sent when the button is pressed.
///
/// Available in private chats only.
pub fn with_request_location(mut self) -> Self {
self.button_type = Some(KeyboardButtonType::RequestLocation(True));
self
}
/// Changes button type to a poll request.
///
/// # Arguments
///
/// * `value` - Type of a poll.
///
/// The user will be asked to create a poll and send it to the bot when the button is pressed.
/// If quiz is passed, the user will be allowed to create only polls in the quiz mode.
/// If regular is passed, only regular polls will be allowed.
/// Otherwise, the user will be allowed to create a poll of any type.
///
/// Available in private chats only.
pub fn with_request_poll<T>(mut self, button_type: T) -> Self
where
T: Into<KeyboardButtonPollType>,
{
self.button_type = Some(KeyboardButtonType::RequestPoll(button_type.into()));
self
}
/// Changes button type to a user request.
///
/// # Arguments
///
/// * `value` - Criteria used to request a suitable user.
///
/// If specified, pressing the button will open a list of suitable users.
/// Tapping on any user will send their identifier
/// to the bot in a [`crate::types::MessageData::UsersShared`] message.
///
/// Available in private chats only.
pub fn with_request_users(mut self, value: KeyboardButtonRequestUsers) -> Self {
self.button_type = Some(KeyboardButtonType::RequestUsers(value));
self
}
/// Changes button type to a web app.
///
/// # Arguments
///
/// * `value` - The Web App that will be launched when the button is pressed.
///
/// The Web App will be able to send a [`crate::types::MessageData::WebAppData`] message.
///
/// Available in private chats only.
pub fn with_web_app(mut self, web_app_info: WebAppInfo) -> Self {
self.button_type = Some(KeyboardButtonType::WebApp(web_app_info));
self
}
}
/// Represents a type of a poll which is allowed to be created
/// and sent when the corresponding button is pressed.
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct KeyboardButtonPollType {
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "type")]
poll_type: Option<PollType>,
}
impl From<PollType> for KeyboardButtonPollType {
fn from(value: PollType) -> Self {
Self { poll_type: Some(value) }
}
}
impl From<Option<PollType>> for KeyboardButtonPollType {
fn from(value: Option<PollType>) -> Self {
Self { poll_type: value }
}
}
/// Represents a criteria used to request a suitable chat.
///
/// The identifier of the selected chat will be shared with
/// the bot when the corresponding button is pressed.
///
/// [More about requesting chats](https://core.telegram.org/bots/features#chat-and-user-selection)
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct KeyboardButtonRequestChat {
request_id: Integer,
chat_is_channel: bool,
#[serde(skip_serializing_if = "Option::is_none")]
bot_administrator_rights: Option<ChatAdministratorRights>,
#[serde(skip_serializing_if = "Option::is_none")]
bot_is_member: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
chat_is_created: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
chat_is_forum: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
chat_has_username: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
request_photo: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
request_title: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
request_username: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
user_administrator_rights: Option<ChatAdministratorRights>,
}
impl KeyboardButtonRequestChat {
/// Creates a new `KeyboardButtonRequestChat`.
///
/// # Arguments
///
/// * `request_id` - Signed 32-bit identifier of the request,
/// which will be received back
/// in the [`crate::types::MessageDataChatShared`] object;
/// must be unique within the message.
/// * `chat_is_channel` - Indicates whether to request a channel chat
/// or a group/supergroup chat.
pub fn new(request_id: Integer, chat_is_channel: bool) -> Self {
Self {
request_id,
chat_is_channel,
bot_administrator_rights: None,
bot_is_member: None,
chat_is_created: None,
chat_is_forum: None,
chat_has_username: None,
request_photo: None,
request_title: None,
request_username: None,
user_administrator_rights: None,
}
}
/// Sets a new bot administrator rights.
///
/// # Arguments
///
/// * `value` - An object listing the required administrator rights of the bot in the chat.
///
/// The rights must be a subset of `user_administrator_rights`.
/// If not specified, no additional restrictions are applied.
pub fn with_bot_administrator_rights(mut self, value: ChatAdministratorRights) -> Self {
self.bot_administrator_rights = Some(value);
self
}
/// Sets a new value for a `bot_is_member` flag.
///
/// # Arguments
///
/// * `value` - Indicates whether to request a chat with the bot as a member.
///
/// If not specified, no additional restrictions are applied.
pub fn with_bot_is_member(mut self, value: bool) -> Self {
self.bot_is_member = Some(value);
self
}
/// Sets a new value for a `chat_is_created` flag.
///
/// # Arguments
///
/// * `value` - Indicates whether to request a chat owned by the user.
///
/// If not specified, no additional restrictions are applied.
pub fn with_chat_is_created(mut self, value: bool) -> Self {
self.chat_is_created = Some(value);
self
}
/// Sets a new value for a `chat_is_forum` flag.
///
/// # Arguments
///
/// * `value` - Indicates whether to request a forum supergroup or a non-forum chat.
///
/// If not specified, no additional restrictions are applied.
pub fn with_chat_is_forum(mut self, value: bool) -> Self {
self.chat_is_forum = Some(value);
self
}
/// Sets a new value for a `chat_has_username` flag.
///
/// # Arguments
///
/// * `value` - Indicates whether to request a supergroup or a channel with a username.
///
/// If not specified, no additional restrictions are applied.
pub fn with_chat_has_username(mut self, value: bool) -> Self {
self.chat_has_username = Some(value);
self
}
/// Sets a new value for a `request_photo` flag.
///
/// # Arguments
///
/// * `value` - Whether to request the chat's photo.
pub fn with_request_photo(mut self, value: bool) -> Self {
self.request_photo = Some(value);
self
}
/// Sets a new value for a `request_title` flag.
///
/// # Arguments
///
/// * `value` - Whether to request the chat's title.
pub fn with_request_title(mut self, value: bool) -> Self {
self.request_title = Some(value);
self
}
/// Sets a new value for a `request_username` flag.
///
/// # Arguments
///
/// * `value` - Whether to request the chat's username.
pub fn with_request_username(mut self, value: bool) -> Self {
self.request_username = Some(value);
self
}
/// Sets a new user administrator rights.
///
/// # Arguments
///
/// * `value` - An object listing the required administrator rights of the user in the chat.
///
/// The rights must be a superset of `bot_administrator_rights`.
/// If not specified, no additional restrictions are applied.
pub fn with_user_administrator_rights(mut self, value: ChatAdministratorRights) -> Self {
self.user_administrator_rights = Some(value);
self
}
}
/// Represents a criteria used to request a suitable user.
///
/// The identifier of the selected user will be shared with
/// the bot when the corresponding button is pressed.
///
/// [More about requesting users](https://core.telegram.org/bots/features#chat-and-user-selection)
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct KeyboardButtonRequestUsers {
request_id: Integer,
#[serde(skip_serializing_if = "Option::is_none")]
max_quantity: Option<Integer>,
#[serde(skip_serializing_if = "Option::is_none")]
request_name: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
request_photo: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
request_username: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
user_is_bot: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
user_is_premium: Option<bool>,
}
impl KeyboardButtonRequestUsers {
/// Creates a new `KeyboardButtonRequestUsers`.
///
/// # Arguments
///
/// * `request_id` - Signed 32-bit identifier of the request,
/// which will be received back
/// in the [`crate::types::MessageDataUsersShared`] object;
/// must be unique within the message.
pub fn new(request_id: Integer) -> Self {
Self {
request_id,
max_quantity: None,
request_name: None,
request_photo: None,
request_username: None,
user_is_bot: None,
user_is_premium: None,
}
}
/// Sets a new max quantity
///
/// # Arguments
///
/// * `value` - The maximum number of users to be selected; 1-10; default - 1.
pub fn with_max_quantity(mut self, value: Integer) -> Self {
self.max_quantity = Some(value);
self
}
/// Sets a new value for the `request_name` flag.
///
/// # Arguments
///
/// * `value` - Whether to request the users' first and last name.
pub fn with_request_name(mut self, value: bool) -> Self {
self.request_name = Some(value);
self
}
/// Sets a new value for the `request_photo` flag.
///
/// # Arguments
///
/// * `value` - Whether to request users' photo.
pub fn with_request_photo(mut self, value: bool) -> Self {
self.request_photo = Some(value);
self
}
/// Sets a new value for the `request_username` flag.
///
/// # Arguments
///
/// * `value` - Whether to request users' username.
pub fn with_request_username(mut self, value: bool) -> Self {
self.request_username = Some(value);
self
}
/// Sets a new value for a `user_is_bot` flag.
///
/// # Arguments
///
/// * `value` - Indicates whether to request a bot or a regular user.
///
/// If not specified, no additional restrictions are applied.
pub fn with_user_is_bot(mut self, value: bool) -> Self {
self.user_is_bot = Some(value);
self
}
/// Sets a new value for a `user_is_premium` flag.
///
/// # Arguments
///
/// * `value` - Indicates whether to request a premium user.
///
/// If not specified, no additional restrictions are applied.
pub fn with_user_is_premium(mut self, value: bool) -> Self {
self.user_is_premium = Some(value);
self
}
}
/// Represents a trigger to remove the custom keyboard.
///
/// User will not be able to summon this keyboard.
/// If you want to hide the keyboard from sight but keep it accessible,
/// use [`ReplyKeyboardMarkup::with_one_time_keyboard`].
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct ReplyKeyboardRemove {
remove_keyboard: bool,
#[serde(skip_serializing_if = "Option::is_none")]
selective: Option<bool>,
}
impl Default for ReplyKeyboardRemove {
fn default() -> Self {
Self {
remove_keyboard: true,
selective: None,
}
}
}
impl ReplyKeyboardRemove {
/// Sets a new value for a `selective` flag.
///
///# Arguments
///
/// * `value` - Indicates whether to remove the keyboard for specific users only.
///
/// Targets:
///
/// 1. users that are @mentioned in the text of the Message object;
/// 2. if the bot message is a reply (has `reply_to_message_id`),
/// sender of the original message
///
/// Example: A user votes in a poll, bot returns confirmation message
/// in reply to the vote and removes the keyboard for that user,
/// while still showing the keyboard with poll options to users who haven't voted yet.
pub fn with_selective(mut self, value: bool) -> Self {
self.selective = Some(value);
self
}
}