From b8579c52d1641d9a3e8edb8c9913ad539c93853e Mon Sep 17 00:00:00 2001 From: Waffle Date: Mon, 30 Dec 2019 23:15:52 +0300 Subject: [PATCH 1/4] Clean `polling` for a bit --- src/dispatching/updaters.rs | 70 ++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/src/dispatching/updaters.rs b/src/dispatching/updaters.rs index 3ee17ec9..750a04b1 100644 --- a/src/dispatching/updaters.rs +++ b/src/dispatching/updaters.rs @@ -115,12 +115,9 @@ impl Updater for S where S: Stream> {} /// Returns a long polling updater with the default configuration. /// -/// It is the same as calling [`polling_advanced`] with `timeout` of 30 seconds, -/// `limit=100` and receive all kinds of updates. -/// -/// [`polling_advanced`]: polling_advanced -pub fn polling_basic(bot: &Bot) -> impl Updater + '_ { - polling_advanced::<&[_]>(bot, Duration::from_secs(30), 100, &[]) +/// [`polling`]: polling +pub fn polling_default(bot: &Bot) -> impl Updater + '_ { + polling(bot, None, None, None) } /// Returns a long/short polling updater with some additional options. @@ -130,38 +127,41 @@ pub fn polling_basic(bot: &Bot) -> impl Updater + '_ { /// - `limit`: Limits the number of updates to be retrieved at once. Values /// between 1—100 are accepted. /// - `allowed_updates`: A list the types of updates you want to receive. -pub fn polling_advanced<'a, A>( - bot: &'a Bot, - timeout: Duration, - limit: u8, - allowed_updates: A, -) -> impl Updater + 'a -where - A: Into<&'a [AllowedUpdate]>, -{ - let mut allowed_updates = Some(allowed_updates.into()); +/// See [`GetUpdates`] for defaults. +/// +/// See also: [`polling_default`](polling_default) +/// +/// [`GetUpdates`]: crate::requests::payloads::GetUpdates +pub fn polling( + bot: &Bot, + timeout: Option, + limit: Option, + allowed_updates: Option>, +) -> impl Updater + '_ { + let timeout = + timeout.map(|t| t.as_secs().try_into().expect("timeout is too big")); - stream::unfold((bot, 0), move |(bot, mut offset)| async move { - let updates = match bot - .get_updates() - .offset(offset) - .timeout(timeout.as_secs().try_into().expect("timeout is too big")) - .limit(limit) - .allowed_updates(allowed_updates.take().unwrap_or(&[])) - .send() - .await - { - Err(err) => vec![Err(err)], - Ok(updates) => { - if let Some(upd) = updates.last() { - offset = upd.id + 1; + stream::unfold( + (allowed_updates, bot, 0), + move |(mut allowed_updates, bot, mut offset)| async move { + let mut req = bot.get_updates().offset(offset); + req.payload.timeout = timeout; + req.payload.limit = limit; + req.payload.allowed_updates = allowed_updates.take(); + + let updates = match req.send().await { + Err(err) => vec![Err(err)], + Ok(updates) => { + if let Some(upd) = updates.last() { + offset = upd.id + 1; + } + updates.into_iter().map(Ok).collect::>() } - updates.into_iter().map(Ok).collect::>() - } - }; + }; - Some((stream::iter(updates), (bot, offset))) - }) + Some((stream::iter(updates), (allowed_updates, bot, offset))) + }, + ) .flatten() } From 49771e2d89e2950febff8e35fc0481e7b75e4109 Mon Sep 17 00:00:00 2001 From: Waffle Date: Mon, 30 Dec 2019 23:26:07 +0300 Subject: [PATCH 2/4] Fix doc test that uses polling --- src/dispatching/dispatchers/filter.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dispatching/dispatchers/filter.rs b/src/dispatching/dispatchers/filter.rs index d5c0b6fb..a6fa842b 100644 --- a/src/dispatching/dispatchers/filter.rs +++ b/src/dispatching/dispatchers/filter.rs @@ -39,7 +39,7 @@ type FiltersWithHandlers<'a, T, E> = Vec>; /// /// use teloxide::{ /// dispatching::{ -/// dispatchers::filter::FilterDispatcher, updaters::polling_basic, +/// dispatchers::filter::FilterDispatcher, updaters::polling_default, /// }, /// types::Message, /// Bot, @@ -65,7 +65,7 @@ type FiltersWithHandlers<'a, T, E> = Vec>; /// .edited_message_handler(true, handle_edited_message); /// /// // Start dispatching updates from long polling -/// dp.dispatch(polling_basic(&bot)).await; +/// dp.dispatch(polling_default(&bot)).await; /// # } /// ``` /// From be43bd6cdcfdc48ccb4bf9a362e07b591d0c4906 Mon Sep 17 00:00:00 2001 From: Waffle Date: Mon, 30 Dec 2019 23:35:58 +0300 Subject: [PATCH 3/4] fix doc --- src/dispatching/updaters.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/dispatching/updaters.rs b/src/dispatching/updaters.rs index 750a04b1..6068fb55 100644 --- a/src/dispatching/updaters.rs +++ b/src/dispatching/updaters.rs @@ -2,8 +2,8 @@ //! //! The key trait here is [`Updater`]. You can get it by these functions: //! -//! - [`polling_basic`], which returns a default long polling updater. -//! - [`polling_advanced`], which returns a long/short polling updater with +//! - [`polling_default`], which returns a default long polling updater. +//! - [`polling`], which returns a long/short polling updater with //! your configuration. //! //! And then you can pass it directly to a dispatcher. @@ -91,8 +91,8 @@ //! updates `0..=N`. //! //! [`Updater`]: Updater -//! [`polling_basic`]: polling_basic -//! [`polling_advanced`]: polling_advanced +//! [`polling_default`]: polling_default +//! [`polling`]: polling //! [`Dispatcher`]: crate::dispatching::Dispatcher::dispatch //! [`Box::get_updates`]: crate::Bot::get_updates //! [getting updates]: https://core.telegram.org/bots/api#getting-updates From 9ee9c24024c95b0f5e671efa7e55649264d0f722 Mon Sep 17 00:00:00 2001 From: Waffle Date: Mon, 30 Dec 2019 23:41:10 +0300 Subject: [PATCH 4/4] fmt :facepalm: --- src/dispatching/updaters.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dispatching/updaters.rs b/src/dispatching/updaters.rs index 6068fb55..0c5248bb 100644 --- a/src/dispatching/updaters.rs +++ b/src/dispatching/updaters.rs @@ -3,8 +3,8 @@ //! The key trait here is [`Updater`]. You can get it by these functions: //! //! - [`polling_default`], which returns a default long polling updater. -//! - [`polling`], which returns a long/short polling updater with -//! your configuration. +//! - [`polling`], which returns a long/short polling updater with your +//! configuration. //! //! And then you can pass it directly to a dispatcher. //!