From 461e1150e59bca46eed7b9168ecd6273f60edd69 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Thu, 30 Jul 2020 18:54:48 +0600 Subject: [PATCH] Add dispatching::repl --- CHANGELOG.md | 4 ++-- src/dispatching/mod.rs | 2 ++ src/dispatching/repl.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/prelude.rs | 2 +- 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 src/dispatching/repl.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b6be63f..fc4ed1d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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}` diff --git a/src/dispatching/mod.rs b/src/dispatching/mod.rs index 320ae5dc..a99c954e 100644 --- a/src/dispatching/mod.rs +++ b/src/dispatching/mod.rs @@ -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; diff --git a/src/dispatching/repl.rs b/src/dispatching/repl.rs new file mode 100644 index 00000000..d138f402 --- /dev/null +++ b/src/dispatching/repl.rs @@ -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(bot: Bot, handler: H) +where + H: Fn(UpdateWithCx) -> Fut + Send + Sync + 'static, + Fut: Future> + Send + 'static, +{ + let handler = Arc::new(handler); + + Dispatcher::new(bot) + .messages_handler(|rx: DispatcherHandlerRx| { + rx.for_each_concurrent(None, move |message| { + let handler = Arc::clone(&handler); + + async move { + handler(message).await.log_on_error().await; + } + }) + }) + .dispatch() + .await; +} diff --git a/src/prelude.rs b/src/prelude.rs index 3b2032d9..ee244451 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -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},