Compare commits
No commits in common. "8a3e8c470543ea43622dcaa1a8c23c908f23d20e" and "9dfa7c52d9bb538db7ad270a83053ef5d2198ab9" have entirely different histories.
8a3e8c4705
...
9dfa7c52d9
@ -17,7 +17,7 @@ use crate::{
|
|||||||
commands::BotCommand,
|
commands::BotCommand,
|
||||||
db::{CallDB, DB},
|
db::{CallDB, DB},
|
||||||
message_answerer::MessageAnswerer,
|
message_answerer::MessageAnswerer,
|
||||||
notify_admin, update_user_tg, BotError, BotResult, BotRuntime,
|
update_user_tg, BotError, BotResult, BotRuntime,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type BotHandler =
|
pub type BotHandler =
|
||||||
@ -69,34 +69,6 @@ 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() {
|
||||||
@ -157,8 +129,7 @@ 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, variant.as_ref().map(|v| v.as_str()), buttons)
|
ma.answer(literal, None, buttons).await?;
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -499,60 +499,17 @@ 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
|
..self.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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
|
||||||
}
|
}
|
||||||
@ -560,14 +517,6 @@ 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 {
|
||||||
@ -912,7 +861,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()).update_defaults())
|
bm.map(|bm| bm.fill_literal(command.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
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