rustigram_api/methods/
updates.rs1use std::future::{Future, IntoFuture};
2use std::pin::Pin;
3
4use serde::Serialize;
5
6use rustigram_types::update::Update;
7use rustigram_types::webhook::WebhookInfo;
8
9use crate::client::BotClient;
10use crate::error::Result;
11
12#[derive(Serialize, Default)]
15pub struct GetUpdatesParams {
17 #[serde(skip_serializing_if = "Option::is_none")]
19 pub offset: Option<i64>,
20 #[serde(skip_serializing_if = "Option::is_none")]
22 pub limit: Option<u8>,
23 #[serde(skip_serializing_if = "Option::is_none")]
25 pub timeout: Option<u32>,
26 #[serde(skip_serializing_if = "Option::is_none")]
28 pub allowed_updates: Option<Vec<String>>,
29}
30
31pub struct GetUpdates {
33 client: BotClient,
34 params: GetUpdatesParams,
35}
36
37impl GetUpdates {
38 pub(crate) fn new(client: BotClient) -> Self {
39 Self {
40 client,
41 params: GetUpdatesParams::default(),
42 }
43 }
44
45 pub fn offset(mut self, offset: i64) -> Self {
48 self.params.offset = Some(offset);
49 self
50 }
51
52 pub fn limit(mut self, limit: u8) -> Self {
54 self.params.limit = Some(limit.clamp(1, 100));
55 self
56 }
57
58 pub fn timeout(mut self, secs: u32) -> Self {
60 self.params.timeout = Some(secs);
61 self
62 }
63
64 pub fn allowed_updates(mut self, types: Vec<impl Into<String>>) -> Self {
66 self.params.allowed_updates = Some(types.into_iter().map(Into::into).collect());
67 self
68 }
69}
70
71impl IntoFuture for GetUpdates {
72 type Output = Result<Vec<Update>>;
73 type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
74
75 fn into_future(self) -> Self::IntoFuture {
76 Box::pin(async move { self.client.post_json("getUpdates", &self.params).await })
77 }
78}
79
80#[derive(Serialize)]
84pub struct SetWebhookParams {
85 url: String,
86 #[serde(skip_serializing_if = "Option::is_none")]
87 ip_address: Option<String>,
88 #[serde(skip_serializing_if = "Option::is_none")]
89 max_connections: Option<u8>,
90 #[serde(skip_serializing_if = "Option::is_none")]
91 allowed_updates: Option<Vec<String>>,
92 #[serde(skip_serializing_if = "Option::is_none")]
93 drop_pending_updates: Option<bool>,
94 #[serde(skip_serializing_if = "Option::is_none")]
95 secret_token: Option<String>,
96}
97
98pub struct SetWebhook {
100 client: BotClient,
101 params: SetWebhookParams,
102}
103
104impl SetWebhook {
105 pub(crate) fn new(client: BotClient, url: impl Into<String>) -> Self {
106 Self {
107 client,
108 params: SetWebhookParams {
109 url: url.into(),
110 ip_address: None,
111 max_connections: None,
112 allowed_updates: None,
113 drop_pending_updates: None,
114 secret_token: None,
115 },
116 }
117 }
118 pub fn ip_address(mut self, ip: impl Into<String>) -> Self {
120 self.params.ip_address = Some(ip.into());
121 self
122 }
123 pub fn max_connections(mut self, n: u8) -> Self {
125 self.params.max_connections = Some(n.clamp(1, 100));
126 self
127 }
128 pub fn allowed_updates(mut self, types: Vec<impl Into<String>>) -> Self {
130 self.params.allowed_updates = Some(types.into_iter().map(Into::into).collect());
131 self
132 }
133 pub fn drop_pending_updates(mut self, v: bool) -> Self {
135 self.params.drop_pending_updates = Some(v);
136 self
137 }
138 pub fn secret_token(mut self, token: impl Into<String>) -> Self {
141 self.params.secret_token = Some(token.into());
142 self
143 }
144}
145
146impl IntoFuture for SetWebhook {
147 type Output = Result<bool>;
148 type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
149 fn into_future(self) -> Self::IntoFuture {
150 Box::pin(async move { self.client.post_json("setWebhook", &self.params).await })
151 }
152}
153
154#[derive(Serialize, Default)]
157struct DeleteWebhookParams {
158 #[serde(skip_serializing_if = "Option::is_none")]
159 drop_pending_updates: Option<bool>,
160}
161
162pub struct DeleteWebhook {
164 client: BotClient,
165 params: DeleteWebhookParams,
166}
167
168impl DeleteWebhook {
169 pub(crate) fn new(client: BotClient) -> Self {
170 Self {
171 client,
172 params: DeleteWebhookParams::default(),
173 }
174 }
175 pub fn drop_pending_updates(mut self, v: bool) -> Self {
177 self.params.drop_pending_updates = Some(v);
178 self
179 }
180}
181
182impl IntoFuture for DeleteWebhook {
183 type Output = Result<bool>;
184 type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
185 fn into_future(self) -> Self::IntoFuture {
186 Box::pin(async move { self.client.post_json("deleteWebhook", &self.params).await })
187 }
188}
189
190pub struct GetWebhookInfo {
194 client: BotClient,
195}
196
197impl GetWebhookInfo {
198 pub(crate) fn new(client: BotClient) -> Self {
199 Self { client }
200 }
201}
202
203impl IntoFuture for GetWebhookInfo {
204 type Output = Result<WebhookInfo>;
205 type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
206 fn into_future(self) -> Self::IntoFuture {
207 Box::pin(async move {
208 self.client
209 .post_json("getWebhookInfo", &serde_json::json!({}))
210 .await
211 })
212 }
213}