mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 22:46:39 +01:00
Update the examples to use teloxide::handler!
This commit is contained in:
parent
9cd4bb11b0
commit
363e0bff18
3 changed files with 58 additions and 70 deletions
39
README.md
39
README.md
|
@ -190,23 +190,16 @@ Below is a bot that asks you three questions and then sends the answers back to
|
||||||
([Full](examples/dialogue.rs))
|
([Full](examples/dialogue.rs))
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
use teloxide::{dispatching::dialogue::InMemStorage, macros::DialogueState, prelude::*};
|
use teloxide::{dispatching::dialogue::InMemStorage, prelude::*};
|
||||||
|
|
||||||
type MyDialogue = Dialogue<State, InMemStorage<State>>;
|
type MyDialogue = Dialogue<State, InMemStorage<State>>;
|
||||||
|
type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
|
||||||
|
|
||||||
#[derive(DialogueState, Clone)]
|
#[derive(Clone)]
|
||||||
#[handler_out(anyhow::Result<()>)]
|
|
||||||
pub enum State {
|
pub enum State {
|
||||||
#[handler(handle_start)]
|
|
||||||
Start,
|
Start,
|
||||||
|
|
||||||
#[handler(handle_receive_full_name)]
|
|
||||||
ReceiveFullName,
|
ReceiveFullName,
|
||||||
|
|
||||||
#[handler(handle_receive_age)]
|
|
||||||
ReceiveAge { full_name: String },
|
ReceiveAge { full_name: String },
|
||||||
|
|
||||||
#[handler(handle_receive_location)]
|
|
||||||
ReceiveLocation { full_name: String, age: u8 },
|
ReceiveLocation { full_name: String, age: u8 },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,7 +220,13 @@ async fn main() {
|
||||||
bot,
|
bot,
|
||||||
Update::filter_message()
|
Update::filter_message()
|
||||||
.enter_dialogue::<Message, InMemStorage<State>, State>()
|
.enter_dialogue::<Message, InMemStorage<State>, State>()
|
||||||
.dispatch_by::<State>(),
|
.branch(teloxide::handler![State::Start].endpoint(start))
|
||||||
|
.branch(teloxide::handler![State::ReceiveFullName].endpoint(receive_full_name))
|
||||||
|
.branch(teloxide::handler![State::ReceiveAge { full_name }].endpoint(receive_age))
|
||||||
|
.branch(
|
||||||
|
teloxide::handler![State::ReceiveLocation { full_name, age }]
|
||||||
|
.endpoint(receive_location),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
.dependencies(dptree::deps![InMemStorage::<State>::new()])
|
.dependencies(dptree::deps![InMemStorage::<State>::new()])
|
||||||
.build()
|
.build()
|
||||||
|
@ -236,21 +235,17 @@ async fn main() {
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_start(
|
async fn start(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||||
bot: AutoSend<Bot>,
|
|
||||||
msg: Message,
|
|
||||||
dialogue: MyDialogue,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
|
bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
|
||||||
dialogue.update(State::ReceiveFullName).await?;
|
dialogue.update(State::ReceiveFullName).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_receive_full_name(
|
async fn receive_full_name(
|
||||||
bot: AutoSend<Bot>,
|
bot: AutoSend<Bot>,
|
||||||
msg: Message,
|
msg: Message,
|
||||||
dialogue: MyDialogue,
|
dialogue: MyDialogue,
|
||||||
) -> anyhow::Result<()> {
|
) -> HandlerResult {
|
||||||
match msg.text() {
|
match msg.text() {
|
||||||
Some(text) => {
|
Some(text) => {
|
||||||
bot.send_message(msg.chat.id, "How old are you?").await?;
|
bot.send_message(msg.chat.id, "How old are you?").await?;
|
||||||
|
@ -264,12 +259,12 @@ async fn handle_receive_full_name(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_receive_age(
|
async fn receive_age(
|
||||||
bot: AutoSend<Bot>,
|
bot: AutoSend<Bot>,
|
||||||
msg: Message,
|
msg: Message,
|
||||||
dialogue: MyDialogue,
|
dialogue: MyDialogue,
|
||||||
(full_name,): (String,), // Available from `State::ReceiveAge`.
|
(full_name,): (String,), // Available from `State::ReceiveAge`.
|
||||||
) -> anyhow::Result<()> {
|
) -> HandlerResult {
|
||||||
match msg.text().map(|text| text.parse::<u8>()) {
|
match msg.text().map(|text| text.parse::<u8>()) {
|
||||||
Some(Ok(age)) => {
|
Some(Ok(age)) => {
|
||||||
bot.send_message(msg.chat.id, "What's your location?").await?;
|
bot.send_message(msg.chat.id, "What's your location?").await?;
|
||||||
|
@ -283,12 +278,12 @@ async fn handle_receive_age(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_receive_location(
|
async fn receive_location(
|
||||||
bot: AutoSend<Bot>,
|
bot: AutoSend<Bot>,
|
||||||
msg: Message,
|
msg: Message,
|
||||||
dialogue: MyDialogue,
|
dialogue: MyDialogue,
|
||||||
(full_name, age): (String, u8), // Available from `State::ReceiveLocation`.
|
(full_name, age): (String, u8), // Available from `State::ReceiveLocation`.
|
||||||
) -> anyhow::Result<()> {
|
) -> HandlerResult {
|
||||||
match msg.text() {
|
match msg.text() {
|
||||||
Some(location) => {
|
Some(location) => {
|
||||||
let message = format!("Full name: {}\nAge: {}\nLocation: {}", full_name, age, location);
|
let message = format!("Full name: {}\nAge: {}\nLocation: {}", full_name, age, location);
|
||||||
|
|
|
@ -6,9 +6,7 @@ use teloxide::{
|
||||||
serializer::{Bincode, Json},
|
serializer::{Bincode, Json},
|
||||||
ErasedStorage, RedisStorage, SqliteStorage, Storage,
|
ErasedStorage, RedisStorage, SqliteStorage, Storage,
|
||||||
},
|
},
|
||||||
macros::DialogueState,
|
|
||||||
prelude::*,
|
prelude::*,
|
||||||
types::Me,
|
|
||||||
utils::command::BotCommand,
|
utils::command::BotCommand,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -16,13 +14,9 @@ type MyDialogue = Dialogue<State, ErasedStorage<State>>;
|
||||||
type MyStorage = std::sync::Arc<ErasedStorage<State>>;
|
type MyStorage = std::sync::Arc<ErasedStorage<State>>;
|
||||||
type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
|
type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
|
||||||
|
|
||||||
#[derive(DialogueState, Clone, serde::Serialize, serde::Deserialize)]
|
#[derive(Clone, serde::Serialize, serde::Deserialize)]
|
||||||
#[handler_out(HandlerResult)]
|
|
||||||
pub enum State {
|
pub enum State {
|
||||||
#[handler(handle_start)]
|
|
||||||
Start,
|
Start,
|
||||||
|
|
||||||
#[handler(handle_got_number)]
|
|
||||||
GotNumber(i32),
|
GotNumber(i32),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,7 +26,7 @@ impl Default for State {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(BotCommand)]
|
#[derive(BotCommand, Clone)]
|
||||||
#[command(rename = "lowercase", description = "These commands are supported:")]
|
#[command(rename = "lowercase", description = "These commands are supported:")]
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
#[command(description = "get your number.")]
|
#[command(description = "get your number.")]
|
||||||
|
@ -56,7 +50,12 @@ async fn main() {
|
||||||
|
|
||||||
let handler = Update::filter_message()
|
let handler = Update::filter_message()
|
||||||
.enter_dialogue::<Message, ErasedStorage<State>, State>()
|
.enter_dialogue::<Message, ErasedStorage<State>, State>()
|
||||||
.dispatch_by::<State>();
|
.branch(teloxide::handler![State::Start].endpoint(start))
|
||||||
|
.branch(
|
||||||
|
teloxide::handler![State::GotNumber(n)]
|
||||||
|
.branch(dptree::entry().filter_command::<Command>().endpoint(got_number))
|
||||||
|
.branch(dptree::endpoint(invalid_command)),
|
||||||
|
);
|
||||||
|
|
||||||
Dispatcher::builder(bot, handler)
|
Dispatcher::builder(bot, handler)
|
||||||
.dependencies(dptree::deps![storage])
|
.dependencies(dptree::deps![storage])
|
||||||
|
@ -66,48 +65,44 @@ async fn main() {
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_start(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
async fn start(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||||
match msg.text().unwrap().parse() {
|
match msg.text().map(|text| text.parse::<i32>()) {
|
||||||
Ok(number) => {
|
Some(Ok(n)) => {
|
||||||
dialogue.update(State::GotNumber(number)).await?;
|
dialogue.update(State::GotNumber(n)).await?;
|
||||||
bot.send_message(
|
bot.send_message(
|
||||||
msg.chat.id,
|
msg.chat.id,
|
||||||
format!("Remembered number {}. Now use /get or /reset", number),
|
format!("Remembered number {}. Now use /get or /reset.", n),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
bot.send_message(msg.chat.id, "Please, send me a number").await?;
|
bot.send_message(msg.chat.id, "Please, send me a number.").await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_got_number(
|
async fn got_number(
|
||||||
bot: AutoSend<Bot>,
|
bot: AutoSend<Bot>,
|
||||||
msg: Message,
|
msg: Message,
|
||||||
dialogue: MyDialogue,
|
dialogue: MyDialogue,
|
||||||
num: i32,
|
num: i32,
|
||||||
me: Me,
|
cmd: Command,
|
||||||
) -> HandlerResult {
|
) -> HandlerResult {
|
||||||
let ans = msg.text().unwrap();
|
match cmd {
|
||||||
let bot_name = me.user.username.unwrap();
|
Command::Get => {
|
||||||
|
bot.send_message(msg.chat.id, format!("Here is your number: {}.", num)).await?;
|
||||||
match Command::parse(ans, bot_name) {
|
}
|
||||||
Ok(cmd) => match cmd {
|
Command::Reset => {
|
||||||
Command::Get => {
|
dialogue.reset().await?;
|
||||||
bot.send_message(msg.chat.id, format!("Here is your number: {}", num)).await?;
|
bot.send_message(msg.chat.id, "Number resetted.").await?;
|
||||||
}
|
|
||||||
Command::Reset => {
|
|
||||||
dialogue.reset().await?;
|
|
||||||
bot.send_message(msg.chat.id, "Number resetted").await?;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Err(_) => {
|
|
||||||
bot.send_message(msg.chat.id, "Please, send /get or /reset").await?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn invalid_command(bot: AutoSend<Bot>, msg: Message) -> HandlerResult {
|
||||||
|
bot.send_message(msg.chat.id, "Please, send /get or /reset.").await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,24 +13,16 @@
|
||||||
// Age: 223
|
// Age: 223
|
||||||
// Location: Middle-earth
|
// Location: Middle-earth
|
||||||
// ```
|
// ```
|
||||||
use teloxide::{dispatching::dialogue::InMemStorage, macros::DialogueState, prelude::*};
|
use teloxide::{dispatching::dialogue::InMemStorage, prelude::*};
|
||||||
|
|
||||||
type MyDialogue = Dialogue<State, InMemStorage<State>>;
|
type MyDialogue = Dialogue<State, InMemStorage<State>>;
|
||||||
type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
|
type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
|
||||||
|
|
||||||
#[derive(DialogueState, Clone)]
|
#[derive(Clone)]
|
||||||
#[handler_out(HandlerResult)]
|
|
||||||
pub enum State {
|
pub enum State {
|
||||||
#[handler(handle_start)]
|
|
||||||
Start,
|
Start,
|
||||||
|
|
||||||
#[handler(handle_receive_full_name)]
|
|
||||||
ReceiveFullName,
|
ReceiveFullName,
|
||||||
|
|
||||||
#[handler(handle_receive_age)]
|
|
||||||
ReceiveAge { full_name: String },
|
ReceiveAge { full_name: String },
|
||||||
|
|
||||||
#[handler(handle_receive_location)]
|
|
||||||
ReceiveLocation { full_name: String, age: u8 },
|
ReceiveLocation { full_name: String, age: u8 },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,7 +43,13 @@ async fn main() {
|
||||||
bot,
|
bot,
|
||||||
Update::filter_message()
|
Update::filter_message()
|
||||||
.enter_dialogue::<Message, InMemStorage<State>, State>()
|
.enter_dialogue::<Message, InMemStorage<State>, State>()
|
||||||
.dispatch_by::<State>(),
|
.branch(teloxide::handler![State::Start].endpoint(start))
|
||||||
|
.branch(teloxide::handler![State::ReceiveFullName].endpoint(receive_full_name))
|
||||||
|
.branch(teloxide::handler![State::ReceiveAge { full_name }].endpoint(receive_age))
|
||||||
|
.branch(
|
||||||
|
teloxide::handler![State::ReceiveLocation { full_name, age }]
|
||||||
|
.endpoint(receive_location),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
.dependencies(dptree::deps![InMemStorage::<State>::new()])
|
.dependencies(dptree::deps![InMemStorage::<State>::new()])
|
||||||
.build()
|
.build()
|
||||||
|
@ -60,13 +58,13 @@ async fn main() {
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_start(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
async fn start(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||||
bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
|
bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
|
||||||
dialogue.update(State::ReceiveFullName).await?;
|
dialogue.update(State::ReceiveFullName).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_receive_full_name(
|
async fn receive_full_name(
|
||||||
bot: AutoSend<Bot>,
|
bot: AutoSend<Bot>,
|
||||||
msg: Message,
|
msg: Message,
|
||||||
dialogue: MyDialogue,
|
dialogue: MyDialogue,
|
||||||
|
@ -84,7 +82,7 @@ async fn handle_receive_full_name(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_receive_age(
|
async fn receive_age(
|
||||||
bot: AutoSend<Bot>,
|
bot: AutoSend<Bot>,
|
||||||
msg: Message,
|
msg: Message,
|
||||||
dialogue: MyDialogue,
|
dialogue: MyDialogue,
|
||||||
|
@ -103,7 +101,7 @@ async fn handle_receive_age(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_receive_location(
|
async fn receive_location(
|
||||||
bot: AutoSend<Bot>,
|
bot: AutoSend<Bot>,
|
||||||
msg: Message,
|
msg: Message,
|
||||||
dialogue: MyDialogue,
|
dialogue: MyDialogue,
|
||||||
|
|
Loading…
Reference in a new issue