mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-31 16:40:37 +01:00
Add dispatching::repl
This commit is contained in:
parent
5b47cbc7d6
commit
461e1150e5
4 changed files with 45 additions and 3 deletions
|
@ -16,11 +16,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
- The `frunk` feature -- enables `teloxide::utils::UpState`, which allows mapping from a structure of `field1, ..., fieldN` to a structure of `field1, ..., fieldN, fieldN+1`.
|
||||
- Upgrade to v4.9 Telegram bots API.
|
||||
- `teloxide::utils::client_from_env` -- constructs a client from the `TELOXIDE_TOKEN` environmental variable.
|
||||
- Import `Transition`, `TransitionIn`, `TransitionOut`, `UpState` to `teloxide::prelude`.
|
||||
- Import `Transition`, `TransitionIn`, `TransitionOut`, `UpState`, `repl` to `teloxide::prelude`.
|
||||
- Let users inspect an unknown API error using `ApiErrorKind::Unknown(String)`. All the known API errors are placed into `KnownApiErrorKind`.
|
||||
- Setters to all the API types.
|
||||
- `teloxide::dispatching::dialogue::serializer` -- various serializers for memory storages. The `Serializer` trait, `Bincode`, `CBOR`, `JSON`.
|
||||
|
||||
- `teloxide::dispatching::repl`
|
||||
|
||||
### Deprecated
|
||||
- `Bot::{from_env_with_client, new, with_client}`
|
||||
|
|
|
@ -78,12 +78,14 @@ pub mod dialogue;
|
|||
mod dispatcher;
|
||||
mod dispatcher_handler;
|
||||
mod dispatcher_handler_rx_ext;
|
||||
mod repl;
|
||||
pub mod update_listeners;
|
||||
mod update_with_cx;
|
||||
|
||||
pub use dispatcher::Dispatcher;
|
||||
pub use dispatcher_handler::DispatcherHandler;
|
||||
pub use dispatcher_handler_rx_ext::DispatcherHandlerRxExt;
|
||||
pub use repl::repl;
|
||||
use tokio::sync::mpsc::UnboundedReceiver;
|
||||
pub use update_with_cx::UpdateWithCx;
|
||||
|
||||
|
|
40
src/dispatching/repl.rs
Normal file
40
src/dispatching/repl.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
use crate::{
|
||||
dispatching::{Dispatcher, DispatcherHandlerRx, UpdateWithCx},
|
||||
error_handlers::OnError,
|
||||
requests::ResponseResult,
|
||||
types::Message,
|
||||
Bot,
|
||||
};
|
||||
use futures::StreamExt;
|
||||
use std::{future::Future, sync::Arc};
|
||||
|
||||
/// A [REPL] for messages.
|
||||
///
|
||||
/// Used mostly for testing and demonstrative purposes.
|
||||
///
|
||||
/// # Caution
|
||||
/// **DO NOT** use this function together with [`Dispatcher`], 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
|
||||
pub async fn repl<H, Fut>(bot: Bot, handler: H)
|
||||
where
|
||||
H: Fn(UpdateWithCx<Message>) -> Fut + Send + Sync + 'static,
|
||||
Fut: Future<Output = ResponseResult<()>> + Send + 'static,
|
||||
{
|
||||
let handler = Arc::new(handler);
|
||||
|
||||
Dispatcher::new(bot)
|
||||
.messages_handler(|rx: DispatcherHandlerRx<Message>| {
|
||||
rx.for_each_concurrent(None, move |message| {
|
||||
let handler = Arc::clone(&handler);
|
||||
|
||||
async move {
|
||||
handler(message).await.log_on_error().await;
|
||||
}
|
||||
})
|
||||
})
|
||||
.dispatch()
|
||||
.await;
|
||||
}
|
|
@ -6,7 +6,7 @@ pub use crate::{
|
|||
exit, next, DialogueDispatcher, DialogueStage, DialogueWithCx, GetChatId, Transition,
|
||||
TransitionIn, TransitionOut,
|
||||
},
|
||||
Dispatcher, DispatcherHandlerRx, DispatcherHandlerRxExt, UpdateWithCx,
|
||||
repl, Dispatcher, DispatcherHandlerRx, DispatcherHandlerRxExt, UpdateWithCx,
|
||||
},
|
||||
error_handlers::{LoggingErrorHandler, OnError},
|
||||
requests::{Request, ResponseResult},
|
||||
|
|
Loading…
Reference in a new issue