From 0ac725ac186a1e053d30274ab2b3d22a6c720e8e Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Thu, 17 Oct 2019 20:34:13 +0600 Subject: [PATCH 1/5] Add AnswerCallbackQuery --- src/bot/api.rs | 11 +++ src/requests/answer_callback_query.rs | 122 ++++++++++++++++++++++++++ src/requests/mod.rs | 2 + 3 files changed, 135 insertions(+) create mode 100644 src/requests/answer_callback_query.rs diff --git a/src/bot/api.rs b/src/bot/api.rs index ea3be1bc..a0dda9cf 100644 --- a/src/bot/api.rs +++ b/src/bot/api.rs @@ -1,4 +1,5 @@ use crate::bot::Bot; +use crate::requests::AnswerCallbackQuery; use crate::{ requests::{ AnswerPreCheckoutQuery, AnswerShippingQuery, EditMessageLiveLocation, @@ -274,4 +275,14 @@ impl Bot { { UnpinChatMessage::new(self, chat_id) } + + pub fn answer_callback_query( + &self, + callback_query_id: S, + ) -> AnswerCallbackQuery + where + S: Into, + { + AnswerCallbackQuery::new(self, callback_query_id) + } } diff --git a/src/requests/answer_callback_query.rs b/src/requests/answer_callback_query.rs new file mode 100644 index 00000000..600cb755 --- /dev/null +++ b/src/requests/answer_callback_query.rs @@ -0,0 +1,122 @@ +use crate::bot::Bot; +use crate::network; +use crate::requests::{Request, ResponseResult}; +use crate::types::True; +use async_trait::async_trait; + +/// Use this method to send answers to callback queries sent from inline +/// keyboards. The answer will be displayed to the user as a notification at the +/// top of the chat screen or as an alert. On success, True is returned. +/// +/// Alternatively, the user can be redirected to the specified Game URL. For +/// this option to work, you must first create a game for your bot via +/// @Botfather and accept the terms. Otherwise, you may use links like +/// t.me/your_bot?start=XXXX that open your bot with a parameter. +#[derive(Debug, Clone, Serialize)] +pub struct AnswerCallbackQuery<'a> { + #[serde(skip_serializing)] + bot: &'a Bot, + + /// Unique identifier for the query to be answered. + callback_query_id: String, + + /// Text of the notification. If not specified, nothing will be shown to + /// the user, 0-200 characters + #[serde(skip_serializing_if = "Option::is_none")] + text: Option, + + /// If true, an alert will be shown by the client instead of a notification + /// at the top of the chat screen. Defaults to false. + #[serde(skip_serializing_if = "Option::is_none")] + show_alert: Option, + + /// URL that will be opened by the user's client. If you have created a + /// Game and accepted the conditions via @Botfather, specify the URL that + /// opens your game – note that this will only work if the query comes from + /// a callback_game button. + #[serde(skip_serializing_if = "Option::is_none")] + url: Option, + + /// The maximum amount of time in seconds that the result of the callback + /// query may be cached client-side. Telegram apps will support caching + /// starting in version 3.14. Defaults to 0. + #[serde(skip_serializing_if = "Option::is_none")] + cache_time: Option, +} + +#[async_trait] +impl Request for AnswerCallbackQuery<'_> { + type Output = True; + + async fn send_boxed(self) -> ResponseResult { + self.send().await + } +} + +impl AnswerCallbackQuery<'_> { + pub async fn send(self) -> ResponseResult { + network::request_json( + self.bot.client(), + self.bot.token(), + "answerCallbackQuery", + &self, + ) + .await + } +} + +impl<'a> AnswerCallbackQuery<'a> { + pub(crate) fn new(bot: &'a Bot, callback_query_id: S) -> Self + where + S: Into, + { + Self { + bot, + callback_query_id: callback_query_id.into(), + text: None, + show_alert: None, + url: None, + cache_time: None, + } + } + + pub fn callback_query_id(mut self, value: S) -> Self + where + S: Into, + { + self.callback_query_id = value.into(); + self + } + + pub fn text(mut self, value: S) -> Self + where + S: Into, + { + self.text = Some(value.into()); + self + } + + pub fn show_alert(mut self, value: B) -> Self + where + B: Into, + { + self.show_alert = Some(value.into()); + self + } + + pub fn url(mut self, value: S) -> Self + where + S: Into, + { + self.url = Some(value.into()); + self + } + + pub fn cache_time(mut self, value: I) -> Self + where + I: Into, + { + self.cache_time = Some(value.into()); + self + } +} diff --git a/src/requests/mod.rs b/src/requests/mod.rs index a1ecef59..ec4b8232 100644 --- a/src/requests/mod.rs +++ b/src/requests/mod.rs @@ -3,6 +3,7 @@ use async_trait::async_trait; use serde::de::DeserializeOwned; +pub use answer_callback_query::*; pub use answer_pre_checkout_query::*; pub use answer_shipping_query::*; pub use edit_message_live_location::*; @@ -37,6 +38,7 @@ pub use unpin_chat_message::*; mod form_builder; mod utils; +mod answer_callback_query; mod answer_pre_checkout_query; mod answer_shipping_query; mod edit_message_live_location; From 6f28f28a4ef0d173dbcb38b3bb2339e877251568 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Thu, 17 Oct 2019 20:51:09 +0600 Subject: [PATCH 2/5] Add DeleteChatStickerSet --- src/bot/api.rs | 9 +++- src/requests/delete_chat_sticker_set.rs | 61 +++++++++++++++++++++++++ src/requests/mod.rs | 2 + 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/requests/delete_chat_sticker_set.rs diff --git a/src/bot/api.rs b/src/bot/api.rs index a0dda9cf..05704df2 100644 --- a/src/bot/api.rs +++ b/src/bot/api.rs @@ -1,5 +1,5 @@ use crate::bot::Bot; -use crate::requests::AnswerCallbackQuery; +use crate::requests::{AnswerCallbackQuery, DeleteChatStickerSet}; use crate::{ requests::{ AnswerPreCheckoutQuery, AnswerShippingQuery, EditMessageLiveLocation, @@ -285,4 +285,11 @@ impl Bot { { AnswerCallbackQuery::new(self, callback_query_id) } + + pub fn delete_chat_sticker_set(&self, chat_id: C) -> DeleteChatStickerSet + where + C: Into, + { + DeleteChatStickerSet::new(self, chat_id) + } } diff --git a/src/requests/delete_chat_sticker_set.rs b/src/requests/delete_chat_sticker_set.rs new file mode 100644 index 00000000..abb64fb6 --- /dev/null +++ b/src/requests/delete_chat_sticker_set.rs @@ -0,0 +1,61 @@ +use crate::bot::Bot; +use crate::network; +use crate::requests::{Request, ResponseResult}; +use crate::types::{ChatId, True}; +use async_trait::async_trait; + +/// Use this method to delete a group sticker set from a supergroup. The bot +/// must be an administrator in the chat for this to work and must have the +/// appropriate admin rights. Use the field can_set_sticker_set optionally +/// returned in getChat requests to check if the bot can use this method. +/// Returns True on success. +#[derive(Debug, Clone, Serialize)] +pub struct DeleteChatStickerSet<'a> { + #[serde(skip_serializing)] + bot: &'a Bot, + + /// Unique identifier for the target chat or username of the target + /// supergroup (in the format @supergroupusername) + chat_id: ChatId, +} + +#[async_trait] +impl Request for DeleteChatStickerSet<'_> { + type Output = True; + + async fn send_boxed(self) -> ResponseResult { + self.send().await + } +} + +impl DeleteChatStickerSet<'_> { + async fn send(&self) -> ResponseResult { + network::request_json( + self.bot.client(), + self.bot.token(), + "deleteChatStickerSet", + &self, + ) + .await + } +} + +impl<'a> DeleteChatStickerSet<'a> { + pub(crate) fn new(bot: &'a Bot, chat_id: C) -> Self + where + C: Into, + { + Self { + bot, + chat_id: chat_id.into(), + } + } + + pub fn chat_id(mut self, value: C) -> Self + where + C: Into, + { + self.chat_id = value.into(); + self + } +} diff --git a/src/requests/mod.rs b/src/requests/mod.rs index ec4b8232..b5790585 100644 --- a/src/requests/mod.rs +++ b/src/requests/mod.rs @@ -6,6 +6,7 @@ use serde::de::DeserializeOwned; pub use answer_callback_query::*; pub use answer_pre_checkout_query::*; pub use answer_shipping_query::*; +pub use delete_chat_sticker_set::*; pub use edit_message_live_location::*; pub use forward_message::*; pub use get_chat::*; @@ -41,6 +42,7 @@ mod utils; mod answer_callback_query; mod answer_pre_checkout_query; mod answer_shipping_query; +mod delete_chat_sticker_set; mod edit_message_live_location; mod forward_message; mod get_chat; From 82db9b18b244fe5d2486b6a9d3521a58968e82fc Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Thu, 17 Oct 2019 20:56:58 +0600 Subject: [PATCH 3/5] Add SetChatStickerSet --- src/bot/api.rs | 16 +++++- src/requests/mod.rs | 2 + src/requests/set_chat_sticker_set.rs | 78 ++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 src/requests/set_chat_sticker_set.rs diff --git a/src/bot/api.rs b/src/bot/api.rs index 05704df2..dae00bdb 100644 --- a/src/bot/api.rs +++ b/src/bot/api.rs @@ -1,5 +1,7 @@ use crate::bot::Bot; -use crate::requests::{AnswerCallbackQuery, DeleteChatStickerSet}; +use crate::requests::{ + AnswerCallbackQuery, DeleteChatStickerSet, SetChatStickerSet, +}; use crate::{ requests::{ AnswerPreCheckoutQuery, AnswerShippingQuery, EditMessageLiveLocation, @@ -292,4 +294,16 @@ impl Bot { { DeleteChatStickerSet::new(self, chat_id) } + + pub fn set_chat_sticker_set( + &self, + chat_id: C, + sticker_set_name: S, + ) -> SetChatStickerSet + where + C: Into, + S: Into, + { + SetChatStickerSet::new(self, chat_id, sticker_set_name) + } } diff --git a/src/requests/mod.rs b/src/requests/mod.rs index b5790585..ef012de8 100644 --- a/src/requests/mod.rs +++ b/src/requests/mod.rs @@ -32,6 +32,7 @@ pub use send_venue::*; pub use send_video::*; pub use send_video_note::*; pub use send_voice::*; +pub use set_chat_sticker_set::*; pub use stop_message_live_location::*; pub use unban_chat_member::*; pub use unpin_chat_message::*; @@ -68,6 +69,7 @@ mod send_venue; mod send_video; mod send_video_note; mod send_voice; +mod set_chat_sticker_set; mod stop_message_live_location; mod unban_chat_member; mod unpin_chat_message; diff --git a/src/requests/set_chat_sticker_set.rs b/src/requests/set_chat_sticker_set.rs new file mode 100644 index 00000000..14927d0d --- /dev/null +++ b/src/requests/set_chat_sticker_set.rs @@ -0,0 +1,78 @@ +use crate::bot::Bot; +use crate::network; +use crate::requests::{Request, ResponseResult}; +use crate::types::{ChatId, True}; +use async_trait::async_trait; + +/// Use this method to set a new group sticker set for a supergroup. The bot +/// must be an administrator in the chat for this to work and must have the +/// appropriate admin rights. Use the field can_set_sticker_set optionally +/// returned in getChat requests to check if the bot can use this method. +/// Returns True on success. +#[derive(Debug, Clone, Serialize)] +pub struct SetChatStickerSet<'a> { + #[serde(skip_serializing)] + bot: &'a Bot, + + /// Unique identifier for the target chat or username of the target + /// supergroup (in the format @supergroupusername) + chat_id: ChatId, + + /// Name of the sticker set to be set as the group sticker set + sticker_set_name: String, +} + +#[async_trait] +impl Request for SetChatStickerSet<'_> { + type Output = True; + + async fn send_boxed(self) -> ResponseResult { + self.send().await + } +} + +impl SetChatStickerSet<'_> { + async fn send(&self) -> ResponseResult { + network::request_json( + self.bot.client(), + self.bot.token(), + "setChatStickerSet", + &self, + ) + .await + } +} + +impl<'a> SetChatStickerSet<'a> { + pub(crate) fn new( + bot: &'a Bot, + chat_id: C, + sticker_set_name: S, + ) -> Self + where + C: Into, + S: Into, + { + Self { + bot, + chat_id: chat_id.into(), + sticker_set_name: sticker_set_name.into(), + } + } + + pub fn chat_id(mut self, value: C) -> Self + where + C: Into, + { + self.chat_id = value.into(); + self + } + + pub fn sticker_set_name(mut self, value: S) -> Self + where + S: Into, + { + self.sticker_set_name = value.into(); + self + } +} From b3572072ada1840ef64c4b8034bddc7b14e56da7 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Thu, 17 Oct 2019 21:09:46 +0600 Subject: [PATCH 4/5] Add GetChatMember --- src/bot/api.rs | 10 ++++- src/requests/get_chat_member.rs | 71 +++++++++++++++++++++++++++++++++ src/requests/mod.rs | 2 + 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 src/requests/get_chat_member.rs diff --git a/src/bot/api.rs b/src/bot/api.rs index dae00bdb..7eec264c 100644 --- a/src/bot/api.rs +++ b/src/bot/api.rs @@ -1,6 +1,6 @@ use crate::bot::Bot; use crate::requests::{ - AnswerCallbackQuery, DeleteChatStickerSet, SetChatStickerSet, + AnswerCallbackQuery, DeleteChatStickerSet, GetChatMember, SetChatStickerSet, }; use crate::{ requests::{ @@ -306,4 +306,12 @@ impl Bot { { SetChatStickerSet::new(self, chat_id, sticker_set_name) } + + pub fn get_chat_member(&self, chat_id: C, user_id: I) -> GetChatMember + where + C: Into, + I: Into, + { + GetChatMember::new(self, chat_id, user_id) + } } diff --git a/src/requests/get_chat_member.rs b/src/requests/get_chat_member.rs new file mode 100644 index 00000000..293db180 --- /dev/null +++ b/src/requests/get_chat_member.rs @@ -0,0 +1,71 @@ +use crate::bot::Bot; +use crate::network; +use crate::requests::{Request, ResponseResult}; +use crate::types::{ChatId, ChatMember}; +use async_trait::async_trait; + +/// Use this method to get information about a member of a chat. Returns a +/// ChatMember object on success. +#[derive(Debug, Clone, Serialize)] +pub struct GetChatMember<'a> { + #[serde(skip_serializing)] + bot: &'a Bot, + + /// Unique identifier for the target chat or username of the target + /// supergroup or channel (in the format @channelusername) + chat_id: ChatId, + + /// Unique identifier of the target user + user_id: i32, +} + +#[async_trait] +impl Request for GetChatMember<'_> { + type Output = ChatMember; + + async fn send_boxed(self) -> ResponseResult { + self.send().await + } +} + +impl GetChatMember<'_> { + async fn send(&self) -> ResponseResult { + network::request_json( + self.bot.client(), + self.bot.token(), + "getChatMember", + &self, + ) + .await + } +} + +impl<'a> GetChatMember<'a> { + pub(crate) fn new(bot: &'a Bot, chat_id: C, user_id: I) -> Self + where + C: Into, + I: Into, + { + Self { + bot, + chat_id: chat_id.into(), + user_id: user_id.into(), + } + } + + pub fn chat_id(mut self, value: C) -> Self + where + C: Into, + { + self.chat_id = value.into(); + self + } + + pub fn user_id(mut self, value: I) -> Self + where + I: Into, + { + self.user_id = value.into(); + self + } +} diff --git a/src/requests/mod.rs b/src/requests/mod.rs index ef012de8..a6664eec 100644 --- a/src/requests/mod.rs +++ b/src/requests/mod.rs @@ -10,6 +10,7 @@ pub use delete_chat_sticker_set::*; pub use edit_message_live_location::*; pub use forward_message::*; pub use get_chat::*; +pub use get_chat_member::*; pub use get_file::*; pub use get_me::*; pub use get_updates::*; @@ -47,6 +48,7 @@ mod delete_chat_sticker_set; mod edit_message_live_location; mod forward_message; mod get_chat; +mod get_chat_member; mod get_file; mod get_me; mod get_updates; From 12d944aeb7b325132678e3a6d227228b7f11a837 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Thu, 17 Oct 2019 21:13:50 +0600 Subject: [PATCH 5/5] Add GetChatMembersCount --- src/bot/api.rs | 10 ++++- src/requests/get_chat_members_count.rs | 61 ++++++++++++++++++++++++++ src/requests/mod.rs | 2 + 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/requests/get_chat_members_count.rs diff --git a/src/bot/api.rs b/src/bot/api.rs index 7eec264c..ae1c26d9 100644 --- a/src/bot/api.rs +++ b/src/bot/api.rs @@ -1,6 +1,7 @@ use crate::bot::Bot; use crate::requests::{ - AnswerCallbackQuery, DeleteChatStickerSet, GetChatMember, SetChatStickerSet, + AnswerCallbackQuery, DeleteChatStickerSet, GetChatMember, + GetChatMembersCount, SetChatStickerSet, }; use crate::{ requests::{ @@ -314,4 +315,11 @@ impl Bot { { GetChatMember::new(self, chat_id, user_id) } + + pub fn get_chat_members_count(&self, chat_id: C) -> GetChatMembersCount + where + C: Into, + { + GetChatMembersCount::new(self, chat_id) + } } diff --git a/src/requests/get_chat_members_count.rs b/src/requests/get_chat_members_count.rs new file mode 100644 index 00000000..3595cf49 --- /dev/null +++ b/src/requests/get_chat_members_count.rs @@ -0,0 +1,61 @@ +use async_trait::async_trait; + +use crate::bot::Bot; +use crate::{ + network, + requests::{Request, ResponseResult}, + types::{Chat, ChatId}, +}; + +/// Use this method to get the number of members in a chat. Returns Int on +/// success. +#[derive(Debug, Clone, Serialize)] +pub struct GetChatMembersCount<'a> { + #[serde(skip_serializing)] + bot: &'a Bot, + + /// Unique identifier for the target chat or username + /// of the target supergroup or channel (in the format @channelusername) + chat_id: ChatId, +} + +#[async_trait] +impl Request for GetChatMembersCount<'_> { + type Output = Chat; + + async fn send_boxed(self) -> ResponseResult { + self.send().await + } +} + +impl GetChatMembersCount<'_> { + pub async fn send(self) -> ResponseResult { + network::request_json( + self.bot.client(), + self.bot.token(), + "getChatMembersCount", + &self, + ) + .await + } +} + +impl<'a> GetChatMembersCount<'a> { + pub fn new(bot: &'a Bot, chat_id: C) -> Self + where + C: Into, + { + Self { + bot, + chat_id: chat_id.into(), + } + } + + pub fn chat_id(mut self, value: C) -> Self + where + C: Into, + { + self.chat_id = value.into(); + self + } +} diff --git a/src/requests/mod.rs b/src/requests/mod.rs index a6664eec..4bef49d3 100644 --- a/src/requests/mod.rs +++ b/src/requests/mod.rs @@ -11,6 +11,7 @@ pub use edit_message_live_location::*; pub use forward_message::*; pub use get_chat::*; pub use get_chat_member::*; +pub use get_chat_members_count::*; pub use get_file::*; pub use get_me::*; pub use get_updates::*; @@ -49,6 +50,7 @@ mod edit_message_live_location; mod forward_message; mod get_chat; mod get_chat_member; +mod get_chat_members_count; mod get_file; mod get_me; mod get_updates;