From 8f18b5ba02f966458453153d942588a6553be672 Mon Sep 17 00:00:00 2001 From: P0lunin Date: Wed, 11 Sep 2019 19:52:53 +0300 Subject: [PATCH] refactored, fixed compiling errors and added docs --- src/core/types/force_reply.rs | 15 +++++++--- src/core/types/inline_keyboard_button.rs | 38 ++++++++++++++++++------ src/core/types/inline_keyboard_markup.rs | 12 ++++++-- src/core/types/keyboard_button.rs | 22 ++++++++++---- src/core/types/mod.rs | 14 +++++++++ src/core/types/reply_keyboard_markup.rs | 38 +++++++++++++++++++----- src/core/types/reply_keyboard_remove.rs | 22 ++++++++++---- 7 files changed, 128 insertions(+), 33 deletions(-) diff --git a/src/core/types/force_reply.rs b/src/core/types/force_reply.rs index 6709a0cb..4daaeb50 100644 --- a/src/core/types/force_reply.rs +++ b/src/core/types/force_reply.rs @@ -3,9 +3,16 @@ /// selected the bot‘s message and tapped ’Reply'). This can be /// extremely useful if you want to create user-friendly step-by-step /// interfaces without having to sacrifice privacy mod -#[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone)] +#[derive(Debug, Serialize, Deserialize, Hash, PartialEq, Eq, Clone)] pub struct ForceReply { - pub force_reply: True, - #[serde(skip_serializing_if = "Not::not")] - pub selective: bool, + /// Shows reply interface to the user, as if they manually selected the + /// bot‘s message and tapped ’Reply' + pub force_reply: bool, + + #[serde(skip_serializing_if = "Option::is_none")] + /// Optional. Use this parameter if you want to force reply from specific + /// users only. Targets: 1) users that are @mentioned in the text of the + /// [`Message`] object; 2) if the bot's message is a reply + /// (has reply_to_message_id), sender of the original message. + pub selective: Option, } \ No newline at end of file diff --git a/src/core/types/inline_keyboard_button.rs b/src/core/types/inline_keyboard_button.rs index e06b149b..2d836da2 100644 --- a/src/core/types/inline_keyboard_button.rs +++ b/src/core/types/inline_keyboard_button.rs @@ -1,19 +1,39 @@ /// This object represents one button of an inline keyboard. -#[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone)] +#[derive(Debug, Serialize, Deserialize, Hash, PartialEq, Eq, Clone)] pub struct InlineKeyboardButton { + /// Label text on the button pub text: String, + #[serde(flatten)] pub kind: InlineKeyboardButtonKind, } -#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Clone)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Eq, Hash, Deserialize)] +#[serde(rename_all = "snake_case")] pub enum InlineKeyboardButtonKind { - #[serde(rename = "url")] + /// HTTP or tg:// url to be opened when button is pressed Url(String), - // TODO(knsd): Url? - #[serde(rename = "callback_data")] - CallbackData(String), // TODO(knsd) Validate size? - // SwitchInlineQuery(String), - // SwitchInlineQueryCurrentChat(String), - // CallbackGame(CallbackGame), + /// Data to be sent in a callback query to the bot when button is pressed, + /// 1-64 bytes + CallbackData(String), + /// If set, pressing the button will prompt the user to select one of their + /// chats, open that chat and insert the bot‘s username and the specified + /// inline query in the input field. Can be empty, in which case just the + /// bot’s username will be inserted. + /// + /// Note: This offers an easy way for users to start using your bot in + /// inline mode when they are currently in a private chat with it. + /// Especially useful when combined with switch_pm… actions – in this case + /// the user will be automatically returned to the chat they switched from, + /// skipping the chat selection screen. + SwitchInlineQuery(String), + /// Optional. If set, pressing the button will insert the bot‘s username and + /// the specified inline query in the current chat's input field. Can be + /// empty, in which case only the bot’s username will be inserted. + /// + ///This offers a quick way for the user to open your bot in inline mode in + /// the same chat – good for selecting something from multiple options. + SwitchInlineQueryCurrentChat(String), + // CallbackGame(CallbackGame), TODO: разобраться, что с этим делать + // TODO: add LoginUrl, pay } \ No newline at end of file diff --git a/src/core/types/inline_keyboard_markup.rs b/src/core/types/inline_keyboard_markup.rs index eb816ab2..77f8eee7 100644 --- a/src/core/types/inline_keyboard_markup.rs +++ b/src/core/types/inline_keyboard_markup.rs @@ -1,5 +1,13 @@ -/// This object represents an inline keyboard that appears right next to the message it belongs to. -#[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone)] +use crate::core::types::InlineKeyboardButton; + +/// This object represents an inline keyboard that appears right next to the +/// message it belongs to. +/// +/// *Note*: This will only work in Telegram versions released after +/// 9 April, 2016. Older clients will display unsupported message. +#[derive(Debug, Serialize, Deserialize, Hash, PartialEq, Eq, Clone)] pub struct InlineKeyboardMarkup { + /// Array of button rows, each represented by an Array of + /// [`InlineKeyboardButton`] objects pub inline_keyboard: Vec>, } diff --git a/src/core/types/keyboard_button.rs b/src/core/types/keyboard_button.rs index 3f253427..712743d2 100644 --- a/src/core/types/keyboard_button.rs +++ b/src/core/types/keyboard_button.rs @@ -1,9 +1,19 @@ -/// This object represents one button of the reply keyboard. -#[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone)] +/// This object represents one button of the reply keyboard. For simple text +/// buttons String can be used instead of this object to specify text of the +/// button. Optional fields are mutually exclusive. +#[derive(Debug, Serialize, Deserialize, Hash, PartialEq, Eq, Clone)] pub struct KeyboardButton { + /// 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 text: String, - #[serde(skip_serializing_if = "Not::not")] - pub request_contact: bool, - #[serde(skip_serializing_if = "Not::not")] - pub request_location: bool, + + #[serde(skip_serializing_if = "Option::is_none")] + /// Optional. If True, the user's phone number will be sent as a contact + /// when the button is pressed. Available in private chats only + pub request_contact: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + /// Optional. If True, the user's current location will be sent when the + /// button is pressed. Available in private chats only + pub request_location: Option, } \ No newline at end of file diff --git a/src/core/types/mod.rs b/src/core/types/mod.rs index 8db6e52d..e1d3921c 100644 --- a/src/core/types/mod.rs +++ b/src/core/types/mod.rs @@ -29,6 +29,13 @@ pub use self::{ successful_payment::SuccessfulPayment, user::User, video::Video, + reply_markup::ReplyMarkup, + force_reply::ForceReply, + inline_keyboard_button::InlineKeyboardButton, + inline_keyboard_markup::InlineKeyboardMarkup, + reply_keyboard_remove::ReplyKeyboardRemove, + reply_keyboard_markup::ReplyKeyboardMarkup, + keyboard_button::KeyboardButton, }; mod answer_pre_checkout_query; @@ -39,9 +46,13 @@ mod chat_member; mod chat_permissions; mod chat_photo; mod document; +mod force_reply; +mod inline_keyboard_markup; +mod inline_keyboard_button; mod input_file; mod input_media; mod invoice; +mod keyboard_button; mod label_price; mod message; mod message_entity; @@ -51,6 +62,9 @@ mod parse_mode; mod photo_size; mod pre_checkout_query; mod response_parameters; +mod reply_markup; +mod reply_keyboard_markup; +mod reply_keyboard_remove; mod send_invoice; mod shipping_address; mod shipping_option; diff --git a/src/core/types/reply_keyboard_markup.rs b/src/core/types/reply_keyboard_markup.rs index 2060ad37..ddb73044 100644 --- a/src/core/types/reply_keyboard_markup.rs +++ b/src/core/types/reply_keyboard_markup.rs @@ -1,11 +1,35 @@ +use crate::core::types::KeyboardButton; + /// This object represents a custom keyboard with reply options. -#[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone)] +#[derive(Debug, Serialize, Deserialize, Hash, PartialEq, Eq, Clone)] pub struct ReplyKeyboardMarkup { + /// Array of button rows, each represented by an Array of [`KeyboardButton`] + /// objects pub keyboard: Vec>, - #[serde(skip_serializing_if = "Not::not")] - pub resize_keyboard: bool, - #[serde(skip_serializing_if = "Not::not")] - pub one_time_keyboard: bool, - #[serde(skip_serializing_if = "Not::not")] - pub selective: bool, + + #[serde(skip_serializing_if = "Option::is_none")] + /// Optional. Requests 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 resize_keyboard: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + /// Optional. Requests clients to hide the keyboard as soon as it's been + /// used. 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. Defaults to false. + pub one_time_keyboard: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + /// Optional. Use this parameter if you want to show the keyboard to + /// specific users only. Targets: 1) users that are @mentioned in the text + /// of the [`Message`] object; 2) if the bot's 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 selective: Option, } \ No newline at end of file diff --git a/src/core/types/reply_keyboard_remove.rs b/src/core/types/reply_keyboard_remove.rs index 0671ae61..4797cde7 100644 --- a/src/core/types/reply_keyboard_remove.rs +++ b/src/core/types/reply_keyboard_remove.rs @@ -2,10 +2,22 @@ /// the current custom keyboard and display the default letter-keyboard. /// By default, custom keyboards are displayed until a new keyboard is sent /// by a bot. An exception is made for one-time keyboards that are hidden -/// immediately after the user presses a button (see ReplyKeyboardMarkup). -#[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone)] +/// immediately after the user presses a button (see [`ReplyKeyboardMarkup`]). +#[derive(Debug, Serialize, Deserialize, Hash, PartialEq, Eq, Clone)] pub struct ReplyKeyboardRemove { - pub remove_keyboard: True, - #[serde(skip_serializing_if = "Not::not")] - pub selective: bool, + /// equests clients 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 one_time_keyboard in ReplyKeyboardMarkup) + pub remove_keyboard: bool, + + #[serde(skip_serializing_if = "Option::is_none")] + /// Optional. Use this parameter if you want to show the keyboard to + /// specific users only. Targets: 1) users that are @mentioned in the text + /// of the [`Message`] object; 2) if the bot's 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 selective: Option, } \ No newline at end of file