diff --git a/examples/ping_pong_bot.rs b/examples/ping_pong_bot.rs index 8948dada..07126937 100644 --- a/examples/ping_pong_bot.rs +++ b/examples/ping_pong_bot.rs @@ -1,34 +1,28 @@ -use futures::stream::StreamExt; use teloxide::{ dispatching::{ - chat::{ChatUpdate, ChatUpdateKind, Dispatcher}, - update_listeners::polling_default, - SessionState, + session::{SessionDispatcher, SessionHandlerCtx, SessionState}, + Dispatcher, }, requests::Request, + types::Message, Bot, }; #[tokio::main] async fn main() { - let bot = &Bot::new("1061598315:AAErEDodTsrqD3UxA_EvFyEfXbKA6DT25G0"); - let mut updater = Box::pin(polling_default(bot)); - let handler = |_, upd: ChatUpdate| async move { - if let ChatUpdateKind::Message(m) = upd.kind { - let msg = bot.send_message(m.chat.id, "pong"); - msg.send().await.unwrap(); - } - SessionState::Continue(()) - }; - let mut dp = Dispatcher::<'_, (), _>::new(handler); - println!("Starting the message handler."); - loop { - let u = updater.next().await.unwrap(); - match u { - Err(e) => eprintln!("Error: {}", e), - Ok(u) => { - let _ = dp.dispatch(u).await; - } - } - } + Dispatcher::<(), (), _, _, ()>::new(&Bot::new( + "1061598315:AAErEDodTsrqD3UxA_EvFyEfXbKA6DT25G0", + )) + .private_message_dp(SessionDispatcher::new( + |ctx: SessionHandlerCtx| async move { + ctx.bot + .send_message(ctx.update.chat.id, "pong") + .send() + .await + .unwrap(); + SessionState::Continue(()) + }, + )) + .dispatch() + .await } diff --git a/src/dispatching/dispatcher.rs b/src/dispatching/dispatcher.rs index 97c63f1b..09a7e65f 100644 --- a/src/dispatching/dispatcher.rs +++ b/src/dispatching/dispatcher.rs @@ -4,7 +4,7 @@ use crate::{ session::{SessionDispatcher, SessionHandlerCtx, SessionState}, update_listeners, update_listeners::UpdateListener, - Handler, + AsyncHandler, }, types::{ CallbackQuery, ChatKind, ChosenInlineResult, InlineQuery, Message, @@ -20,35 +20,40 @@ pub struct BasicHandlerCtx<'a, Upd> { pub update: Upd, } +/// The main dispatcher that joins all the parts together. pub struct Dispatcher<'a, Session1, Session2, H1, H2, HandlerE> { bot: &'a Bot, - handlers_error_handler: Box + 'a>, + handlers_error_handler: Box + 'a>, private_message_dp: Option>, private_edited_message_dp: Option>, message_handler: - Option, ()> + 'a>>, + Option, ()> + 'a>>, edited_message_handler: - Option, ()> + 'a>>, + Option, ()> + 'a>>, channel_post_handler: - Option, ()> + 'a>>, + Option, ()> + 'a>>, edited_channel_post_handler: - Option, ()> + 'a>>, - inline_query_handler: - Option, ()> + 'a>>, + Option, ()> + 'a>>, + inline_query_handler: Option< + Box, ()> + 'a>, + >, chosen_inline_result_handler: Option< - Box, ()> + 'a>, + Box, ()> + 'a>, + >, + callback_query_handler: Option< + Box, ()> + 'a>, + >, + shipping_query_handler: Option< + Box, ()> + 'a>, >, - callback_query_handler: - Option, ()> + 'a>>, - shipping_query_handler: - Option, ()> + 'a>>, pre_checkout_query_handler: Option< - Box, ()> + 'a>, + Box, ()> + 'a>, >, - poll_handler: Option, ()> + 'a>>, + poll_handler: + Option, ()> + 'a>>, } impl<'a, Session1, Session2, H1, H2, HandlerE> @@ -56,11 +61,11 @@ impl<'a, Session1, Session2, H1, H2, HandlerE> where Session1: Default + 'a, Session2: Default + 'a, - H1: Handler< + H1: AsyncHandler< SessionHandlerCtx<'a, Message, Session1>, SessionState, > + 'a, - H2: Handler< + H2: AsyncHandler< SessionHandlerCtx<'a, Message, Session2>, SessionState, > + 'a, @@ -87,7 +92,7 @@ where pub fn handlers_error_handler(mut self, val: T) -> Self where - T: Handler + 'a, + T: AsyncHandler + 'a, { self.handlers_error_handler = Box::new(val); self @@ -111,7 +116,7 @@ where pub fn message_handler(mut self, h: H) -> Self where - H: Handler, ()> + 'a, + H: AsyncHandler, ()> + 'a, { self.message_handler = Some(Box::new(h)); self @@ -119,7 +124,7 @@ where pub fn edited_message_handler(mut self, h: H) -> Self where - H: Handler, ()> + 'a, + H: AsyncHandler, ()> + 'a, { self.edited_message_handler = Some(Box::new(h)); self @@ -127,7 +132,7 @@ where pub fn channel_post_handler(mut self, h: H) -> Self where - H: Handler, ()> + 'a, + H: AsyncHandler, ()> + 'a, { self.channel_post_handler = Some(Box::new(h)); self @@ -135,7 +140,7 @@ where pub fn edited_channel_post_handler(mut self, h: H) -> Self where - H: Handler, ()> + 'a, + H: AsyncHandler, ()> + 'a, { self.edited_channel_post_handler = Some(Box::new(h)); self @@ -143,7 +148,7 @@ where pub fn inline_query_handler(mut self, h: H) -> Self where - H: Handler, ()> + 'a, + H: AsyncHandler, ()> + 'a, { self.inline_query_handler = Some(Box::new(h)); self @@ -151,7 +156,7 @@ where pub fn chosen_inline_result_handler(mut self, h: H) -> Self where - H: Handler, ()> + 'a, + H: AsyncHandler, ()> + 'a, { self.chosen_inline_result_handler = Some(Box::new(h)); self @@ -159,7 +164,7 @@ where pub fn callback_query_handler(mut self, h: H) -> Self where - H: Handler, ()> + 'a, + H: AsyncHandler, ()> + 'a, { self.callback_query_handler = Some(Box::new(h)); self @@ -167,7 +172,7 @@ where pub fn shipping_query_handler(mut self, h: H) -> Self where - H: Handler, ()> + 'a, + H: AsyncHandler, ()> + 'a, { self.shipping_query_handler = Some(Box::new(h)); self @@ -175,7 +180,7 @@ where pub fn pre_checkout_query_handler(mut self, h: H) -> Self where - H: Handler, ()> + 'a, + H: AsyncHandler, ()> + 'a, { self.pre_checkout_query_handler = Some(Box::new(h)); self @@ -183,7 +188,7 @@ where pub fn poll_handler(mut self, h: H) -> Self where - H: Handler, ()> + 'a, + H: AsyncHandler, ()> + 'a, { self.poll_handler = Some(Box::new(h)); self @@ -203,7 +208,7 @@ where update_listener_error_handler: &'a Eh, ) where UListener: UpdateListener + 'a, - Eh: Handler + 'a, + Eh: AsyncHandler + 'a, ListenerE: Debug, { let update_listener = Box::pin(update_listener); diff --git a/src/dispatching/error_handlers.rs b/src/dispatching/error_handlers.rs index 848b4502..76f360df 100644 --- a/src/dispatching/error_handlers.rs +++ b/src/dispatching/error_handlers.rs @@ -1,4 +1,6 @@ -use crate::dispatching::Handler; +//! Handlers of errors. + +use crate::dispatching::AsyncHandler; use std::{convert::Infallible, fmt::Debug, future::Future, pin::Pin}; /// A handler that silently ignores all errors. @@ -16,7 +18,7 @@ use std::{convert::Infallible, fmt::Debug, future::Future, pin::Pin}; /// ``` pub struct Ignore; -impl Handler for Ignore { +impl AsyncHandler for Ignore { fn handle<'a>(&'a self, _: E) -> Pin + 'a>> where E: 'a, @@ -59,7 +61,7 @@ impl Handler for Ignore { pub struct IgnoreSafe; #[allow(unreachable_code)] -impl Handler for IgnoreSafe { +impl AsyncHandler for IgnoreSafe { fn handle<'a>( &'a self, _: Infallible, @@ -86,7 +88,7 @@ impl Handler for IgnoreSafe { /// ``` pub struct Log; -impl Handler for Log +impl AsyncHandler for Log where E: Debug, { diff --git a/src/dispatching/handler.rs b/src/dispatching/handler.rs index a2b94130..292e5e71 100644 --- a/src/dispatching/handler.rs +++ b/src/dispatching/handler.rs @@ -1,7 +1,7 @@ use std::{future::Future, pin::Pin}; /// An asynchronous polymorphic handler of a context. -pub trait Handler { +pub trait AsyncHandler { #[must_use] fn handle<'a>( &'a self, @@ -11,8 +11,7 @@ pub trait Handler { Ctx: 'a; } -/// The implementation of `Handler` for `Fn(Ctx) -> Future`. -impl Handler for F +impl AsyncHandler for F where F: Fn(Ctx) -> Fut, Fut: Future, diff --git a/src/dispatching/session/mod.rs b/src/dispatching/session/mod.rs index ec47ab25..c2d499fc 100644 --- a/src/dispatching/session/mod.rs +++ b/src/dispatching/session/mod.rs @@ -31,7 +31,7 @@ mod get_chat_id; mod storage; -use crate::{dispatching::Handler, Bot}; +use crate::{dispatching::AsyncHandler, Bot}; pub use get_chat_id::*; pub use storage::*; @@ -86,7 +86,10 @@ where /// Dispatches a single `message` from a private chat. pub async fn dispatch(&'a self, bot: &'a Bot, update: Upd) where - H: Handler, SessionState>, + H: AsyncHandler< + SessionHandlerCtx<'a, Upd, Session>, + SessionState, + >, Upd: GetChatId, { let chat_id = update.chat_id();