diff --git a/CHANGELOG.md b/CHANGELOG.md index 4761c3d0..f53895ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [unreleased] +### Changed + + - Automatically delete a webhook if it was set up in `update_listeners::polling_default` (thereby making it `async`, [issue 319](https://github.com/teloxide/teloxide/issues/319)). + ### Fixed - Remove `reqwest` dependency. It's not needed after the [teloxide-core] integration. + ## [0.4.0] - 2021-03-22 ### Added diff --git a/src/dispatching/dispatcher.rs b/src/dispatching/dispatcher.rs index e7635ebb..93bf8a7e 100644 --- a/src/dispatching/dispatcher.rs +++ b/src/dispatching/dispatcher.rs @@ -233,7 +233,7 @@ where <R as Requester>::GetUpdatesFaultTolerant: Send, { self.dispatch_with_listener( - update_listeners::polling_default(self.requester.clone()), + update_listeners::polling_default(self.requester.clone()).await, LoggingErrorHandler::with_custom_text("An error from the update listener"), ) .await; diff --git a/src/dispatching/repls/commands_repl.rs b/src/dispatching/repls/commands_repl.rs index 249d4861..88a7c33d 100644 --- a/src/dispatching/repls/commands_repl.rs +++ b/src/dispatching/repls/commands_repl.rs @@ -39,7 +39,7 @@ where requester, bot_name, handler, - update_listeners::polling_default(cloned_requester), + update_listeners::polling_default(cloned_requester).await, ) .await; } diff --git a/src/dispatching/repls/dialogues_repl.rs b/src/dispatching/repls/dialogues_repl.rs index 706d26a1..1863af8b 100644 --- a/src/dispatching/repls/dialogues_repl.rs +++ b/src/dispatching/repls/dialogues_repl.rs @@ -36,7 +36,7 @@ where dialogues_repl_with_listener( requester, handler, - update_listeners::polling_default(cloned_requester), + update_listeners::polling_default(cloned_requester).await, ) .await; } diff --git a/src/dispatching/repls/repl.rs b/src/dispatching/repls/repl.rs index 31075f60..3c498696 100644 --- a/src/dispatching/repls/repl.rs +++ b/src/dispatching/repls/repl.rs @@ -31,8 +31,12 @@ where <R as Requester>::GetUpdatesFaultTolerant: Send, { let cloned_requester = requester.clone(); - repl_with_listener(requester, handler, update_listeners::polling_default(cloned_requester)) - .await; + repl_with_listener( + requester, + handler, + update_listeners::polling_default(cloned_requester).await, + ) + .await; } /// Like [`repl`], but with a custom [`UpdateListener`]. diff --git a/src/dispatching/update_listeners.rs b/src/dispatching/update_listeners.rs index a3ec5097..bdece104 100644 --- a/src/dispatching/update_listeners.rs +++ b/src/dispatching/update_listeners.rs @@ -120,11 +120,16 @@ impl<S, E> UpdateListener<E> for S where S: Stream<Item = Result<Update, E>> {} /// Returns a long polling update listener with `timeout` of 10 seconds. /// /// See also: [`polling`](polling). -pub fn polling_default<R>(requester: R) -> impl UpdateListener<R::Err> +/// +/// ## Notes +/// +/// This function will automatically delete a webhook if it was set up. +pub async fn polling_default<R>(requester: R) -> impl UpdateListener<R::Err> where R: Requester, <R as Requester>::GetUpdatesFaultTolerant: Send, { + delete_webhook_if_setup(&requester).await; polling(requester, Some(Duration::from_secs(10)), None, None) } @@ -200,3 +205,24 @@ where ) .flatten() } + +async fn delete_webhook_if_setup<R>(requester: &R) +where + R: Requester, +{ + let webhook_info = match requester.get_webhook_info().send().await { + Ok(ok) => ok, + Err(e) => { + log::error!("Failed to get webhook info: {:?}", e); + return; + } + }; + + let is_webhook_setup = !webhook_info.url.is_empty(); + + if is_webhook_setup { + if let Err(e) = requester.delete_webhook().send().await { + log::error!("Failed to delete a webhook: {:?}", e); + } + } +}