mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-03 17:52:12 +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
|
||||
teloxide = { path = "../../", features = ["redis-storage", "bincode-serializer"] }
|
||||
teloxide-macros = { git = "http://github.com/teloxide/teloxide-macros", branch = "master" }
|
||||
|
||||
serde = "1.0.104"
|
||||
futures = "0.3.5"
|
||||
|
||||
thiserror = "1.0.15"
|
||||
smart-default = "0.6.0"
|
||||
|
|
|
@ -7,7 +7,6 @@ mod states;
|
|||
mod transitions;
|
||||
|
||||
use states::*;
|
||||
use transitions::*;
|
||||
|
||||
use teloxide::{
|
||||
dispatching::dialogue::{serializer::Bincode, RedisStorage, Storage},
|
||||
|
@ -27,18 +26,6 @@ enum Error {
|
|||
|
||||
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]
|
||||
async fn main() {
|
||||
run().await;
|
||||
|
@ -48,8 +35,11 @@ async fn run() {
|
|||
let bot = Bot::from_env();
|
||||
Dispatcher::new(bot)
|
||||
.messages_handler(DialogueDispatcher::with_storage(
|
||||
|cx| async move {
|
||||
handle_message(cx)
|
||||
|input: In| async move {
|
||||
let (cx, dialogue) = input.unpack();
|
||||
|
||||
dialogue
|
||||
.dispatch(cx)
|
||||
.await
|
||||
.expect("Something is wrong with the bot!")
|
||||
},
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
use teloxide::prelude::*;
|
||||
use teloxide_macros::BotDialogue;
|
||||
|
||||
use super::transitions::{have_number, start};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
@ -15,9 +18,12 @@ up!(
|
|||
StartState + [number: i32] -> HaveNumberState,
|
||||
);
|
||||
|
||||
#[derive(SmartDefault, From, Serialize, Deserialize)]
|
||||
#[derive(BotDialogue, SmartDefault, From, Serialize, Deserialize)]
|
||||
pub enum Dialogue {
|
||||
#[default]
|
||||
#[transition(start)]
|
||||
Start(StartState),
|
||||
|
||||
#[transition(have_number)]
|
||||
HaveNumber(HaveNumberState),
|
||||
}
|
||||
|
|
|
@ -2,9 +2,24 @@ use teloxide::prelude::*;
|
|||
|
||||
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>;
|
||||
|
||||
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() {
|
||||
cx.answer_str(format!(
|
||||
"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(
|
||||
cx: TransitionIn,
|
||||
state: HaveNumberState,
|
||||
text: &str,
|
||||
) -> Out {
|
||||
pub async fn have_number(cx: TransitionIn, state: HaveNumberState) -> Out {
|
||||
let text = extract_text!(cx);
|
||||
let num = state.number;
|
||||
|
||||
if text.starts_with("/get") {
|
||||
|
@ -36,10 +48,3 @@ async fn have_number(
|
|||
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