Skip to content

Commit

Permalink
chore: configuring changes
Browse files Browse the repository at this point in the history
  • Loading branch information
shawakash committed May 7, 2024
1 parent 756ff2c commit b8fcf07
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
17 changes: 7 additions & 10 deletions src/commands/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use teloxide::{prelude::*, utils::command::BotCommands};
pub enum Command {
#[command(description = "Get to know what we provide you with😊")]
Help,
#[command(description = "handle a username.")]
Username(String),
#[command(description = "handle a username and an age.", parse_with = "split")]
UsernameAndAge { username: String, age: u8 },
#[command(description = "Start with configuring your account.")]
Start,
#[command(description = "Get token info")]
Token,
}

pub async fn answer(bot: Bot, msg: Message, cmd: Command) -> ResponseResult<()> {
Expand All @@ -20,17 +20,14 @@ pub async fn answer(bot: Bot, msg: Message, cmd: Command) -> ResponseResult<()>
bot.send_message(msg.chat.id, Command::descriptions().to_string())
.await?
}
Command::Username(username) => {
bot.send_message(msg.chat.id, format!("Your username is @{username}."))
.await?
}
Command::UsernameAndAge { username, age } => {
Command::Start => {
bot.send_message(
msg.chat.id,
format!("Your username is @{username} and age is {age}."),
"Let's take your credentials and market interest info...",
)
.await?
}
Command::Token => bot.send_message(msg.chat.id, "Token info").await?,
};

Ok(())
Expand Down
17 changes: 17 additions & 0 deletions src/dialogs/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
pub enum State {
#[default]
Start,
Help,
// ReceiveName,
// InterestedTokens {
// name: String,
Expand All @@ -23,5 +24,21 @@ pub async fn start_handler(bot: Bot, dialog: MyDialogue, msg: Message) -> Handle
"Let's take your credentials and market interest info...",
)
.await;
// dialog.update(State::Start).await?;
Ok(())
}

pub async fn dialog_handler(bot: Bot) -> HandlerResult {
Dispatcher::builder(
bot,
Update::filter_message()
.enter_dialogue::<Message, InMemStorage<State>, State>()
.branch(dptree::case![State::Start].endpoint(start_handler)),
)
.dependencies(dptree::deps![InMemStorage::<State>::new()])
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
Ok(())
}
31 changes: 12 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use teloxide::dispatching::UpdateFilterExt;
use teloxide::{dispatching::dialogue::InMemStorage, prelude::*};

mod commands;
mod dialogs;
mod keyboard;
use crate::commands::command::{answer, Command};
use crate::dialogs::dialog::{start_handler, State};
use crate::keyboard::keyboard::{callback_handler, inline_query_handler, message_handler};

#[tokio::main]
async fn main() {
Expand All @@ -13,24 +16,14 @@ async fn main() {

let bot = Bot::from_env();

// teloxide::repl(bot, |bot: Bot, msg: Message| async move {
// println!("{:?}", msg);
// bot.send_message(msg.chat.id, "Hello!").send().await?;
// Ok(())
// })
// .await;
let handler = dptree::entry()
.branch(Update::filter_message().endpoint(message_handler))
.branch(Update::filter_callback_query().endpoint(callback_handler))
.branch(Update::filter_inline_query().endpoint(inline_query_handler));

// Command::repl(bot.clone(), answer).await;

Dispatcher::builder(
bot,
Update::filter_message()
.enter_dialogue::<Message, InMemStorage<State>, State>()
.branch(dptree::case![State::Start].endpoint(start_handler)),
)
.dependencies(dptree::deps![InMemStorage::<State>::new()])
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
Dispatcher::builder(bot, handler)
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
}

0 comments on commit b8fcf07

Please sign in to comment.