Compare commits
8 Commits
9dfa7c52d9
...
8a3e8c4705
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8a3e8c4705 | ||
|
|
50e2d6e824 | ||
|
|
c301b72f0c | ||
|
|
4103c5dfbe | ||
|
|
303dbfdaa8 | ||
|
|
b202f385fe | ||
|
|
3b4ab9b481 | ||
|
|
f58f559f8d |
@ -17,7 +17,7 @@ use crate::{
|
|||||||
commands::BotCommand,
|
commands::BotCommand,
|
||||||
db::{CallDB, DB},
|
db::{CallDB, DB},
|
||||||
message_answerer::MessageAnswerer,
|
message_answerer::MessageAnswerer,
|
||||||
update_user_tg, BotError, BotResult, BotRuntime,
|
notify_admin, update_user_tg, BotError, BotResult, BotRuntime,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type BotHandler =
|
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);
|
let user = update_user_tg(user, &tguser);
|
||||||
user.update_user(&mut db).await?;
|
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() {
|
let is_propagate: bool = match bm.get_handler() {
|
||||||
Some(handler) => 'prop: {
|
Some(handler) => 'prop: {
|
||||||
let ctx = match handler.context() {
|
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 literal = bm.literal().map_or("", |s| s.as_str());
|
||||||
|
|
||||||
let ma = MessageAnswerer::new(&bot, &mut db, msg.chat.id.0);
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -499,17 +499,60 @@ pub struct BotMessage {
|
|||||||
buttons: Option<KeyboardDefinition>,
|
buttons: Option<KeyboardDefinition>,
|
||||||
state: Option<String>,
|
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>,
|
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 {
|
impl BotMessage {
|
||||||
pub fn fill_literal(&self, l: String) -> Self {
|
pub fn fill_literal(self, l: String) -> Self {
|
||||||
BotMessage {
|
BotMessage {
|
||||||
literal: self.clone().literal.or(Some(l)),
|
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 {
|
pub fn is_replace(&self) -> bool {
|
||||||
self.replace
|
self.replace
|
||||||
}
|
}
|
||||||
@ -517,6 +560,14 @@ impl BotMessage {
|
|||||||
pub fn get_handler(&self) -> Option<&BotFunction> {
|
pub fn get_handler(&self) -> Option<&BotFunction> {
|
||||||
self.handler.as_ref()
|
self.handler.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn meta(&self) -> bool {
|
||||||
|
self.meta.unwrap_or(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn variants(&self) -> &[MessageVariant] {
|
||||||
|
&self.variants
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BotMessage {
|
impl BotMessage {
|
||||||
@ -861,7 +912,7 @@ impl RunnerConfig {
|
|||||||
pub fn get_command_message(&self, command: &str) -> Option<BotMessage> {
|
pub fn get_command_message(&self, command: &str) -> Option<BotMessage> {
|
||||||
let bm = self.dialog.commands.get(command).cloned();
|
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> {
|
pub fn get_callback_message(&self, callback: &str) -> Option<BotMessage> {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user