Skip to main content

rust_tg_bot_raw/bot/
gifts_methods.rs

1use super::{push_opt, push_opt_str, Bot, ChatId, Result};
2use crate::request::request_parameter::RequestParameter;
3use crate::types::{gifts, message_entity, owned_gift};
4
5#[allow(dead_code)]
6impl Bot {
7    // ======================================================================
8    // Gifts
9    // ======================================================================
10
11    /// Use this method to get the list of gifts that can be sent by the bot to users.
12    ///
13    /// Calls the Telegram `getAvailableGifts` API method.
14    pub async fn get_available_gifts_raw(&self) -> Result<gifts::Gifts> {
15        self.do_post("getAvailableGifts", Vec::new()).await
16    }
17
18    /// Use this method to send a gift to a user or channel chat.
19    ///
20    /// Calls the Telegram `sendGift` API method.
21    pub async fn send_gift_raw(
22        &self,
23        gift_id: &str,
24        user_id: Option<i64>,
25        chat_id: Option<ChatId>,
26        text: Option<&str>,
27        text_parse_mode: Option<&str>,
28        text_entities: Option<Vec<message_entity::MessageEntity>>,
29        pay_for_upgrade: Option<bool>,
30    ) -> Result<bool> {
31        let mut params = vec![RequestParameter::new(
32            "gift_id",
33            serde_json::Value::String(gift_id.to_owned()),
34        )];
35        push_opt(&mut params, "user_id", &user_id)?;
36        push_opt(&mut params, "chat_id", &chat_id)?;
37        push_opt_str(&mut params, "text", text);
38        push_opt_str(&mut params, "text_parse_mode", text_parse_mode);
39        push_opt(&mut params, "text_entities", &text_entities)?;
40        push_opt(&mut params, "pay_for_upgrade", &pay_for_upgrade)?;
41        self.do_post("sendGift", params).await
42    }
43
44    /// Use this method to gift a Telegram Premium subscription to a user.
45    ///
46    /// Calls the Telegram `giftPremiumSubscription` API method.
47    pub async fn gift_premium_subscription_raw(
48        &self,
49        user_id: i64,
50        month_count: i64,
51        star_count: i64,
52        text: Option<&str>,
53        text_parse_mode: Option<&str>,
54        text_entities: Option<Vec<message_entity::MessageEntity>>,
55    ) -> Result<bool> {
56        let mut params = vec![
57            RequestParameter::new("user_id", serde_json::to_value(user_id)?),
58            RequestParameter::new("month_count", serde_json::to_value(month_count)?),
59            RequestParameter::new("star_count", serde_json::to_value(star_count)?),
60        ];
61        push_opt_str(&mut params, "text", text);
62        push_opt_str(&mut params, "text_parse_mode", text_parse_mode);
63        push_opt(&mut params, "text_entities", &text_entities)?;
64        self.do_post("giftPremiumSubscription", params).await
65    }
66
67    /// Use this method to get the list of gifts received by a user.
68    ///
69    /// Calls the Telegram `getUserGifts` API method.
70    pub async fn get_user_gifts_raw(
71        &self,
72        user_id: i64,
73        exclude_unlimited: Option<bool>,
74        exclude_limited_upgradable: Option<bool>,
75        exclude_limited_non_upgradable: Option<bool>,
76        exclude_from_blockchain: Option<bool>,
77        exclude_unique: Option<bool>,
78        sort_by_price: Option<bool>,
79        offset: Option<&str>,
80        limit: Option<i64>,
81    ) -> Result<owned_gift::OwnedGifts> {
82        let mut params = vec![RequestParameter::new(
83            "user_id",
84            serde_json::to_value(user_id)?,
85        )];
86        push_opt(&mut params, "exclude_unlimited", &exclude_unlimited)?;
87        push_opt(
88            &mut params,
89            "exclude_limited_upgradable",
90            &exclude_limited_upgradable,
91        )?;
92        push_opt(
93            &mut params,
94            "exclude_limited_non_upgradable",
95            &exclude_limited_non_upgradable,
96        )?;
97        push_opt(
98            &mut params,
99            "exclude_from_blockchain",
100            &exclude_from_blockchain,
101        )?;
102        push_opt(&mut params, "exclude_unique", &exclude_unique)?;
103        push_opt(&mut params, "sort_by_price", &sort_by_price)?;
104        push_opt_str(&mut params, "offset", offset);
105        push_opt(&mut params, "limit", &limit)?;
106        self.do_post("getUserGifts", params).await
107    }
108
109    /// Use this method to get the list of gifts received by a chat.
110    ///
111    /// Calls the Telegram `getChatGifts` API method.
112    pub async fn get_chat_gifts_raw(
113        &self,
114        chat_id: ChatId,
115        exclude_unsaved: Option<bool>,
116        exclude_saved: Option<bool>,
117        exclude_unlimited: Option<bool>,
118        exclude_limited_upgradable: Option<bool>,
119        exclude_limited_non_upgradable: Option<bool>,
120        exclude_from_blockchain: Option<bool>,
121        exclude_unique: Option<bool>,
122        sort_by_price: Option<bool>,
123        offset: Option<&str>,
124        limit: Option<i64>,
125    ) -> Result<owned_gift::OwnedGifts> {
126        let mut params = vec![RequestParameter::new(
127            "chat_id",
128            serde_json::to_value(&chat_id)?,
129        )];
130        push_opt(&mut params, "exclude_unsaved", &exclude_unsaved)?;
131        push_opt(&mut params, "exclude_saved", &exclude_saved)?;
132        push_opt(&mut params, "exclude_unlimited", &exclude_unlimited)?;
133        push_opt(
134            &mut params,
135            "exclude_limited_upgradable",
136            &exclude_limited_upgradable,
137        )?;
138        push_opt(
139            &mut params,
140            "exclude_limited_non_upgradable",
141            &exclude_limited_non_upgradable,
142        )?;
143        push_opt(
144            &mut params,
145            "exclude_from_blockchain",
146            &exclude_from_blockchain,
147        )?;
148        push_opt(&mut params, "exclude_unique", &exclude_unique)?;
149        push_opt(&mut params, "sort_by_price", &sort_by_price)?;
150        push_opt_str(&mut params, "offset", offset);
151        push_opt(&mut params, "limit", &limit)?;
152        self.do_post("getChatGifts", params).await
153    }
154}