Updated generic handler to handle the lack of the text of the message

This commit is contained in:
StNicolay 2023-05-15 18:14:21 +03:00
parent 2f96d84e34
commit a2477424a5
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
6 changed files with 18 additions and 25 deletions

View File

@ -2,10 +2,6 @@
#[error("No user info found")]
pub struct NoUserInfo;
#[derive(thiserror::Error, Debug)]
#[error("Message text not found")]
pub struct NoMessageText;
#[derive(thiserror::Error, Debug)]
#[error("Handler was already used")]
pub struct HandlerUsed;

View File

@ -9,7 +9,6 @@ use teloxide::{adaptors::Throttle, prelude::*};
/// A generic state handler. It checks for "/cancel" messages and runs the provided validation function
pub async fn generic<F>(
bot: Throttle<Bot>,
text: String,
msg: Message,
db: DatabaseConnection,
dialogue: crate::handlers::MainDialogue,
@ -27,7 +26,15 @@ where
let handler = next.lock().await.take();
let previous = handler.as_ref().and_then(|h| h.previous.as_ref());
delete_optional(&bot, previous).await;
if text.trim_end() == "/cancel" {
let text = match msg.text() {
Some(text) => text.trim_end(),
None => {
bot.send_message(msg.chat.id, "Couldn't get the text of the message")
.await?;
return Ok(());
}
};
if text == "/cancel" {
dialogue.exit().await?;
bot.send_message(msg.chat.id, "Successfully cancelled")
.reply_markup(deletion_markup())
@ -41,7 +48,7 @@ where
return Err(HandlerUsed.into());
}
};
match check(&bot, &msg, &db, &text).await {
match check(&bot, &msg, &db, text).await {
Ok(true) => (),
Ok(false) => {
dialogue.exit().await?;
@ -52,6 +59,7 @@ where
return Err(err);
}
};
let text = text.to_owned();
if let Err(err) = (handler.handler)(bot, msg, db, dialogue.clone(), text).await {
let _ = dialogue.exit().await;
return Err(err);

View File

@ -1,9 +1,8 @@
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
use crate::{
errors::NoMessageText,
handlers::{markups::deletion_markup, utils::validate_field, MainDialogue, PackagedHandler},
use crate::handlers::{
markups::deletion_markup, utils::validate_field, MainDialogue, PackagedHandler,
};
/// Function to handle GetAccountName state
@ -14,10 +13,8 @@ pub async fn get_account_name(
dialogue: MainDialogue,
next: PackagedHandler<String>,
) -> crate::Result<()> {
let text = msg.text().ok_or(NoMessageText)?.trim().to_owned();
super::generic::generic(
bot,
text,
msg,
db,
dialogue,

View File

@ -1,9 +1,8 @@
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
use crate::{
errors::NoMessageText,
handlers::{markups::deletion_markup, utils::validate_field, MainDialogue, PackagedHandler},
use crate::handlers::{
markups::deletion_markup, utils::validate_field, MainDialogue, PackagedHandler,
};
/// Function to handle GetLogin state
@ -14,10 +13,8 @@ pub async fn get_login(
dialogue: MainDialogue,
next: PackagedHandler<String>,
) -> crate::Result<()> {
let text = msg.text().ok_or(NoMessageText)?.trim().to_owned();
super::generic::generic(
bot,
text,
msg,
db,
dialogue,

View File

@ -1,6 +1,6 @@
use crate::{
entity::prelude::MasterPass,
errors::{NoMessageText, NoUserInfo},
errors::{NoUserInfo},
handlers::{markups::deletion_markup, MainDialogue, PackagedHandler},
};
use sea_orm::prelude::*;
@ -41,10 +41,8 @@ pub async fn get_master_pass(
dialogue: MainDialogue,
next: PackagedHandler<String>,
) -> crate::Result<()> {
let text = msg.text().ok_or(NoMessageText)?.trim().to_owned();
super::generic::generic(
bot,
text,
msg,
db,
dialogue,

View File

@ -1,9 +1,8 @@
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
use crate::{
errors::NoMessageText,
handlers::{markups::deletion_markup, utils::validate_field, MainDialogue, PackagedHandler},
use crate::handlers::{
markups::deletion_markup, utils::validate_field, MainDialogue, PackagedHandler,
};
/// Function to handle GetPassword state
@ -14,10 +13,8 @@ pub async fn get_password(
dialogue: MainDialogue,
next: PackagedHandler<String>,
) -> crate::Result<()> {
let text = msg.text().ok_or(NoMessageText)?.trim().to_owned();
super::generic::generic(
bot,
text,
msg,
db,
dialogue,