Trying to fix ping_pong_bot.rs

This commit is contained in:
Temirkhan Myrzamadi 2020-01-29 21:08:15 +06:00
parent 3bad400c03
commit 27ae3e13cf
5 changed files with 65 additions and 62 deletions

View file

@ -1,34 +1,28 @@
use futures::stream::StreamExt;
use teloxide::{ use teloxide::{
dispatching::{ dispatching::{
chat::{ChatUpdate, ChatUpdateKind, Dispatcher}, session::{SessionDispatcher, SessionHandlerCtx, SessionState},
update_listeners::polling_default, Dispatcher,
SessionState,
}, },
requests::Request, requests::Request,
types::Message,
Bot, Bot,
}; };
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let bot = &Bot::new("1061598315:AAErEDodTsrqD3UxA_EvFyEfXbKA6DT25G0"); Dispatcher::<(), (), _, _, ()>::new(&Bot::new(
let mut updater = Box::pin(polling_default(bot)); "1061598315:AAErEDodTsrqD3UxA_EvFyEfXbKA6DT25G0",
let handler = |_, upd: ChatUpdate| async move { ))
if let ChatUpdateKind::Message(m) = upd.kind { .private_message_dp(SessionDispatcher::new(
let msg = bot.send_message(m.chat.id, "pong"); |ctx: SessionHandlerCtx<Message, ()>| async move {
msg.send().await.unwrap(); ctx.bot
} .send_message(ctx.update.chat.id, "pong")
.send()
.await
.unwrap();
SessionState::Continue(()) SessionState::Continue(())
}; },
let mut dp = Dispatcher::<'_, (), _>::new(handler); ))
println!("Starting the message handler."); .dispatch()
loop { .await
let u = updater.next().await.unwrap();
match u {
Err(e) => eprintln!("Error: {}", e),
Ok(u) => {
let _ = dp.dispatch(u).await;
}
}
}
} }

View file

@ -4,7 +4,7 @@ use crate::{
session::{SessionDispatcher, SessionHandlerCtx, SessionState}, session::{SessionDispatcher, SessionHandlerCtx, SessionState},
update_listeners, update_listeners,
update_listeners::UpdateListener, update_listeners::UpdateListener,
Handler, AsyncHandler,
}, },
types::{ types::{
CallbackQuery, ChatKind, ChosenInlineResult, InlineQuery, Message, CallbackQuery, ChatKind, ChosenInlineResult, InlineQuery, Message,
@ -20,35 +20,40 @@ pub struct BasicHandlerCtx<'a, Upd> {
pub update: Upd, pub update: Upd,
} }
/// The main dispatcher that joins all the parts together.
pub struct Dispatcher<'a, Session1, Session2, H1, H2, HandlerE> { pub struct Dispatcher<'a, Session1, Session2, H1, H2, HandlerE> {
bot: &'a Bot, bot: &'a Bot,
handlers_error_handler: Box<dyn Handler<HandlerE, ()> + 'a>, handlers_error_handler: Box<dyn AsyncHandler<HandlerE, ()> + 'a>,
private_message_dp: Option<SessionDispatcher<'a, Session1, H1>>, private_message_dp: Option<SessionDispatcher<'a, Session1, H1>>,
private_edited_message_dp: Option<SessionDispatcher<'a, Session2, H2>>, private_edited_message_dp: Option<SessionDispatcher<'a, Session2, H2>>,
message_handler: message_handler:
Option<Box<dyn Handler<BasicHandlerCtx<'a, Message>, ()> + 'a>>, Option<Box<dyn AsyncHandler<BasicHandlerCtx<'a, Message>, ()> + 'a>>,
edited_message_handler: edited_message_handler:
Option<Box<dyn Handler<BasicHandlerCtx<'a, Message>, ()> + 'a>>, Option<Box<dyn AsyncHandler<BasicHandlerCtx<'a, Message>, ()> + 'a>>,
channel_post_handler: channel_post_handler:
Option<Box<dyn Handler<BasicHandlerCtx<'a, Message>, ()> + 'a>>, Option<Box<dyn AsyncHandler<BasicHandlerCtx<'a, Message>, ()> + 'a>>,
edited_channel_post_handler: edited_channel_post_handler:
Option<Box<dyn Handler<BasicHandlerCtx<'a, Message>, ()> + 'a>>, Option<Box<dyn AsyncHandler<BasicHandlerCtx<'a, Message>, ()> + 'a>>,
inline_query_handler: inline_query_handler: Option<
Option<Box<dyn Handler<BasicHandlerCtx<'a, InlineQuery>, ()> + 'a>>, Box<dyn AsyncHandler<BasicHandlerCtx<'a, InlineQuery>, ()> + 'a>,
>,
chosen_inline_result_handler: Option< chosen_inline_result_handler: Option<
Box<dyn Handler<BasicHandlerCtx<'a, ChosenInlineResult>, ()> + 'a>, Box<dyn AsyncHandler<BasicHandlerCtx<'a, ChosenInlineResult>, ()> + 'a>,
>,
callback_query_handler: Option<
Box<dyn AsyncHandler<BasicHandlerCtx<'a, CallbackQuery>, ()> + 'a>,
>,
shipping_query_handler: Option<
Box<dyn AsyncHandler<BasicHandlerCtx<'a, ShippingQuery>, ()> + 'a>,
>, >,
callback_query_handler:
Option<Box<dyn Handler<BasicHandlerCtx<'a, CallbackQuery>, ()> + 'a>>,
shipping_query_handler:
Option<Box<dyn Handler<BasicHandlerCtx<'a, ShippingQuery>, ()> + 'a>>,
pre_checkout_query_handler: Option< pre_checkout_query_handler: Option<
Box<dyn Handler<BasicHandlerCtx<'a, PreCheckoutQuery>, ()> + 'a>, Box<dyn AsyncHandler<BasicHandlerCtx<'a, PreCheckoutQuery>, ()> + 'a>,
>, >,
poll_handler: Option<Box<dyn Handler<BasicHandlerCtx<'a, Poll>, ()> + 'a>>, poll_handler:
Option<Box<dyn AsyncHandler<BasicHandlerCtx<'a, Poll>, ()> + 'a>>,
} }
impl<'a, Session1, Session2, H1, H2, HandlerE> impl<'a, Session1, Session2, H1, H2, HandlerE>
@ -56,11 +61,11 @@ impl<'a, Session1, Session2, H1, H2, HandlerE>
where where
Session1: Default + 'a, Session1: Default + 'a,
Session2: Default + 'a, Session2: Default + 'a,
H1: Handler< H1: AsyncHandler<
SessionHandlerCtx<'a, Message, Session1>, SessionHandlerCtx<'a, Message, Session1>,
SessionState<Session1>, SessionState<Session1>,
> + 'a, > + 'a,
H2: Handler< H2: AsyncHandler<
SessionHandlerCtx<'a, Message, Session2>, SessionHandlerCtx<'a, Message, Session2>,
SessionState<Session2>, SessionState<Session2>,
> + 'a, > + 'a,
@ -87,7 +92,7 @@ where
pub fn handlers_error_handler<T>(mut self, val: T) -> Self pub fn handlers_error_handler<T>(mut self, val: T) -> Self
where where
T: Handler<HandlerE, ()> + 'a, T: AsyncHandler<HandlerE, ()> + 'a,
{ {
self.handlers_error_handler = Box::new(val); self.handlers_error_handler = Box::new(val);
self self
@ -111,7 +116,7 @@ where
pub fn message_handler<H>(mut self, h: H) -> Self pub fn message_handler<H>(mut self, h: H) -> Self
where where
H: Handler<BasicHandlerCtx<'a, Message>, ()> + 'a, H: AsyncHandler<BasicHandlerCtx<'a, Message>, ()> + 'a,
{ {
self.message_handler = Some(Box::new(h)); self.message_handler = Some(Box::new(h));
self self
@ -119,7 +124,7 @@ where
pub fn edited_message_handler<H>(mut self, h: H) -> Self pub fn edited_message_handler<H>(mut self, h: H) -> Self
where where
H: Handler<BasicHandlerCtx<'a, Message>, ()> + 'a, H: AsyncHandler<BasicHandlerCtx<'a, Message>, ()> + 'a,
{ {
self.edited_message_handler = Some(Box::new(h)); self.edited_message_handler = Some(Box::new(h));
self self
@ -127,7 +132,7 @@ where
pub fn channel_post_handler<H>(mut self, h: H) -> Self pub fn channel_post_handler<H>(mut self, h: H) -> Self
where where
H: Handler<BasicHandlerCtx<'a, Message>, ()> + 'a, H: AsyncHandler<BasicHandlerCtx<'a, Message>, ()> + 'a,
{ {
self.channel_post_handler = Some(Box::new(h)); self.channel_post_handler = Some(Box::new(h));
self self
@ -135,7 +140,7 @@ where
pub fn edited_channel_post_handler<H>(mut self, h: H) -> Self pub fn edited_channel_post_handler<H>(mut self, h: H) -> Self
where where
H: Handler<BasicHandlerCtx<'a, Message>, ()> + 'a, H: AsyncHandler<BasicHandlerCtx<'a, Message>, ()> + 'a,
{ {
self.edited_channel_post_handler = Some(Box::new(h)); self.edited_channel_post_handler = Some(Box::new(h));
self self
@ -143,7 +148,7 @@ where
pub fn inline_query_handler<H>(mut self, h: H) -> Self pub fn inline_query_handler<H>(mut self, h: H) -> Self
where where
H: Handler<BasicHandlerCtx<'a, InlineQuery>, ()> + 'a, H: AsyncHandler<BasicHandlerCtx<'a, InlineQuery>, ()> + 'a,
{ {
self.inline_query_handler = Some(Box::new(h)); self.inline_query_handler = Some(Box::new(h));
self self
@ -151,7 +156,7 @@ where
pub fn chosen_inline_result_handler<H>(mut self, h: H) -> Self pub fn chosen_inline_result_handler<H>(mut self, h: H) -> Self
where where
H: Handler<BasicHandlerCtx<'a, ChosenInlineResult>, ()> + 'a, H: AsyncHandler<BasicHandlerCtx<'a, ChosenInlineResult>, ()> + 'a,
{ {
self.chosen_inline_result_handler = Some(Box::new(h)); self.chosen_inline_result_handler = Some(Box::new(h));
self self
@ -159,7 +164,7 @@ where
pub fn callback_query_handler<H>(mut self, h: H) -> Self pub fn callback_query_handler<H>(mut self, h: H) -> Self
where where
H: Handler<BasicHandlerCtx<'a, CallbackQuery>, ()> + 'a, H: AsyncHandler<BasicHandlerCtx<'a, CallbackQuery>, ()> + 'a,
{ {
self.callback_query_handler = Some(Box::new(h)); self.callback_query_handler = Some(Box::new(h));
self self
@ -167,7 +172,7 @@ where
pub fn shipping_query_handler<H>(mut self, h: H) -> Self pub fn shipping_query_handler<H>(mut self, h: H) -> Self
where where
H: Handler<BasicHandlerCtx<'a, ShippingQuery>, ()> + 'a, H: AsyncHandler<BasicHandlerCtx<'a, ShippingQuery>, ()> + 'a,
{ {
self.shipping_query_handler = Some(Box::new(h)); self.shipping_query_handler = Some(Box::new(h));
self self
@ -175,7 +180,7 @@ where
pub fn pre_checkout_query_handler<H>(mut self, h: H) -> Self pub fn pre_checkout_query_handler<H>(mut self, h: H) -> Self
where where
H: Handler<BasicHandlerCtx<'a, PreCheckoutQuery>, ()> + 'a, H: AsyncHandler<BasicHandlerCtx<'a, PreCheckoutQuery>, ()> + 'a,
{ {
self.pre_checkout_query_handler = Some(Box::new(h)); self.pre_checkout_query_handler = Some(Box::new(h));
self self
@ -183,7 +188,7 @@ where
pub fn poll_handler<H>(mut self, h: H) -> Self pub fn poll_handler<H>(mut self, h: H) -> Self
where where
H: Handler<BasicHandlerCtx<'a, Poll>, ()> + 'a, H: AsyncHandler<BasicHandlerCtx<'a, Poll>, ()> + 'a,
{ {
self.poll_handler = Some(Box::new(h)); self.poll_handler = Some(Box::new(h));
self self
@ -203,7 +208,7 @@ where
update_listener_error_handler: &'a Eh, update_listener_error_handler: &'a Eh,
) where ) where
UListener: UpdateListener<ListenerE> + 'a, UListener: UpdateListener<ListenerE> + 'a,
Eh: Handler<ListenerE, ()> + 'a, Eh: AsyncHandler<ListenerE, ()> + 'a,
ListenerE: Debug, ListenerE: Debug,
{ {
let update_listener = Box::pin(update_listener); let update_listener = Box::pin(update_listener);

View file

@ -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}; use std::{convert::Infallible, fmt::Debug, future::Future, pin::Pin};
/// A handler that silently ignores all errors. /// A handler that silently ignores all errors.
@ -16,7 +18,7 @@ use std::{convert::Infallible, fmt::Debug, future::Future, pin::Pin};
/// ``` /// ```
pub struct Ignore; pub struct Ignore;
impl<E> Handler<E, ()> for Ignore { impl<E> AsyncHandler<E, ()> for Ignore {
fn handle<'a>(&'a self, _: E) -> Pin<Box<dyn Future<Output = ()> + 'a>> fn handle<'a>(&'a self, _: E) -> Pin<Box<dyn Future<Output = ()> + 'a>>
where where
E: 'a, E: 'a,
@ -59,7 +61,7 @@ impl<E> Handler<E, ()> for Ignore {
pub struct IgnoreSafe; pub struct IgnoreSafe;
#[allow(unreachable_code)] #[allow(unreachable_code)]
impl Handler<Infallible, ()> for IgnoreSafe { impl AsyncHandler<Infallible, ()> for IgnoreSafe {
fn handle<'a>( fn handle<'a>(
&'a self, &'a self,
_: Infallible, _: Infallible,
@ -86,7 +88,7 @@ impl Handler<Infallible, ()> for IgnoreSafe {
/// ``` /// ```
pub struct Log; pub struct Log;
impl<E> Handler<E, ()> for Log impl<E> AsyncHandler<E, ()> for Log
where where
E: Debug, E: Debug,
{ {

View file

@ -1,7 +1,7 @@
use std::{future::Future, pin::Pin}; use std::{future::Future, pin::Pin};
/// An asynchronous polymorphic handler of a context. /// An asynchronous polymorphic handler of a context.
pub trait Handler<Ctx, Output> { pub trait AsyncHandler<Ctx, Output> {
#[must_use] #[must_use]
fn handle<'a>( fn handle<'a>(
&'a self, &'a self,
@ -11,8 +11,7 @@ pub trait Handler<Ctx, Output> {
Ctx: 'a; Ctx: 'a;
} }
/// The implementation of `Handler` for `Fn(Ctx) -> Future<Output = Output>`. impl<Ctx, Output, F, Fut> AsyncHandler<Ctx, Output> for F
impl<Ctx, Output, F, Fut> Handler<Ctx, Output> for F
where where
F: Fn(Ctx) -> Fut, F: Fn(Ctx) -> Fut,
Fut: Future<Output = Output>, Fut: Future<Output = Output>,

View file

@ -31,7 +31,7 @@
mod get_chat_id; mod get_chat_id;
mod storage; mod storage;
use crate::{dispatching::Handler, Bot}; use crate::{dispatching::AsyncHandler, Bot};
pub use get_chat_id::*; pub use get_chat_id::*;
pub use storage::*; pub use storage::*;
@ -86,7 +86,10 @@ where
/// Dispatches a single `message` from a private chat. /// Dispatches a single `message` from a private chat.
pub async fn dispatch<Upd>(&'a self, bot: &'a Bot, update: Upd) pub async fn dispatch<Upd>(&'a self, bot: &'a Bot, update: Upd)
where where
H: Handler<SessionHandlerCtx<'a, Upd, Session>, SessionState<Session>>, H: AsyncHandler<
SessionHandlerCtx<'a, Upd, Session>,
SessionState<Session>,
>,
Upd: GetChatId, Upd: GetChatId,
{ {
let chat_id = update.chat_id(); let chat_id = update.chat_id();