Merge pull request #66 from telebofr/requests_gymmasssorla

Add more requests
This commit is contained in:
Temirkhan Myrzamadi 2019-10-17 15:41:52 +00:00 committed by GitHub
commit cf244605ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 451 additions and 0 deletions

View file

@ -1,4 +1,8 @@
use crate::bot::Bot;
use crate::requests::{
AnswerCallbackQuery, DeleteChatStickerSet, GetChatMember,
GetChatMembersCount, SetChatStickerSet,
};
use crate::{
requests::{
AnswerPreCheckoutQuery, AnswerShippingQuery, EditMessageLiveLocation,
@ -281,4 +285,48 @@ impl Bot {
{
UnpinChatMessage::new(self, chat_id)
}
pub fn answer_callback_query<S>(
&self,
callback_query_id: S,
) -> AnswerCallbackQuery
where
S: Into<String>,
{
AnswerCallbackQuery::new(self, callback_query_id)
}
pub fn delete_chat_sticker_set<C>(&self, chat_id: C) -> DeleteChatStickerSet
where
C: Into<ChatId>,
{
DeleteChatStickerSet::new(self, chat_id)
}
pub fn set_chat_sticker_set<C, S>(
&self,
chat_id: C,
sticker_set_name: S,
) -> SetChatStickerSet
where
C: Into<ChatId>,
S: Into<String>,
{
SetChatStickerSet::new(self, chat_id, sticker_set_name)
}
pub fn get_chat_member<C, I>(&self, chat_id: C, user_id: I) -> GetChatMember
where
C: Into<ChatId>,
I: Into<i32>,
{
GetChatMember::new(self, chat_id, user_id)
}
pub fn get_chat_members_count<C>(&self, chat_id: C) -> GetChatMembersCount
where
C: Into<ChatId>,
{
GetChatMembersCount::new(self, chat_id)
}
}

View file

@ -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<String>,
/// 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<bool>,
/// 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<String>,
/// 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<i32>,
}
#[async_trait]
impl Request for AnswerCallbackQuery<'_> {
type Output = True;
async fn send_boxed(self) -> ResponseResult<Self::Output> {
self.send().await
}
}
impl AnswerCallbackQuery<'_> {
pub async fn send(self) -> ResponseResult<True> {
network::request_json(
self.bot.client(),
self.bot.token(),
"answerCallbackQuery",
&self,
)
.await
}
}
impl<'a> AnswerCallbackQuery<'a> {
pub(crate) fn new<S>(bot: &'a Bot, callback_query_id: S) -> Self
where
S: Into<String>,
{
Self {
bot,
callback_query_id: callback_query_id.into(),
text: None,
show_alert: None,
url: None,
cache_time: None,
}
}
pub fn callback_query_id<S>(mut self, value: S) -> Self
where
S: Into<String>,
{
self.callback_query_id = value.into();
self
}
pub fn text<S>(mut self, value: S) -> Self
where
S: Into<String>,
{
self.text = Some(value.into());
self
}
pub fn show_alert<B>(mut self, value: B) -> Self
where
B: Into<bool>,
{
self.show_alert = Some(value.into());
self
}
pub fn url<S>(mut self, value: S) -> Self
where
S: Into<String>,
{
self.url = Some(value.into());
self
}
pub fn cache_time<I>(mut self, value: I) -> Self
where
I: Into<i32>,
{
self.cache_time = Some(value.into());
self
}
}

View file

@ -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::Output> {
self.send().await
}
}
impl DeleteChatStickerSet<'_> {
async fn send(&self) -> ResponseResult<True> {
network::request_json(
self.bot.client(),
self.bot.token(),
"deleteChatStickerSet",
&self,
)
.await
}
}
impl<'a> DeleteChatStickerSet<'a> {
pub(crate) fn new<C>(bot: &'a Bot, chat_id: C) -> Self
where
C: Into<ChatId>,
{
Self {
bot,
chat_id: chat_id.into(),
}
}
pub fn chat_id<C>(mut self, value: C) -> Self
where
C: Into<ChatId>,
{
self.chat_id = value.into();
self
}
}

View file

@ -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::Output> {
self.send().await
}
}
impl GetChatMember<'_> {
async fn send(&self) -> ResponseResult<ChatMember> {
network::request_json(
self.bot.client(),
self.bot.token(),
"getChatMember",
&self,
)
.await
}
}
impl<'a> GetChatMember<'a> {
pub(crate) fn new<C, I>(bot: &'a Bot, chat_id: C, user_id: I) -> Self
where
C: Into<ChatId>,
I: Into<i32>,
{
Self {
bot,
chat_id: chat_id.into(),
user_id: user_id.into(),
}
}
pub fn chat_id<C>(mut self, value: C) -> Self
where
C: Into<ChatId>,
{
self.chat_id = value.into();
self
}
pub fn user_id<I>(mut self, value: I) -> Self
where
I: Into<i32>,
{
self.user_id = value.into();
self
}
}

View file

@ -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::Output> {
self.send().await
}
}
impl GetChatMembersCount<'_> {
pub async fn send(self) -> ResponseResult<Chat> {
network::request_json(
self.bot.client(),
self.bot.token(),
"getChatMembersCount",
&self,
)
.await
}
}
impl<'a> GetChatMembersCount<'a> {
pub fn new<C>(bot: &'a Bot, chat_id: C) -> Self
where
C: Into<ChatId>,
{
Self {
bot,
chat_id: chat_id.into(),
}
}
pub fn chat_id<C>(mut self, value: C) -> Self
where
C: Into<ChatId>,
{
self.chat_id = value.into();
self
}
}

View file

@ -3,11 +3,15 @@
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 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_chat_members_count::*;
pub use get_file::*;
pub use get_me::*;
pub use get_updates::*;
@ -30,6 +34,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 set_chat_description::*;
pub use stop_message_live_location::*;
pub use unban_chat_member::*;
@ -38,11 +43,15 @@ pub use unpin_chat_message::*;
mod form_builder;
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;
mod get_chat_member;
mod get_chat_members_count;
mod get_file;
mod get_me;
mod get_updates;
@ -65,6 +74,7 @@ mod send_venue;
mod send_video;
mod send_video_note;
mod send_voice;
mod set_chat_sticker_set;
mod set_chat_description;
mod stop_message_live_location;
mod unban_chat_member;

View file

@ -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::Output> {
self.send().await
}
}
impl SetChatStickerSet<'_> {
async fn send(&self) -> ResponseResult<True> {
network::request_json(
self.bot.client(),
self.bot.token(),
"setChatStickerSet",
&self,
)
.await
}
}
impl<'a> SetChatStickerSet<'a> {
pub(crate) fn new<C, S>(
bot: &'a Bot,
chat_id: C,
sticker_set_name: S,
) -> Self
where
C: Into<ChatId>,
S: Into<String>,
{
Self {
bot,
chat_id: chat_id.into(),
sticker_set_name: sticker_set_name.into(),
}
}
pub fn chat_id<C>(mut self, value: C) -> Self
where
C: Into<ChatId>,
{
self.chat_id = value.into();
self
}
pub fn sticker_set_name<S>(mut self, value: S) -> Self
where
S: Into<String>,
{
self.sticker_set_name = value.into();
self
}
}