DispatcherBuilder::new => Dispatcher::builder

This commit is contained in:
Hirrolot 2022-02-04 20:09:53 +06:00
parent a5d8a36f2c
commit 9efa2f6cbd
13 changed files with 37 additions and 31 deletions

View file

@ -220,7 +220,7 @@ async fn main() {
let bot = Bot::from_env().auto_send();
DispatcherBuilder::new(
Dispatcher::builder(
bot,
Update::filter_message()
.enter_dialogue::<Message, InMemStorage<State>, State>()

View file

@ -124,7 +124,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.branch(Update::filter_callback_query().endpoint(callback_handler))
.branch(Update::filter_inline_query().endpoint(inline_query_handler));
DispatcherBuilder::new(bot, handler).build().setup_ctrlc_handler().dispatch().await;
Dispatcher::builder(bot, handler).build().setup_ctrlc_handler().dispatch().await;
log::info!("Closing bot... Goodbye!");

View file

@ -46,7 +46,7 @@ async fn main() {
let bot = Bot::from_env().auto_send();
DispatcherBuilder::new(
Dispatcher::builder(
bot,
Update::filter_message()
.enter_dialogue::<Message, InMemStorage<State>, State>()

View file

@ -81,7 +81,7 @@ async fn main() {
);
// Start create dispatcher.
DispatcherBuilder::new(bot, handler)
Dispatcher::builder(bot, handler)
// You can specify dependencies to that you have access inside of handlers. It may be
// configs, connection to Database, or dialogue storage (see more in the dialogue_bot
// example). It is similar to the `actix_web::Extensions`.

View file

@ -60,5 +60,5 @@ async fn main() {
},
));
DispatcherBuilder::new(bot, handler).build().setup_ctrlc_handler().dispatch().await;
Dispatcher::builder(bot, handler).build().setup_ctrlc_handler().dispatch().await;
}

View file

@ -47,7 +47,7 @@ async fn main() {
.enter_dialogue::<Message, RedisStorage<Bincode>, State>()
.dispatch_by::<State>();
DispatcherBuilder::new(bot, handler)
Dispatcher::builder(bot, handler)
.dependencies(dptree::deps![storage])
.build()
.setup_ctrlc_handler()

View file

@ -25,5 +25,5 @@ async fn main() {
},
));
DispatcherBuilder::new(bot, handler).build().setup_ctrlc_handler().dispatch().await;
Dispatcher::builder(bot, handler).build().setup_ctrlc_handler().dispatch().await;
}

View file

@ -43,7 +43,7 @@ async fn main() {
.enter_dialogue::<Message, SqliteStorage<Json>, State>()
.dispatch_by::<State>();
DispatcherBuilder::new(bot, handler)
Dispatcher::builder(bot, handler)
.dependencies(dptree::deps![storage])
.build()
.setup_ctrlc_handler()

View file

@ -28,20 +28,6 @@ where
R: Clone + Requester + Clone + Send + Sync + 'static,
Err: Debug + Send + Sync + 'static,
{
/// Constructs a new [`DispatcherBuilder`] with `bot` and `handler`.
#[must_use]
pub fn new(bot: R, handler: UpdateHandler<Err>) -> Self {
Self {
bot,
dependencies: DependencyMap::new(),
handler,
default_handler: dptree::endpoint(|update: Update| async move {
log::warn!("Unhandled update: {:?}", update);
}),
error_handler: LoggingErrorHandler::new(),
}
}
/// Specifies a handler that will be called for an unhandled update.
///
/// By default, it is a mere [`log::warn`]. Note that it **must** always
@ -114,6 +100,23 @@ where
R: Requester + Clone + Send + Sync + 'static,
Err: Send + Sync + 'static,
{
/// Constructs a new [`DispatcherBuilder`] with `bot` and `handler`.
#[must_use]
pub fn builder(bot: R, handler: UpdateHandler<Err>) -> DispatcherBuilder<R, Err>
where
Err: Debug,
{
DispatcherBuilder {
bot,
dependencies: DependencyMap::new(),
handler,
default_handler: dptree::endpoint(|update: Update| async move {
log::warn!("Unhandled update: {:?}", update);
}),
error_handler: LoggingErrorHandler::new(),
}
}
/// Starts your bot with the default parameters.
///
/// The default parameters are a long polling update listener and log all
@ -123,7 +126,7 @@ where
/// dependencies (in addition to those passed to
/// [`DispatcherBuilder::dependencies`]):
///
/// - Your bot passed to [`DispatcherBuilder::new`];
/// - Your bot passed to [`Dispatcher::builder`];
/// - An update from Telegram;
/// - [`crate::types::Me`] (can be used in [`HandlerExt::filter_command`]).
///

View file

@ -21,7 +21,7 @@
//! The [`Dispatcher`] type puts all these things together: it only provides
//! [`Dispatcher::dispatch`] and a handful of other methods. Once you call
//! `.dispatch()`, it will retrieve updates from the Telegram server and pass
//! them to your handler, which is a parameter of [`DispatcherBuilder::new`].
//! them to your handler, which is a parameter of [`Dispatcher::builder`].
//!
//! Let us look at a simple example:
//!
@ -54,7 +54,7 @@
//! },
//! ));
//!
//! DispatcherBuilder::new(bot, handler).build().setup_ctrlc_handler().dispatch().await;
//! Dispatcher::builder(bot, handler).build().setup_ctrlc_handler().dispatch().await;
//! # }
//! ```
//!

View file

@ -1,6 +1,6 @@
use crate::{
dispatching::{update_listeners, update_listeners::UpdateListener},
dispatching2::{DispatcherBuilder, HandlerExt, UpdateFilterExt},
dispatching2::{HandlerExt, UpdateFilterExt},
error_handlers::LoggingErrorHandler,
types::Update,
utils::command::BotCommand,
@ -74,7 +74,9 @@ pub async fn commands_repl_with_listener<'a, R, Cmd, H, L, ListenerE, E, Args>(
R: Requester + Clone + Send + Sync + 'static,
E: Debug + Send + Sync + 'static,
{
let mut dispatcher = DispatcherBuilder::new(
use crate::dispatching2::Dispatcher;
let mut dispatcher = Dispatcher::builder(
bot,
Update::filter_message().filter_command::<Cmd>().branch(dptree::endpoint(handler)),
)

View file

@ -1,6 +1,6 @@
use crate::{
dispatching::{update_listeners, update_listeners::UpdateListener},
dispatching2::{DispatcherBuilder, UpdateFilterExt},
dispatching2::UpdateFilterExt,
error_handlers::{LoggingErrorHandler, OnError},
types::Update,
};
@ -54,9 +54,11 @@ where
E: Debug + Send + Sync + 'static,
R: Requester + Clone + Send + Sync + 'static,
{
use crate::dispatching2::Dispatcher;
#[allow(unused_mut)]
let mut dispatcher =
DispatcherBuilder::new(bot, Update::filter_message().branch(dptree::endpoint(handler)))
Dispatcher::builder(bot, Update::filter_message().branch(dptree::endpoint(handler)))
.build();
#[cfg(feature = "ctrlc_handler")]

View file

@ -6,8 +6,7 @@ pub use crate::{
};
pub use crate::dispatching2::{
dialogue::Dialogue, Dispatcher, DispatcherBuilder, HandlerExt as _, MessageFilterExt as _,
UpdateFilterExt as _,
dialogue::Dialogue, Dispatcher, HandlerExt as _, MessageFilterExt as _, UpdateFilterExt as _,
};
#[cfg_attr(all(docsrs, feature = "nightly"), doc(cfg(feature = "macros")))]