mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
Trying to fix ping_pong_bot.rs
This commit is contained in:
parent
3bad400c03
commit
27ae3e13cf
5 changed files with 65 additions and 62 deletions
|
@ -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<Message, ()>| async move {
|
||||
ctx.bot
|
||||
.send_message(ctx.update.chat.id, "pong")
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
SessionState::Continue(())
|
||||
},
|
||||
))
|
||||
.dispatch()
|
||||
.await
|
||||
}
|
||||
|
|
|
@ -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<dyn Handler<HandlerE, ()> + 'a>,
|
||||
handlers_error_handler: Box<dyn AsyncHandler<HandlerE, ()> + 'a>,
|
||||
|
||||
private_message_dp: Option<SessionDispatcher<'a, Session1, H1>>,
|
||||
private_edited_message_dp: Option<SessionDispatcher<'a, Session2, H2>>,
|
||||
|
||||
message_handler:
|
||||
Option<Box<dyn Handler<BasicHandlerCtx<'a, Message>, ()> + 'a>>,
|
||||
Option<Box<dyn AsyncHandler<BasicHandlerCtx<'a, Message>, ()> + 'a>>,
|
||||
edited_message_handler:
|
||||
Option<Box<dyn Handler<BasicHandlerCtx<'a, Message>, ()> + 'a>>,
|
||||
Option<Box<dyn AsyncHandler<BasicHandlerCtx<'a, Message>, ()> + 'a>>,
|
||||
channel_post_handler:
|
||||
Option<Box<dyn Handler<BasicHandlerCtx<'a, Message>, ()> + 'a>>,
|
||||
Option<Box<dyn AsyncHandler<BasicHandlerCtx<'a, Message>, ()> + 'a>>,
|
||||
edited_channel_post_handler:
|
||||
Option<Box<dyn Handler<BasicHandlerCtx<'a, Message>, ()> + 'a>>,
|
||||
inline_query_handler:
|
||||
Option<Box<dyn Handler<BasicHandlerCtx<'a, InlineQuery>, ()> + 'a>>,
|
||||
Option<Box<dyn AsyncHandler<BasicHandlerCtx<'a, Message>, ()> + 'a>>,
|
||||
inline_query_handler: Option<
|
||||
Box<dyn AsyncHandler<BasicHandlerCtx<'a, InlineQuery>, ()> + 'a>,
|
||||
>,
|
||||
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<
|
||||
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>
|
||||
|
@ -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<Session1>,
|
||||
> + 'a,
|
||||
H2: Handler<
|
||||
H2: AsyncHandler<
|
||||
SessionHandlerCtx<'a, Message, Session2>,
|
||||
SessionState<Session2>,
|
||||
> + 'a,
|
||||
|
@ -87,7 +92,7 @@ where
|
|||
|
||||
pub fn handlers_error_handler<T>(mut self, val: T) -> Self
|
||||
where
|
||||
T: Handler<HandlerE, ()> + 'a,
|
||||
T: AsyncHandler<HandlerE, ()> + 'a,
|
||||
{
|
||||
self.handlers_error_handler = Box::new(val);
|
||||
self
|
||||
|
@ -111,7 +116,7 @@ where
|
|||
|
||||
pub fn message_handler<H>(mut self, h: H) -> Self
|
||||
where
|
||||
H: Handler<BasicHandlerCtx<'a, Message>, ()> + 'a,
|
||||
H: AsyncHandler<BasicHandlerCtx<'a, Message>, ()> + 'a,
|
||||
{
|
||||
self.message_handler = Some(Box::new(h));
|
||||
self
|
||||
|
@ -119,7 +124,7 @@ where
|
|||
|
||||
pub fn edited_message_handler<H>(mut self, h: H) -> Self
|
||||
where
|
||||
H: Handler<BasicHandlerCtx<'a, Message>, ()> + 'a,
|
||||
H: AsyncHandler<BasicHandlerCtx<'a, Message>, ()> + 'a,
|
||||
{
|
||||
self.edited_message_handler = Some(Box::new(h));
|
||||
self
|
||||
|
@ -127,7 +132,7 @@ where
|
|||
|
||||
pub fn channel_post_handler<H>(mut self, h: H) -> Self
|
||||
where
|
||||
H: Handler<BasicHandlerCtx<'a, Message>, ()> + 'a,
|
||||
H: AsyncHandler<BasicHandlerCtx<'a, Message>, ()> + 'a,
|
||||
{
|
||||
self.channel_post_handler = Some(Box::new(h));
|
||||
self
|
||||
|
@ -135,7 +140,7 @@ where
|
|||
|
||||
pub fn edited_channel_post_handler<H>(mut self, h: H) -> Self
|
||||
where
|
||||
H: Handler<BasicHandlerCtx<'a, Message>, ()> + 'a,
|
||||
H: AsyncHandler<BasicHandlerCtx<'a, Message>, ()> + 'a,
|
||||
{
|
||||
self.edited_channel_post_handler = Some(Box::new(h));
|
||||
self
|
||||
|
@ -143,7 +148,7 @@ where
|
|||
|
||||
pub fn inline_query_handler<H>(mut self, h: H) -> Self
|
||||
where
|
||||
H: Handler<BasicHandlerCtx<'a, InlineQuery>, ()> + 'a,
|
||||
H: AsyncHandler<BasicHandlerCtx<'a, InlineQuery>, ()> + 'a,
|
||||
{
|
||||
self.inline_query_handler = Some(Box::new(h));
|
||||
self
|
||||
|
@ -151,7 +156,7 @@ where
|
|||
|
||||
pub fn chosen_inline_result_handler<H>(mut self, h: H) -> Self
|
||||
where
|
||||
H: Handler<BasicHandlerCtx<'a, ChosenInlineResult>, ()> + 'a,
|
||||
H: AsyncHandler<BasicHandlerCtx<'a, ChosenInlineResult>, ()> + 'a,
|
||||
{
|
||||
self.chosen_inline_result_handler = Some(Box::new(h));
|
||||
self
|
||||
|
@ -159,7 +164,7 @@ where
|
|||
|
||||
pub fn callback_query_handler<H>(mut self, h: H) -> Self
|
||||
where
|
||||
H: Handler<BasicHandlerCtx<'a, CallbackQuery>, ()> + 'a,
|
||||
H: AsyncHandler<BasicHandlerCtx<'a, CallbackQuery>, ()> + 'a,
|
||||
{
|
||||
self.callback_query_handler = Some(Box::new(h));
|
||||
self
|
||||
|
@ -167,7 +172,7 @@ where
|
|||
|
||||
pub fn shipping_query_handler<H>(mut self, h: H) -> Self
|
||||
where
|
||||
H: Handler<BasicHandlerCtx<'a, ShippingQuery>, ()> + 'a,
|
||||
H: AsyncHandler<BasicHandlerCtx<'a, ShippingQuery>, ()> + 'a,
|
||||
{
|
||||
self.shipping_query_handler = Some(Box::new(h));
|
||||
self
|
||||
|
@ -175,7 +180,7 @@ where
|
|||
|
||||
pub fn pre_checkout_query_handler<H>(mut self, h: H) -> Self
|
||||
where
|
||||
H: Handler<BasicHandlerCtx<'a, PreCheckoutQuery>, ()> + 'a,
|
||||
H: AsyncHandler<BasicHandlerCtx<'a, PreCheckoutQuery>, ()> + 'a,
|
||||
{
|
||||
self.pre_checkout_query_handler = Some(Box::new(h));
|
||||
self
|
||||
|
@ -183,7 +188,7 @@ where
|
|||
|
||||
pub fn poll_handler<H>(mut self, h: H) -> Self
|
||||
where
|
||||
H: Handler<BasicHandlerCtx<'a, Poll>, ()> + 'a,
|
||||
H: AsyncHandler<BasicHandlerCtx<'a, Poll>, ()> + 'a,
|
||||
{
|
||||
self.poll_handler = Some(Box::new(h));
|
||||
self
|
||||
|
@ -203,7 +208,7 @@ where
|
|||
update_listener_error_handler: &'a Eh,
|
||||
) where
|
||||
UListener: UpdateListener<ListenerE> + 'a,
|
||||
Eh: Handler<ListenerE, ()> + 'a,
|
||||
Eh: AsyncHandler<ListenerE, ()> + 'a,
|
||||
ListenerE: Debug,
|
||||
{
|
||||
let update_listener = Box::pin(update_listener);
|
||||
|
|
|
@ -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<E> Handler<E, ()> for Ignore {
|
||||
impl<E> AsyncHandler<E, ()> for Ignore {
|
||||
fn handle<'a>(&'a self, _: E) -> Pin<Box<dyn Future<Output = ()> + 'a>>
|
||||
where
|
||||
E: 'a,
|
||||
|
@ -59,7 +61,7 @@ impl<E> Handler<E, ()> for Ignore {
|
|||
pub struct IgnoreSafe;
|
||||
|
||||
#[allow(unreachable_code)]
|
||||
impl Handler<Infallible, ()> for IgnoreSafe {
|
||||
impl AsyncHandler<Infallible, ()> for IgnoreSafe {
|
||||
fn handle<'a>(
|
||||
&'a self,
|
||||
_: Infallible,
|
||||
|
@ -86,7 +88,7 @@ impl Handler<Infallible, ()> for IgnoreSafe {
|
|||
/// ```
|
||||
pub struct Log;
|
||||
|
||||
impl<E> Handler<E, ()> for Log
|
||||
impl<E> AsyncHandler<E, ()> for Log
|
||||
where
|
||||
E: Debug,
|
||||
{
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use std::{future::Future, pin::Pin};
|
||||
|
||||
/// An asynchronous polymorphic handler of a context.
|
||||
pub trait Handler<Ctx, Output> {
|
||||
pub trait AsyncHandler<Ctx, Output> {
|
||||
#[must_use]
|
||||
fn handle<'a>(
|
||||
&'a self,
|
||||
|
@ -11,8 +11,7 @@ pub trait Handler<Ctx, Output> {
|
|||
Ctx: 'a;
|
||||
}
|
||||
|
||||
/// The implementation of `Handler` for `Fn(Ctx) -> Future<Output = Output>`.
|
||||
impl<Ctx, Output, F, Fut> Handler<Ctx, Output> for F
|
||||
impl<Ctx, Output, F, Fut> AsyncHandler<Ctx, Output> for F
|
||||
where
|
||||
F: Fn(Ctx) -> Fut,
|
||||
Fut: Future<Output = Output>,
|
||||
|
|
|
@ -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<Upd>(&'a self, bot: &'a Bot, update: Upd)
|
||||
where
|
||||
H: Handler<SessionHandlerCtx<'a, Upd, Session>, SessionState<Session>>,
|
||||
H: AsyncHandler<
|
||||
SessionHandlerCtx<'a, Upd, Session>,
|
||||
SessionState<Session>,
|
||||
>,
|
||||
Upd: GetChatId,
|
||||
{
|
||||
let chat_id = update.chat_id();
|
||||
|
|
Loading…
Reference in a new issue