Move the dispatch schema to a function (examples/purchase.rs)

This commit is contained in:
Hirrolot 2022-04-26 20:50:55 +06:00
parent 056b6df3eb
commit 51d00ec351

View file

@ -13,7 +13,10 @@
// ``` // ```
use teloxide::{ use teloxide::{
dispatching::dialogue::{self, InMemStorage}, dispatching::{
dialogue::{self, InMemStorage},
UpdateHandler,
},
prelude::*, prelude::*,
types::{InlineKeyboardButton, InlineKeyboardMarkup}, types::{InlineKeyboardButton, InlineKeyboardMarkup},
utils::command::BotCommands, utils::command::BotCommands,
@ -53,6 +56,15 @@ async fn main() {
let bot = Bot::from_env().auto_send(); let bot = Bot::from_env().auto_send();
Dispatcher::builder(bot, schema())
.dependencies(dptree::deps![InMemStorage::<State>::new()])
.build()
.setup_ctrlc_handler()
.dispatch()
.await;
}
fn schema() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>> {
let command_handler = teloxide::filter_command::<Command, _>() let command_handler = teloxide::filter_command::<Command, _>()
.branch( .branch(
teloxide::handler![State::Start] teloxide::handler![State::Start]
@ -71,17 +83,9 @@ async fn main() {
.endpoint(receive_product_selection), .endpoint(receive_product_selection),
); );
Dispatcher::builder( dialogue::enter::<Update, InMemStorage<State>, State, _>()
bot, .branch(message_handler)
dialogue::enter::<Update, InMemStorage<State>, State, _>() .branch(callback_query_handler)
.branch(message_handler)
.branch(callback_query_handler),
)
.dependencies(dptree::deps![InMemStorage::<State>::new()])
.build()
.setup_ctrlc_handler()
.dispatch()
.await;
} }
async fn start(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult { async fn start(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult {