mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-23 06:51:01 +01:00
Add commands_repl_with_listener
This commit is contained in:
parent
cfb78097b2
commit
1ab789fd71
3 changed files with 61 additions and 22 deletions
|
@ -1,45 +1,84 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
dispatching::{Dispatcher, DispatcherHandlerRx, DispatcherHandlerRxExt, UpdateWithCx},
|
dispatching::{
|
||||||
error_handlers::OnError,
|
update_listeners, update_listeners::UpdateListener, Dispatcher, DispatcherHandlerRx,
|
||||||
|
DispatcherHandlerRxExt, UpdateWithCx,
|
||||||
|
},
|
||||||
|
error_handlers::{LoggingErrorHandler, OnError},
|
||||||
requests::ResponseResult,
|
requests::ResponseResult,
|
||||||
types::Message,
|
types::Message,
|
||||||
utils::command::BotCommand,
|
utils::command::BotCommand,
|
||||||
Bot,
|
Bot,
|
||||||
};
|
};
|
||||||
use futures::StreamExt;
|
use futures::{future::BoxFuture, FutureExt, StreamExt};
|
||||||
use std::{future::Future, sync::Arc};
|
use std::{fmt::Debug, future::Future, sync::Arc};
|
||||||
|
|
||||||
/// A [REPL] for commands.
|
/// A [REPL] for commands.
|
||||||
///
|
///
|
||||||
/// # Caution
|
/// # Caution
|
||||||
/// **DO NOT** use this function together with [`Dispatcher`] and [`repl`],
|
/// **DO NOT** use this function together with [`Dispatcher`] and other REPLs,
|
||||||
/// because Telegram disallow multiple requests at the same time from the same
|
/// because Telegram disallow multiple requests at the same time from the same
|
||||||
/// bot.
|
/// bot.
|
||||||
///
|
///
|
||||||
/// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop
|
/// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop
|
||||||
/// [`Dispatcher`]: crate::dispatching::Dispatcher
|
/// [`Dispatcher`]: crate::dispatching::Dispatcher
|
||||||
/// [`repl`]: crate::dispatching::repl
|
pub fn commands_repl<Cmd, H, Fut>(
|
||||||
pub async fn commands_repl<Cmd, H, Fut>(bot: Bot, bot_name: &'static str, handler: H)
|
bot: Bot,
|
||||||
|
bot_name: &'static str,
|
||||||
|
handler: H,
|
||||||
|
) -> BoxFuture<'static, ()>
|
||||||
where
|
where
|
||||||
Cmd: BotCommand + Send + 'static,
|
Cmd: BotCommand + Send + 'static,
|
||||||
H: Fn(UpdateWithCx<Message>, Cmd) -> Fut + Send + Sync + 'static,
|
H: Fn(UpdateWithCx<Message>, Cmd) -> Fut + Send + Sync + 'static,
|
||||||
Fut: Future<Output = ResponseResult<()>> + Send + 'static,
|
Fut: Future<Output = ResponseResult<()>> + Send + 'static,
|
||||||
|
{
|
||||||
|
let cloned_bot = bot.clone();
|
||||||
|
|
||||||
|
commands_repl_with_listener(
|
||||||
|
bot,
|
||||||
|
bot_name,
|
||||||
|
handler,
|
||||||
|
update_listeners::polling_default(cloned_bot),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Like [`commands_repl`], but with a custom [`UpdateListener`].
|
||||||
|
///
|
||||||
|
/// [`commands_repl`]: crate::dispatching::commands_repl
|
||||||
|
/// [`UpdateListener`]: crate::dispatching::update_listeners::UpdateListener
|
||||||
|
pub fn commands_repl_with_listener<'a, Cmd, H, Fut, UL, ListenerE>(
|
||||||
|
bot: Bot,
|
||||||
|
bot_name: &'static str,
|
||||||
|
handler: H,
|
||||||
|
update_listener: UL,
|
||||||
|
) -> BoxFuture<'a, ()>
|
||||||
|
where
|
||||||
|
Cmd: BotCommand + Send + 'static,
|
||||||
|
H: Fn(UpdateWithCx<Message>, Cmd) -> Fut + Send + Sync + 'static,
|
||||||
|
Fut: Future<Output = ResponseResult<()>> + Send + 'static,
|
||||||
|
UL: UpdateListener<ListenerE> + Send + 'a,
|
||||||
|
ListenerE: Debug + Send + 'a,
|
||||||
{
|
{
|
||||||
let handler = Arc::new(handler);
|
let handler = Arc::new(handler);
|
||||||
|
|
||||||
Dispatcher::new(bot)
|
async move {
|
||||||
.messages_handler(move |rx: DispatcherHandlerRx<Message>| {
|
Dispatcher::new(bot)
|
||||||
rx.commands::<Cmd, &'static str>(bot_name).for_each_concurrent(
|
.messages_handler(move |rx: DispatcherHandlerRx<Message>| {
|
||||||
None,
|
rx.commands::<Cmd, &'static str>(bot_name).for_each_concurrent(
|
||||||
move |(cx, cmd)| {
|
None,
|
||||||
let handler = Arc::clone(&handler);
|
move |(cx, cmd)| {
|
||||||
|
let handler = Arc::clone(&handler);
|
||||||
|
|
||||||
async move {
|
async move {
|
||||||
handler(cx, cmd).await.log_on_error().await;
|
handler(cx, cmd).await.log_on_error().await;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.dispatch_with_listener(
|
||||||
|
update_listener,
|
||||||
|
LoggingErrorHandler::with_custom_text("An error from the update listener"),
|
||||||
)
|
)
|
||||||
})
|
.await
|
||||||
.dispatch()
|
}
|
||||||
.await;
|
.boxed()
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,7 +52,7 @@ mod repl;
|
||||||
pub mod update_listeners;
|
pub mod update_listeners;
|
||||||
mod update_with_cx;
|
mod update_with_cx;
|
||||||
|
|
||||||
pub use commands_repl::commands_repl;
|
pub use commands_repl::{commands_repl, commands_repl_with_listener};
|
||||||
pub use dispatcher::Dispatcher;
|
pub use dispatcher::Dispatcher;
|
||||||
pub use dispatcher_handler::DispatcherHandler;
|
pub use dispatcher_handler::DispatcherHandler;
|
||||||
pub use dispatcher_handler_rx_ext::DispatcherHandlerRxExt;
|
pub use dispatcher_handler_rx_ext::DispatcherHandlerRxExt;
|
||||||
|
|
|
@ -42,7 +42,7 @@
|
||||||
#![forbid(unsafe_code)]
|
#![forbid(unsafe_code)]
|
||||||
|
|
||||||
pub use bot::{Bot, BotBuilder};
|
pub use bot::{Bot, BotBuilder};
|
||||||
pub use dispatching::{commands_repl, repl};
|
pub use dispatching::{commands_repl, commands_repl_with_listener, repl};
|
||||||
pub use errors::{ApiErrorKind, DownloadError, KnownApiErrorKind, RequestError};
|
pub use errors::{ApiErrorKind, DownloadError, KnownApiErrorKind, RequestError};
|
||||||
|
|
||||||
mod errors;
|
mod errors;
|
||||||
|
|
Loading…
Reference in a new issue