Compare commits

..

8 Commits

Author SHA1 Message Date
Akulij
8a3e8c4705 filter variant to use only defined in runtime config
All checks were successful
Build && Deploy / cargo build (push) Successful in 1m5s
2025-06-05 22:59:28 +05:00
Akulij
50e2d6e824 use variants of messages 2025-06-05 22:59:12 +05:00
Akulij
c301b72f0c create MessageVariant 2025-06-05 22:56:52 +05:00
Akulij
4103c5dfbe create BotMessage.update_defaults method for some logic for default values 2025-06-05 22:36:30 +05:00
Akulij
303dbfdaa8 make BotMessage.meta optional 2025-06-05 22:35:51 +05:00
Akulij
b202f385fe fix: make BotMessage.fill_literal consuming self 2025-06-05 22:35:16 +05:00
Akulij
3b4ab9b481 implement usage of BotMessage.meta flag in handle_botmessage 2025-06-05 22:21:44 +05:00
Akulij
f58f559f8d create BotMessage.meta flag 2025-06-05 22:21:14 +05:00
2 changed files with 85 additions and 5 deletions

View File

@ -17,7 +17,7 @@ use crate::{
commands::BotCommand,
db::{CallDB, DB},
message_answerer::MessageAnswerer,
update_user_tg, BotError, BotResult, BotRuntime,
notify_admin, update_user_tg, BotError, BotResult, BotRuntime,
};
pub type BotHandler =
@ -69,6 +69,34 @@ async fn handle_botmessage(bot: Bot, mut db: DB, bm: BotMessage, msg: Message) -
let user = update_user_tg(user, &tguser);
user.update_user(&mut db).await?;
let variant = if bm.meta() == true {
let meta = match BotCommand::from_str(msg.text().unwrap_or("")) {
Ok(cmd) => cmd.args().map(|m| m.to_string()),
Err(err) => {
notify_admin(&format!("Error while parsing cmd in `meta`, possibly meta is set not in command, err: {err}")).await;
None
}
};
if let Some(ref meta) = meta {
user.insert_meta(&mut db, meta).await?;
};
meta
} else {
None
};
// Filtering to use only defined variants
let variant = match bm
.variants()
.iter()
.any(|v| v == variant.as_ref().map_or("", |v| v))
{
true => variant,
false => None,
};
let is_propagate: bool = match bm.get_handler() {
Some(handler) => 'prop: {
let ctx = match handler.context() {
@ -129,7 +157,8 @@ async fn handle_botmessage(bot: Bot, mut db: DB, bm: BotMessage, msg: Message) -
let literal = bm.literal().map_or("", |s| s.as_str());
let ma = MessageAnswerer::new(&bot, &mut db, msg.chat.id.0);
ma.answer(literal, None, buttons).await?;
ma.answer(literal, variant.as_ref().map(|v| v.as_str()), buttons)
.await?;
Ok(())
}

View File

@ -499,17 +499,60 @@ pub struct BotMessage {
buttons: Option<KeyboardDefinition>,
state: Option<String>,
/// flag options to command is meta, so it will be appended to user.metas in db
meta: Option<bool>,
variants: Vec<MessageVariant>,
handler: Option<BotFunction>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MessageVariant(String);
impl MessageVariant {
pub fn get_name(&self) -> &str {
&self.0
}
}
impl PartialEq<String> for &MessageVariant {
fn eq(&self, other: &String) -> bool {
self.0 == *other
}
}
impl PartialEq<&str> for &MessageVariant {
fn eq(&self, other: &&str) -> bool {
self.0 == *other
}
}
impl BotMessage {
pub fn fill_literal(&self, l: String) -> Self {
pub fn fill_literal(self, l: String) -> Self {
BotMessage {
literal: self.clone().literal.or(Some(l)),
..self.clone()
..self
}
}
/// chain of modifications on BotMessage
pub fn update_defaults(self) -> Self {
let bm = self;
// if message is `start`, defaulting meta to true, if not set
let bm = match bm.meta {
Some(_) => bm,
None => match &bm.literal {
Some(l) if l == "start" => Self {
meta: Some(true),
..bm
},
_ => bm,
},
};
bm
}
pub fn is_replace(&self) -> bool {
self.replace
}
@ -517,6 +560,14 @@ impl BotMessage {
pub fn get_handler(&self) -> Option<&BotFunction> {
self.handler.as_ref()
}
pub fn meta(&self) -> bool {
self.meta.unwrap_or(false)
}
pub fn variants(&self) -> &[MessageVariant] {
&self.variants
}
}
impl BotMessage {
@ -861,7 +912,7 @@ impl RunnerConfig {
pub fn get_command_message(&self, command: &str) -> Option<BotMessage> {
let bm = self.dialog.commands.get(command).cloned();
bm.map(|bm| bm.fill_literal(command.to_string()))
bm.map(|bm| bm.fill_literal(command.to_string()).update_defaults())
}
pub fn get_callback_message(&self, callback: &str) -> Option<BotMessage> {