mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-23 06:51:01 +01:00
Some REPLs twiddling
This commit is contained in:
parent
4af85f3d5b
commit
18651a8253
3 changed files with 12 additions and 121 deletions
|
@ -73,23 +73,16 @@ pub async fn commands_repl_with_listener<'a, R, Cmd, H, L, ListenerE, E, Args>(
|
||||||
// commands. See <https://github.com/teloxide/teloxide/issues/557>.
|
// commands. See <https://github.com/teloxide/teloxide/issues/557>.
|
||||||
let ignore_update = |_upd| Box::pin(async {});
|
let ignore_update = |_upd| Box::pin(async {});
|
||||||
|
|
||||||
let mut dispatcher = Dispatcher::builder(
|
Dispatcher::builder(
|
||||||
bot,
|
bot,
|
||||||
Update::filter_message().filter_command::<Cmd>().branch(dptree::endpoint(handler)),
|
Update::filter_message().filter_command::<Cmd>().branch(dptree::endpoint(handler)),
|
||||||
)
|
)
|
||||||
.default_handler(ignore_update)
|
.default_handler(ignore_update)
|
||||||
.build();
|
.build()
|
||||||
|
.setup_ctrlc_handler()
|
||||||
#[cfg(feature = "ctrlc_handler")]
|
.dispatch_with_listener(
|
||||||
dispatcher.setup_ctrlc_handler();
|
listener,
|
||||||
|
LoggingErrorHandler::with_custom_text("An error from the update listener"),
|
||||||
// To make mutable var from immutable.
|
)
|
||||||
let mut dispatcher = dispatcher;
|
.await;
|
||||||
|
|
||||||
dispatcher
|
|
||||||
.dispatch_with_listener(
|
|
||||||
listener,
|
|
||||||
LoggingErrorHandler::with_custom_text("An error from the update listener"),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,96 +0,0 @@
|
||||||
use crate::{
|
|
||||||
dispatching::{
|
|
||||||
dialogue::{DialogueDispatcher, DialogueStage, DialogueWithCx, InMemStorageError},
|
|
||||||
update_listeners,
|
|
||||||
update_listeners::UpdateListener,
|
|
||||||
Dispatcher, UpdateWithCx,
|
|
||||||
},
|
|
||||||
error_handlers::LoggingErrorHandler,
|
|
||||||
};
|
|
||||||
use std::{fmt::Debug, future::Future, sync::Arc};
|
|
||||||
use teloxide_core::{requests::Requester, types::Message};
|
|
||||||
|
|
||||||
/// A [REPL] for dialogues.
|
|
||||||
///
|
|
||||||
/// All errors from an update listener and handler will be logged. This function
|
|
||||||
/// uses [`InMemStorage`].
|
|
||||||
///
|
|
||||||
/// # Caution
|
|
||||||
/// **DO NOT** use this function together with [`Dispatcher`] and other REPLs,
|
|
||||||
/// because Telegram disallow multiple requests at the same time from the same
|
|
||||||
/// bot.
|
|
||||||
///
|
|
||||||
/// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop
|
|
||||||
/// [`Dispatcher`]: crate::dispatching::Dispatcher
|
|
||||||
/// [`InMemStorage`]: crate::dispatching::dialogue::InMemStorage
|
|
||||||
#[cfg(feature = "ctrlc_handler")]
|
|
||||||
pub async fn dialogues_repl<'a, R, H, D, Fut>(requester: R, handler: H)
|
|
||||||
where
|
|
||||||
H: Fn(UpdateWithCx<R, Message>, D) -> Fut + Send + Sync + 'static,
|
|
||||||
D: Clone + Default + Send + 'static,
|
|
||||||
Fut: Future<Output = DialogueStage<D>> + Send + 'static,
|
|
||||||
R: Requester + Send + Clone + 'static,
|
|
||||||
<R as Requester>::GetUpdatesFaultTolerant: Send,
|
|
||||||
{
|
|
||||||
let cloned_requester = requester.clone();
|
|
||||||
|
|
||||||
dialogues_repl_with_listener(
|
|
||||||
requester,
|
|
||||||
handler,
|
|
||||||
update_listeners::polling_default(cloned_requester).await,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Like [`dialogues_repl`], but with a custom [`UpdateListener`].
|
|
||||||
///
|
|
||||||
/// All errors from an update listener and handler will be logged. This function
|
|
||||||
/// uses [`InMemStorage`].
|
|
||||||
///
|
|
||||||
/// # Caution
|
|
||||||
/// **DO NOT** use this function together with [`Dispatcher`] and other REPLs,
|
|
||||||
/// because Telegram disallow multiple requests at the same time from the same
|
|
||||||
/// bot.
|
|
||||||
///
|
|
||||||
/// [`Dispatcher`]: crate::dispatching::Dispatcher
|
|
||||||
/// [`dialogues_repl`]: crate::dispatching::repls::dialogues_repl()
|
|
||||||
/// [`UpdateListener`]: crate::dispatching::update_listeners::UpdateListener
|
|
||||||
/// [`InMemStorage`]: crate::dispatching::dialogue::InMemStorage
|
|
||||||
#[cfg(feature = "ctrlc_handler")]
|
|
||||||
pub async fn dialogues_repl_with_listener<'a, R, H, D, Fut, L, ListenerE>(
|
|
||||||
requester: R,
|
|
||||||
handler: H,
|
|
||||||
listener: L,
|
|
||||||
) where
|
|
||||||
H: Fn(UpdateWithCx<R, Message>, D) -> Fut + Send + Sync + 'static,
|
|
||||||
D: Clone + Default + Send + 'static,
|
|
||||||
Fut: Future<Output = DialogueStage<D>> + Send + 'static,
|
|
||||||
L: UpdateListener<ListenerE> + Send + 'a,
|
|
||||||
ListenerE: Debug + Send + 'a,
|
|
||||||
R: Requester + Send + Clone + 'static,
|
|
||||||
{
|
|
||||||
let handler = Arc::new(handler);
|
|
||||||
|
|
||||||
Dispatcher::new(requester)
|
|
||||||
.messages_handler(DialogueDispatcher::new(
|
|
||||||
move |DialogueWithCx { cx, dialogue }: DialogueWithCx<
|
|
||||||
R,
|
|
||||||
Message,
|
|
||||||
D,
|
|
||||||
InMemStorageError,
|
|
||||||
>| {
|
|
||||||
let handler = Arc::clone(&handler);
|
|
||||||
|
|
||||||
async move {
|
|
||||||
let dialogue = dialogue.expect("std::convert::Infallible");
|
|
||||||
handler(cx, dialogue).await
|
|
||||||
}
|
|
||||||
},
|
|
||||||
))
|
|
||||||
.setup_ctrlc_handler()
|
|
||||||
.dispatch_with_listener(
|
|
||||||
listener,
|
|
||||||
LoggingErrorHandler::with_custom_text("An error from the update listener"),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
}
|
|
|
@ -59,16 +59,10 @@ where
|
||||||
// messages. See <https://github.com/teloxide/teloxide/issues/557>.
|
// messages. See <https://github.com/teloxide/teloxide/issues/557>.
|
||||||
let ignore_update = |_upd| Box::pin(async {});
|
let ignore_update = |_upd| Box::pin(async {});
|
||||||
|
|
||||||
#[allow(unused_mut)]
|
Dispatcher::builder(bot, Update::filter_message().branch(dptree::endpoint(handler)))
|
||||||
let mut dispatcher =
|
.default_handler(ignore_update)
|
||||||
Dispatcher::builder(bot, Update::filter_message().branch(dptree::endpoint(handler)))
|
.build()
|
||||||
.default_handler(ignore_update)
|
.setup_ctrlc_handler()
|
||||||
.build();
|
|
||||||
|
|
||||||
#[cfg(feature = "ctrlc_handler")]
|
|
||||||
dispatcher.setup_ctrlc_handler();
|
|
||||||
|
|
||||||
dispatcher
|
|
||||||
.dispatch_with_listener(
|
.dispatch_with_listener(
|
||||||
listener,
|
listener,
|
||||||
LoggingErrorHandler::with_custom_text("An error from the update listener"),
|
LoggingErrorHandler::with_custom_text("An error from the update listener"),
|
||||||
|
|
Loading…
Reference in a new issue