mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-20 13:59:00 +01:00
Added handler_factory.rs
This commit is contained in:
parent
600330f827
commit
6c0f1b9fc4
5 changed files with 74 additions and 36 deletions
|
@ -17,6 +17,7 @@ use teloxide::{
|
||||||
dispatching2::dialogue::{serializer::Json, SqliteStorage},
|
dispatching2::dialogue::{serializer::Json, SqliteStorage},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
};
|
};
|
||||||
|
use teloxide::dispatching2::HandlerFactory;
|
||||||
|
|
||||||
// FIXME: naming
|
// FIXME: naming
|
||||||
type MyBot = AutoSend<Bot>;
|
type MyBot = AutoSend<Bot>;
|
||||||
|
@ -27,8 +28,49 @@ type BotDialogue = Dialogue<State, Store>;
|
||||||
pub enum State {
|
pub enum State {
|
||||||
Start,
|
Start,
|
||||||
ReceiveFullName,
|
ReceiveFullName,
|
||||||
ReceiveAge { full_name: String },
|
ReceiveAge(String),
|
||||||
ReceiveLocation { full_name: String, age: u8 },
|
ReceiveLocation(ReceiveLocation),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, serde::Serialize, serde::Deserialize)]
|
||||||
|
pub struct ReceiveLocation { full_name: String, age: u8 }
|
||||||
|
|
||||||
|
impl HandlerFactory for State {
|
||||||
|
type Out = anyhow::Result<()>;
|
||||||
|
|
||||||
|
fn handler() -> dptree::Handler<'static, DependencyMap, anyhow::Result<()>> {
|
||||||
|
dptree::entry()
|
||||||
|
.branch(
|
||||||
|
dptree::filter(|dialogue: Dialogue<State, Store>| async move {
|
||||||
|
let state = match dialogue.current_state_or_default().await {
|
||||||
|
Ok(state) => state,
|
||||||
|
Err(_) => return false,
|
||||||
|
};
|
||||||
|
match state { State::Start => true, _ => false }
|
||||||
|
}).endpoint(handle_start)
|
||||||
|
)
|
||||||
|
.branch(
|
||||||
|
dptree::filter(|dialogue: Dialogue<State, Store>| async move {
|
||||||
|
let state = match dialogue.current_state_or_default().await {
|
||||||
|
Ok(state) => state,
|
||||||
|
Err(_) => return false,
|
||||||
|
};
|
||||||
|
match state { State::ReceiveFullName => true, _ => false }
|
||||||
|
}).endpoint(handle_receive_full_name)
|
||||||
|
)
|
||||||
|
.branch(
|
||||||
|
dptree::filter_map(|dialogue: Dialogue<State, Store>| async move {
|
||||||
|
let state = dialogue.current_state_or_default().await.ok()?;
|
||||||
|
match state { State::ReceiveAge(arg) => Some(arg), _ => None }
|
||||||
|
}).endpoint(handle_receive_age)
|
||||||
|
)
|
||||||
|
.branch(
|
||||||
|
dptree::filter_map(|dialogue: Dialogue<State, Store>| async move {
|
||||||
|
let state = dialogue.current_state_or_default().await.ok()?;
|
||||||
|
match state { State::ReceiveLocation(arg) => Some(arg), _ => None }
|
||||||
|
}).endpoint(handle_receive_location)
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for State {
|
impl Default for State {
|
||||||
|
@ -48,31 +90,12 @@ async fn main() {
|
||||||
Dispatcher::new(bot)
|
Dispatcher::new(bot)
|
||||||
.dependencies(dptree::deps![storage])
|
.dependencies(dptree::deps![storage])
|
||||||
.messages_handler(|h| {
|
.messages_handler(|h| {
|
||||||
h.add_dialogue::<Message, Store, State>().branch(dptree::endpoint(handle_message))
|
h.add_dialogue::<Message, Store, State>().dispatch_by::<State>()
|
||||||
})
|
})
|
||||||
.dispatch()
|
.dispatch()
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_message(bot: MyBot, mes: Message, dialogue: BotDialogue) -> anyhow::Result<()> {
|
|
||||||
match mes.text().map(ToOwned::to_owned) {
|
|
||||||
None => {
|
|
||||||
bot.send_message(mes.chat_id(), "Send me a text message.").await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
Some(_) => match dialogue.current_state_or_default().await? {
|
|
||||||
State::Start => handle_start(bot, mes, dialogue).await,
|
|
||||||
State::ReceiveFullName => handle_receive_full_name(bot, mes, dialogue).await,
|
|
||||||
State::ReceiveAge { full_name } => {
|
|
||||||
handle_receive_age(bot, mes, dialogue, full_name).await
|
|
||||||
}
|
|
||||||
State::ReceiveLocation { full_name, age } => {
|
|
||||||
handle_receive_location(bot, mes, dialogue, full_name, age).await
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn handle_start(bot: MyBot, mes: Message, dialogue: BotDialogue) -> anyhow::Result<()> {
|
async fn handle_start(bot: MyBot, mes: Message, dialogue: BotDialogue) -> anyhow::Result<()> {
|
||||||
bot.send_message(mes.chat_id(), "Let's start! What's your full name?").await?;
|
bot.send_message(mes.chat_id(), "Let's start! What's your full name?").await?;
|
||||||
dialogue.next(State::ReceiveFullName).await?;
|
dialogue.next(State::ReceiveFullName).await?;
|
||||||
|
@ -85,7 +108,7 @@ async fn handle_receive_full_name(
|
||||||
dialogue: BotDialogue,
|
dialogue: BotDialogue,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
bot.send_message(mes.chat_id(), "How old are you?").await?;
|
bot.send_message(mes.chat_id(), "How old are you?").await?;
|
||||||
dialogue.next(State::ReceiveAge { full_name: mes.text().unwrap().into() }).await?;
|
dialogue.next(State::ReceiveAge(mes.text().unwrap().into())).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,7 +121,7 @@ async fn handle_receive_age(
|
||||||
match mes.text().unwrap().parse::<u8>() {
|
match mes.text().unwrap().parse::<u8>() {
|
||||||
Ok(age) => {
|
Ok(age) => {
|
||||||
bot.send_message(mes.chat_id(), "What's your location?").await?;
|
bot.send_message(mes.chat_id(), "What's your location?").await?;
|
||||||
dialogue.next(State::ReceiveLocation { full_name, age }).await?;
|
dialogue.next(State::ReceiveLocation(ReceiveLocation { full_name, age })).await?;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
bot.send_message(mes.chat_id(), "Send me a number.").await?;
|
bot.send_message(mes.chat_id(), "Send me a number.").await?;
|
||||||
|
@ -111,11 +134,10 @@ async fn handle_receive_location(
|
||||||
bot: MyBot,
|
bot: MyBot,
|
||||||
mes: Message,
|
mes: Message,
|
||||||
dialogue: BotDialogue,
|
dialogue: BotDialogue,
|
||||||
full_name: String,
|
state: ReceiveLocation
|
||||||
age: u8,
|
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
let location = mes.text().unwrap();
|
let location = mes.text().unwrap();
|
||||||
let message = format!("Full name: {}\nAge: {}\nLocation: {}", full_name, age, location);
|
let message = format!("Full name: {}\nAge: {}\nLocation: {}", state.full_name, state.age, location);
|
||||||
bot.send_message(mes.chat_id(), message).await?;
|
bot.send_message(mes.chat_id(), message).await?;
|
||||||
dialogue.exit().await?;
|
dialogue.exit().await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -1,30 +1,34 @@
|
||||||
use crate::{types::Message, utils::command::BotCommand};
|
use crate::{types::Message, utils::command::BotCommand};
|
||||||
use dptree::{
|
use dptree::{
|
||||||
di::{DependencySupplier, Insert},
|
|
||||||
Handler,
|
Handler,
|
||||||
};
|
};
|
||||||
|
use crate::dispatching2::HandlerFactory;
|
||||||
|
use dptree::di::DependencyMap;
|
||||||
|
|
||||||
pub trait HandlerExt<IR> {
|
pub trait HandlerExt<Output> {
|
||||||
fn add_command<C>(self, bot_name: String) -> Self
|
fn add_command<C>(self, bot_name: String) -> Self
|
||||||
where
|
where
|
||||||
C: BotCommand + Send,
|
C: BotCommand + Send + Sync + 'static;
|
||||||
IR: Insert<C>;
|
|
||||||
|
fn dispatch_by<F>(self) -> Self
|
||||||
|
where F: HandlerFactory<Out = Output>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Input, Output, IR> HandlerExt<IR> for Handler<'_, Input, Output, IR>
|
impl<Output> HandlerExt<Output> for Handler<'static, DependencyMap, Output>
|
||||||
where
|
where
|
||||||
Input: Send + Sync + 'static,
|
|
||||||
Output: Send + Sync + 'static,
|
Output: Send + Sync + 'static,
|
||||||
IR: Send + Sync + 'static + Clone + DependencySupplier<Message>,
|
|
||||||
{
|
{
|
||||||
fn add_command<C>(self, bot_name: String) -> Self
|
fn add_command<C>(self, bot_name: String) -> Self
|
||||||
where
|
where
|
||||||
C: BotCommand + Send,
|
C: BotCommand + Send + Sync + 'static,
|
||||||
IR: Insert<C>,
|
|
||||||
{
|
{
|
||||||
self.chain(dptree::filter_map(move |message: Message| {
|
self.chain(dptree::filter_map(move |message: Message| {
|
||||||
let bot_name = bot_name.clone();
|
let bot_name = bot_name.clone();
|
||||||
async move { message.text().and_then(|text| C::parse(text, bot_name).ok()) }
|
async move { message.text().and_then(|text| C::parse(text, bot_name).ok()) }
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn dispatch_by<F>(self) -> Self where F: HandlerFactory<Out = Output> {
|
||||||
|
self.chain(F::handler())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
8
src/dispatching2/handler_factory.rs
Normal file
8
src/dispatching2/handler_factory.rs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
use dptree::di::DependencyMap;
|
||||||
|
use dptree::Handler;
|
||||||
|
|
||||||
|
pub trait HandlerFactory {
|
||||||
|
type Out;
|
||||||
|
|
||||||
|
fn handler() -> Handler<'static, DependencyMap, Self::Out>;
|
||||||
|
}
|
|
@ -3,5 +3,8 @@ pub(crate) mod repls;
|
||||||
pub mod dialogue;
|
pub mod dialogue;
|
||||||
mod dispatcher;
|
mod dispatcher;
|
||||||
mod handler_ext;
|
mod handler_ext;
|
||||||
|
mod handler_factory;
|
||||||
|
|
||||||
pub use dispatcher::Dispatcher;
|
pub use dispatcher::Dispatcher;
|
||||||
|
pub use handler_factory::HandlerFactory;
|
||||||
|
pub use handler_ext::HandlerExt;
|
||||||
|
|
|
@ -17,6 +17,7 @@ pub use crate::dispatching::{
|
||||||
#[cfg(feature = "new-dispatching")]
|
#[cfg(feature = "new-dispatching")]
|
||||||
pub use crate::dispatching2::{
|
pub use crate::dispatching2::{
|
||||||
dialogue::{Dialogue, DialogueHandlerExt as _},
|
dialogue::{Dialogue, DialogueHandlerExt as _},
|
||||||
|
HandlerExt as _,
|
||||||
Dispatcher,
|
Dispatcher,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue