teloxide/examples/redis_remember.rs

117 lines
3 KiB
Rust
Raw Normal View History

2020-04-19 19:47:12 +02:00
use teloxide::{
2022-01-06 12:54:07 +01:00
dispatching2::dialogue::{serializer::Bincode, RedisStorage, Storage},
macros::DialogueState,
prelude2::*,
types::Me,
utils::command::BotCommand,
2021-03-15 04:22:10 +01:00
RequestError,
2020-04-19 19:47:12 +02:00
};
use thiserror::Error;
type MyDialogue = Dialogue<State, RedisStorage<Bincode>>;
type StorageError = <RedisStorage<Bincode> as Storage<State>>::Error;
2020-04-19 19:47:12 +02:00
#[derive(Debug, Error)]
enum Error {
#[error("error from Telegram: {0}")]
TelegramError(#[from] RequestError),
2020-04-19 19:47:12 +02:00
#[error("error from storage: {0}")]
StorageError(#[from] StorageError),
}
#[derive(DialogueState, Clone, serde::Serialize, serde::Deserialize)]
#[handler_out(anyhow::Result<()>)]
pub enum State {
#[handler(handle_start)]
2022-01-06 12:54:07 +01:00
Start,
#[handler(handle_got_number)]
GotNumber(i32),
2022-01-06 12:54:07 +01:00
}
2020-04-19 19:47:12 +02:00
impl Default for State {
2022-01-06 12:54:07 +01:00
fn default() -> Self {
Self::Start
}
2020-04-19 19:47:12 +02:00
}
#[derive(BotCommand)]
#[command(rename = "lowercase", description = "These commands are supported:")]
pub enum Command {
#[command(description = "get your number.")]
Get,
#[command(description = "reset your number.")]
Reset,
}
2022-01-06 12:54:07 +01:00
#[tokio::main]
async fn main() {
let bot = Bot::from_env().auto_send();
// You can also choose serializer::JSON or serializer::CBOR
// All serializers but JSON require enabling feature
// "serializer-<name>", e. g. "serializer-cbor"
// or "serializer-bincode"
let storage = RedisStorage::open("redis://127.0.0.1:6379", Bincode).await.unwrap();
2022-02-02 21:36:36 +01:00
let handler = Update::filter_message()
.enter_dialogue::<Message, RedisStorage<Bincode>, State>()
.dispatch_by::<State>();
2022-01-26 10:51:51 +01:00
Dispatcher::builder(bot, handler)
.dependencies(dptree::deps![storage])
.build()
.setup_ctrlc_handler()
.dispatch()
.await;
}
async fn handle_start(
bot: AutoSend<Bot>,
msg: Message,
dialogue: MyDialogue,
) -> anyhow::Result<()> {
match msg.text().unwrap().parse() {
Ok(number) => {
dialogue.update(State::GotNumber(number)).await?;
bot.send_message(
msg.chat.id,
format!("Remembered number {}. Now use /get or /reset", number),
)
.await?;
}
_ => {
bot.send_message(msg.chat.id, "Please, send me a number").await?;
}
}
Ok(())
}
async fn handle_got_number(
bot: AutoSend<Bot>,
msg: Message,
dialogue: MyDialogue,
num: i32,
me: Me,
) -> anyhow::Result<()> {
let ans = msg.text().unwrap();
let bot_name = me.user.username.unwrap();
match Command::parse(ans, bot_name) {
Ok(cmd) => match cmd {
Command::Get => {
bot.send_message(msg.chat.id, format!("Here is your number: {}", num)).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(())
}