mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
Move webhook deletion in Polling
This commit is contained in:
parent
ad303ef130
commit
22ec6a3c3b
4 changed files with 29 additions and 46 deletions
|
@ -293,7 +293,7 @@ where
|
||||||
R: Requester + Clone,
|
R: Requester + Clone,
|
||||||
<R as Requester>::GetUpdates: Send,
|
<R as Requester>::GetUpdates: Send,
|
||||||
{
|
{
|
||||||
let listener = update_listeners::polling_default(self.bot.clone()).await;
|
let listener = update_listeners::polling_default(self.bot.clone());
|
||||||
let error_handler =
|
let error_handler =
|
||||||
LoggingErrorHandler::with_custom_text("An error from the update listener");
|
LoggingErrorHandler::with_custom_text("An error from the update listener");
|
||||||
|
|
||||||
|
|
|
@ -110,14 +110,11 @@ where
|
||||||
{
|
{
|
||||||
let cloned_bot = bot.clone();
|
let cloned_bot = bot.clone();
|
||||||
|
|
||||||
Box::pin(async move {
|
Box::pin(Self::repl_with_listener(
|
||||||
Self::repl_with_listener(
|
bot,
|
||||||
bot,
|
handler,
|
||||||
handler,
|
update_listeners::polling_default(cloned_bot),
|
||||||
update_listeners::polling_default(cloned_bot).await,
|
))
|
||||||
)
|
|
||||||
.await
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn repl_with_listener<'a, R, H, L, Args>(bot: R, handler: H, listener: L) -> BoxFuture<'a, ()>
|
fn repl_with_listener<'a, R, H, L, Args>(bot: R, handler: H, listener: L) -> BoxFuture<'a, ()>
|
||||||
|
@ -212,13 +209,8 @@ where
|
||||||
let cloned_bot = bot.clone();
|
let cloned_bot = bot.clone();
|
||||||
|
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
commands_repl_with_listener(
|
commands_repl_with_listener(bot, handler, update_listeners::polling_default(cloned_bot), cmd)
|
||||||
bot,
|
.await;
|
||||||
handler,
|
|
||||||
update_listeners::polling_default(cloned_bot).await,
|
|
||||||
cmd,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A [REPL] for commands, with a custom [`UpdateListener`].
|
/// A [REPL] for commands, with a custom [`UpdateListener`].
|
||||||
|
|
|
@ -56,7 +56,7 @@ where
|
||||||
H: Injectable<DependencyMap, ResponseResult<()>, Args> + Send + Sync + 'static,
|
H: Injectable<DependencyMap, ResponseResult<()>, Args> + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
let cloned_bot = bot.clone();
|
let cloned_bot = bot.clone();
|
||||||
repl_with_listener(bot, handler, update_listeners::polling_default(cloned_bot).await).await;
|
repl_with_listener(bot, handler, update_listeners::polling_default(cloned_bot)).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A [REPL] for messages, with a custom [`UpdateListener`].
|
/// A [REPL] for messages, with a custom [`UpdateListener`].
|
||||||
|
|
|
@ -31,6 +31,7 @@ pub struct PollingBuilder<R> {
|
||||||
pub limit: Option<u8>,
|
pub limit: Option<u8>,
|
||||||
pub allowed_updates: Option<Vec<AllowedUpdate>>,
|
pub allowed_updates: Option<Vec<AllowedUpdate>>,
|
||||||
pub drop_pending_updates: bool,
|
pub drop_pending_updates: bool,
|
||||||
|
pub delete_webhook: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R> PollingBuilder<R>
|
impl<R> PollingBuilder<R>
|
||||||
|
@ -85,10 +86,8 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deletes webhook if it was set up.
|
/// Deletes webhook if it was set up.
|
||||||
pub async fn delete_webhook(self) -> Self {
|
pub fn delete_webhook(self) -> Self {
|
||||||
delete_webhook_if_setup(&self.bot).await;
|
Self { delete_webhook: true, ..self }
|
||||||
|
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a long polling update listener with configuration from the
|
/// Returns a long polling update listener with configuration from the
|
||||||
|
@ -96,7 +95,8 @@ where
|
||||||
///
|
///
|
||||||
/// See also: [`polling_default`], [`Polling`].
|
/// See also: [`polling_default`], [`Polling`].
|
||||||
pub fn build(self) -> Polling<R> {
|
pub fn build(self) -> Polling<R> {
|
||||||
let Self { bot, timeout, limit, allowed_updates, drop_pending_updates } = self;
|
let Self { bot, timeout, limit, allowed_updates, drop_pending_updates, delete_webhook } =
|
||||||
|
self;
|
||||||
let (token, flag) = mk_stop_token();
|
let (token, flag) = mk_stop_token();
|
||||||
let polling = Polling {
|
let polling = Polling {
|
||||||
bot,
|
bot,
|
||||||
|
@ -104,6 +104,7 @@ where
|
||||||
limit,
|
limit,
|
||||||
allowed_updates,
|
allowed_updates,
|
||||||
drop_pending_updates,
|
drop_pending_updates,
|
||||||
|
delete_webhook,
|
||||||
flag: Some(flag),
|
flag: Some(flag),
|
||||||
token,
|
token,
|
||||||
};
|
};
|
||||||
|
@ -119,13 +120,12 @@ where
|
||||||
/// ## Notes
|
/// ## Notes
|
||||||
///
|
///
|
||||||
/// This function will automatically delete a webhook if it was set up.
|
/// This function will automatically delete a webhook if it was set up.
|
||||||
pub async fn polling_default<R>(bot: R) -> Polling<R>
|
pub fn polling_default<R>(bot: R) -> Polling<R>
|
||||||
where
|
where
|
||||||
R: Requester + Send + 'static,
|
R: Requester + Send + 'static,
|
||||||
<R as Requester>::GetUpdates: Send,
|
<R as Requester>::GetUpdates: Send,
|
||||||
{
|
{
|
||||||
let polling =
|
let polling = Polling::builder(bot).timeout(Duration::from_secs(10)).delete_webhook().build();
|
||||||
Polling::builder(bot).timeout(Duration::from_secs(10)).delete_webhook().await.build();
|
|
||||||
|
|
||||||
assert_update_listener(polling)
|
assert_update_listener(polling)
|
||||||
}
|
}
|
||||||
|
@ -149,27 +149,6 @@ where
|
||||||
assert_update_listener(builder.build())
|
assert_update_listener(builder.build())
|
||||||
}
|
}
|
||||||
|
|
||||||
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_some();
|
|
||||||
|
|
||||||
if is_webhook_setup {
|
|
||||||
if let Err(e) = requester.delete_webhook().send().await {
|
|
||||||
log::error!("Failed to delete a webhook: {:?}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg_attr(doc, aquamarine::aquamarine)]
|
#[cfg_attr(doc, aquamarine::aquamarine)]
|
||||||
/// A polling update listener.
|
/// A polling update listener.
|
||||||
///
|
///
|
||||||
|
@ -248,6 +227,7 @@ pub struct Polling<B> {
|
||||||
limit: Option<u8>,
|
limit: Option<u8>,
|
||||||
allowed_updates: Option<Vec<AllowedUpdate>>,
|
allowed_updates: Option<Vec<AllowedUpdate>>,
|
||||||
drop_pending_updates: bool,
|
drop_pending_updates: bool,
|
||||||
|
delete_webhook: bool,
|
||||||
flag: Option<StopFlag>,
|
flag: Option<StopFlag>,
|
||||||
token: StopToken,
|
token: StopToken,
|
||||||
}
|
}
|
||||||
|
@ -268,6 +248,7 @@ where
|
||||||
limit: None,
|
limit: None,
|
||||||
allowed_updates: None,
|
allowed_updates: None,
|
||||||
drop_pending_updates: false,
|
drop_pending_updates: false,
|
||||||
|
delete_webhook: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -327,6 +308,16 @@ impl<B: Requester + Send + 'static> UpdateListener for Polling<B> {
|
||||||
// and you need to call `stop_token` *again* after it
|
// and you need to call `stop_token` *again* after it
|
||||||
self.reinit_stop_flag_if_needed();
|
self.reinit_stop_flag_if_needed();
|
||||||
|
|
||||||
|
if self.delete_webhook {
|
||||||
|
let webhook_info = self.bot.get_webhook_info().send().await?;
|
||||||
|
|
||||||
|
let is_webhook_setup = webhook_info.url.is_some();
|
||||||
|
|
||||||
|
if is_webhook_setup {
|
||||||
|
self.bot.delete_webhook().send().await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if self.drop_pending_updates {
|
if self.drop_pending_updates {
|
||||||
self.bot.get_updates().offset(-1).limit(1).timeout(0).await?;
|
self.bot.get_updates().offset(-1).limit(1).timeout(0).await?;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue