mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-05 10:24:32 +01:00
Update examples/redis_remember_bot
This commit is contained in:
parent
09ec40305d
commit
aa0b2c7d1e
4 changed files with 33 additions and 29 deletions
|
@ -9,7 +9,10 @@ tokio = "0.2.9"
|
||||||
|
|
||||||
# You can also choose "cbor-serializer" or built-in JSON serializer
|
# You can also choose "cbor-serializer" or built-in JSON serializer
|
||||||
teloxide = { path = "../../", features = ["redis-storage", "bincode-serializer"] }
|
teloxide = { path = "../../", features = ["redis-storage", "bincode-serializer"] }
|
||||||
|
teloxide-macros = { git = "http://github.com/teloxide/teloxide-macros", branch = "master" }
|
||||||
|
|
||||||
serde = "1.0.104"
|
serde = "1.0.104"
|
||||||
|
futures = "0.3.5"
|
||||||
|
|
||||||
thiserror = "1.0.15"
|
thiserror = "1.0.15"
|
||||||
smart-default = "0.6.0"
|
smart-default = "0.6.0"
|
||||||
|
|
|
@ -7,7 +7,6 @@ mod states;
|
||||||
mod transitions;
|
mod transitions;
|
||||||
|
|
||||||
use states::*;
|
use states::*;
|
||||||
use transitions::*;
|
|
||||||
|
|
||||||
use teloxide::{
|
use teloxide::{
|
||||||
dispatching::dialogue::{serializer::Bincode, RedisStorage, Storage},
|
dispatching::dialogue::{serializer::Bincode, RedisStorage, Storage},
|
||||||
|
@ -27,18 +26,6 @@ enum Error {
|
||||||
|
|
||||||
type In = DialogueWithCx<Message, Dialogue, StorageError>;
|
type In = DialogueWithCx<Message, Dialogue, StorageError>;
|
||||||
|
|
||||||
async fn handle_message(input: In) -> Out {
|
|
||||||
let (cx, dialogue) = input.unpack();
|
|
||||||
|
|
||||||
match cx.update.text_owned() {
|
|
||||||
Some(text) => dispatch(cx, dialogue, &text).await,
|
|
||||||
None => {
|
|
||||||
cx.answer_str("Please, send me a text message").await?;
|
|
||||||
next(StartState)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
run().await;
|
run().await;
|
||||||
|
@ -48,8 +35,11 @@ async fn run() {
|
||||||
let bot = Bot::from_env();
|
let bot = Bot::from_env();
|
||||||
Dispatcher::new(bot)
|
Dispatcher::new(bot)
|
||||||
.messages_handler(DialogueDispatcher::with_storage(
|
.messages_handler(DialogueDispatcher::with_storage(
|
||||||
|cx| async move {
|
|input: In| async move {
|
||||||
handle_message(cx)
|
let (cx, dialogue) = input.unpack();
|
||||||
|
|
||||||
|
dialogue
|
||||||
|
.dispatch(cx)
|
||||||
.await
|
.await
|
||||||
.expect("Something is wrong with the bot!")
|
.expect("Something is wrong with the bot!")
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
use teloxide::prelude::*;
|
use teloxide::prelude::*;
|
||||||
|
use teloxide_macros::BotDialogue;
|
||||||
|
|
||||||
|
use super::transitions::{have_number, start};
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
@ -15,9 +18,12 @@ up!(
|
||||||
StartState + [number: i32] -> HaveNumberState,
|
StartState + [number: i32] -> HaveNumberState,
|
||||||
);
|
);
|
||||||
|
|
||||||
#[derive(SmartDefault, From, Serialize, Deserialize)]
|
#[derive(BotDialogue, SmartDefault, From, Serialize, Deserialize)]
|
||||||
pub enum Dialogue {
|
pub enum Dialogue {
|
||||||
#[default]
|
#[default]
|
||||||
|
#[transition(start)]
|
||||||
Start(StartState),
|
Start(StartState),
|
||||||
|
|
||||||
|
#[transition(have_number)]
|
||||||
HaveNumber(HaveNumberState),
|
HaveNumber(HaveNumberState),
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,24 @@ use teloxide::prelude::*;
|
||||||
|
|
||||||
use super::states::*;
|
use super::states::*;
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! extract_text {
|
||||||
|
($cx:ident) => {
|
||||||
|
match $cx.update.text_owned() {
|
||||||
|
Some(text) => text,
|
||||||
|
None => {
|
||||||
|
$cx.answer_str("Please, send me a text message").await?;
|
||||||
|
return next(StartState);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
pub type Out = TransitionOut<Dialogue>;
|
pub type Out = TransitionOut<Dialogue>;
|
||||||
|
|
||||||
async fn start(cx: TransitionIn, state: StartState, text: &str) -> Out {
|
pub async fn start(cx: TransitionIn, state: StartState) -> Out {
|
||||||
|
let text = extract_text!(cx);
|
||||||
|
|
||||||
if let Ok(number) = text.parse() {
|
if let Ok(number) = text.parse() {
|
||||||
cx.answer_str(format!(
|
cx.answer_str(format!(
|
||||||
"Remembered number {}. Now use /get or /reset",
|
"Remembered number {}. Now use /get or /reset",
|
||||||
|
@ -18,11 +33,8 @@ async fn start(cx: TransitionIn, state: StartState, text: &str) -> Out {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn have_number(
|
pub async fn have_number(cx: TransitionIn, state: HaveNumberState) -> Out {
|
||||||
cx: TransitionIn,
|
let text = extract_text!(cx);
|
||||||
state: HaveNumberState,
|
|
||||||
text: &str,
|
|
||||||
) -> Out {
|
|
||||||
let num = state.number;
|
let num = state.number;
|
||||||
|
|
||||||
if text.starts_with("/get") {
|
if text.starts_with("/get") {
|
||||||
|
@ -36,10 +48,3 @@ async fn have_number(
|
||||||
next(state)
|
next(state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn dispatch(cx: TransitionIn, dialogue: Dialogue, text: &str) -> Out {
|
|
||||||
match dialogue {
|
|
||||||
Dialogue::Start(state) => start(cx, state, text).await,
|
|
||||||
Dialogue::HaveNumber(state) => have_number(cx, state, text).await,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue