-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: add thiserror, dotenv, update teloxide
refactor: change bot architecture build: add chrono, update rusqlite feat: add command /add: - insert, select catigories,hometasks,users in database - add pagination pages to command /add - add commands: add,cancel,help - add validation deadline
- Loading branch information
1 parent
21f875b
commit 301915c
Showing
9 changed files
with
547 additions
and
191 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::models::{State, Command}; | ||
use crate::telegram::basic_methods::*; | ||
use teloxide::dispatching::dialogue::InMemStorage; | ||
use teloxide::dispatching::{dialogue, DpHandlerDescription}; | ||
use teloxide::dptree; | ||
use teloxide::dptree::{case, Handler}; | ||
use teloxide::prelude::*; | ||
|
||
|
||
pub fn schema() -> Handler<'static, DependencyMap, crate::errors::Result<()>, DpHandlerDescription> | ||
{ | ||
let command_handler = teloxide::filter_command::<Command, _>() | ||
.branch( | ||
case![State::Start] | ||
.branch(case![Command::Help].endpoint(help)) | ||
.branch(case![Command::Add].endpoint(add)), | ||
) | ||
.branch(case![Command::Cancel].endpoint(cancel)); | ||
|
||
let callback_query_handler = Update::filter_callback_query().branch( | ||
case![State::ReceiveProductChoice].endpoint(receive_add_button), | ||
); | ||
|
||
// let callback_query_handler = Update::filter_message() | ||
// .branch(case![State::GetEmail { phone_number }].endpoint(get_email)) | ||
// .branch(case![State::GetAge { phone_number }].endpoint(get_age)) | ||
// .branch(case![State::GetWeightAndHeight { phone_number }].endpoint(get_height_and_weight)); | ||
|
||
let message_handler = Update::filter_message() | ||
.branch(command_handler) | ||
// .branch(case![State::GetPhoneNumber].endpoint(get_number)) | ||
// .branch(case![State::HomeTrainingMenu { phone_number }].endpoint(home_training_menu)) | ||
// .branch(case![State::GymTrainingMenu { phone_number }].endpoint(gym_training_menu)) | ||
.branch(case![State::CreateCategorie].endpoint(send_categorie)) | ||
.branch(case![State::AddTaskName { categorie }].endpoint(send_taskname)) | ||
.branch(case![State::AddDescription { categorie, taskname }].endpoint(send_description)) | ||
.branch(case![State::CreateTask { categorie, taskname, description }].endpoint(send_deadline)) | ||
.branch(dptree::endpoint(invalid_state)); | ||
|
||
|
||
dialogue::enter::<Update, InMemStorage<State>, State, _>().branch(message_handler).branch(callback_query_handler) | ||
//State::Start - initial state | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#[allow(clippy::enum_variant_names)] | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum Errors { | ||
#[error(transparent)] | ||
EnvError(#[from] dotenv::Error), | ||
|
||
#[error(transparent)] | ||
TeloxideError(#[from] teloxide::RequestError), | ||
|
||
#[error(transparent)] | ||
InMemStorageError(#[from] teloxide::dispatching::dialogue::InMemStorageError), | ||
|
||
|
||
#[error(transparent)] | ||
ParseIntError(#[from] std::num::ParseIntError), | ||
} | ||
|
||
pub type Result<T> = std::result::Result<T, Errors>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,35 @@ | ||
pub mod bot; | ||
pub mod telegram; | ||
pub mod db; | ||
|
||
use crate::errors::Result; | ||
use crate::commands::schema; | ||
use crate::models::State; | ||
use dotenv::dotenv; | ||
use std::sync::Arc; | ||
use db::create_db; | ||
use bot::*; | ||
use teloxide::{dispatching::dialogue::InMemStorage, prelude::*}; | ||
use teloxide::dispatching::dialogue::InMemStorage; | ||
use teloxide::prelude::*; | ||
|
||
mod models; | ||
mod commands; | ||
mod errors; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
async fn main() -> Result<()> { | ||
dotenv().ok(); | ||
log::info!("Starting dialogue bot..."); | ||
create_db(); | ||
pretty_env_logger::init(); | ||
|
||
let bot = Bot::from_env(); | ||
let bot = Bot::new(dotenv::var("TELOXIDE_TOKEN")?); | ||
let state = Arc::new(State::Start); | ||
|
||
Dispatcher::builder( | ||
bot, | ||
Update::filter_message() | ||
.enter_dialogue::<Message, InMemStorage<State>, State>() | ||
.branch(dptree::case![State::Start].endpoint(start)) | ||
.branch(dptree::case![State::ReceiveFullName].endpoint(receive_full_name)) | ||
.branch(dptree::case![State::ReceiveAge { full_name }].endpoint(receive_age)) | ||
.branch( | ||
dptree::case![State::ReceiveLocation { full_name, age }].endpoint(receive_location), | ||
), | ||
) | ||
.dependencies(dptree::deps![InMemStorage::<State>::new()]) | ||
Dispatcher::builder(bot, schema()) | ||
.dependencies(dptree::deps![ | ||
InMemStorage::<State>::new(), | ||
Arc::clone(&state) | ||
]) | ||
.enable_ctrlc_handler() | ||
.build() | ||
.dispatch() | ||
.await; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
use teloxide::dispatching::dialogue::InMemStorage; | ||
use teloxide::prelude::Dialogue; | ||
use teloxide::utils::command::BotCommands; | ||
|
||
pub type MyDialogue = Dialogue<State, InMemStorage<State>>; | ||
|
||
/// These commands are supported: | ||
#[derive(BotCommands, Clone)] | ||
#[command( | ||
rename_rule = "lowercase", | ||
description = "These commands are supported:" | ||
)] | ||
pub enum Command { | ||
#[command(description = "display this text.")] | ||
Help, | ||
#[command(description = "cancel the purchase procedure.")] | ||
Cancel, | ||
#[command(description = "create new hometask")] | ||
Add, | ||
} | ||
|
||
#[derive(Clone, Default)] | ||
pub enum State { | ||
#[default] | ||
Start, | ||
ReceiveProductChoice, | ||
CreateCategorie, | ||
AddTaskName { categorie: String }, | ||
AddDescription { categorie: String, taskname: String }, | ||
CreateTask { categorie: String, taskname: String, description: String }, | ||
} |
Oops, something went wrong.