Compare commits
No commits in common. "c371f81008bfe3f88e18c86d50834aa62cb4b051" and "6099a2ba81ad97d4e5083891009e4c6e30ef728f" have entirely different histories.
c371f81008
...
6099a2ba81
18
src/admin.rs
18
src/admin.rs
@ -31,9 +31,6 @@ pub enum AdminCommands {
|
|||||||
EditButton,
|
EditButton,
|
||||||
/// Set specified literal value
|
/// Set specified literal value
|
||||||
SetLiteral { literal: String },
|
SetLiteral { literal: String },
|
||||||
/// Set specified literal value
|
|
||||||
#[command(description = "handle a username and an age.", parse_with = "split")]
|
|
||||||
SetAlternative { literal: String, variant: String },
|
|
||||||
/// Sets chat where this message entered as support's chats
|
/// Sets chat where this message entered as support's chats
|
||||||
SetChat,
|
SetChat,
|
||||||
}
|
}
|
||||||
@ -89,7 +86,6 @@ pub async fn admin_command_handler(
|
|||||||
dialogue
|
dialogue
|
||||||
.update(State::Edit {
|
.update(State::Edit {
|
||||||
literal,
|
literal,
|
||||||
variant: None,
|
|
||||||
lang: "ru".to_string(),
|
lang: "ru".to_string(),
|
||||||
is_caption_set: false,
|
is_caption_set: false,
|
||||||
})
|
})
|
||||||
@ -99,20 +95,6 @@ pub async fn admin_command_handler(
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
AdminCommands::SetAlternative { literal, variant } => {
|
|
||||||
dialogue
|
|
||||||
.update(State::Edit {
|
|
||||||
literal,
|
|
||||||
variant: Some(variant),
|
|
||||||
lang: "ru".to_string(),
|
|
||||||
is_caption_set: false,
|
|
||||||
})
|
|
||||||
.await?;
|
|
||||||
bot.send_message(msg.chat.id, "Send message for literal alternative")
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
AdminCommands::SetChat => {
|
AdminCommands::SetChat => {
|
||||||
dialogue.exit().await?;
|
dialogue.exit().await?;
|
||||||
db.set_literal("support_chat_id", &msg.chat.id.0.to_string())
|
db.set_literal("support_chat_id", &msg.chat.id.0.to_string())
|
||||||
|
|||||||
@ -112,14 +112,6 @@ pub struct Literal {
|
|||||||
pub value: String,
|
pub value: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
|
||||||
pub struct LiteralAlternative {
|
|
||||||
pub _id: bson::oid::ObjectId,
|
|
||||||
pub token: String,
|
|
||||||
pub variant: String,
|
|
||||||
pub value: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Event {
|
pub struct Event {
|
||||||
pub _id: bson::oid::ObjectId,
|
pub _id: bson::oid::ObjectId,
|
||||||
@ -329,51 +321,6 @@ pub trait CallDB {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_literal_alternative(
|
|
||||||
&mut self,
|
|
||||||
literal: &str,
|
|
||||||
variant: &str,
|
|
||||||
) -> DbResult<Option<LiteralAlternative>> {
|
|
||||||
let db = self.get_database().await;
|
|
||||||
let messages = db.collection::<LiteralAlternative>("literal_alternatives");
|
|
||||||
|
|
||||||
let literal = messages
|
|
||||||
.find_one(doc! { "token": literal, "variant": variant })
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(literal)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_literal_alternative_value(
|
|
||||||
&mut self,
|
|
||||||
literal: &str,
|
|
||||||
variant: &str,
|
|
||||||
) -> DbResult<Option<String>> {
|
|
||||||
let literal = self.get_literal_alternative(literal, variant).await?;
|
|
||||||
|
|
||||||
Ok(literal.map(|l| l.value))
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn set_literal_alternative(
|
|
||||||
&mut self,
|
|
||||||
literal: &str,
|
|
||||||
variant: &str,
|
|
||||||
valuestr: &str,
|
|
||||||
) -> DbResult<()> {
|
|
||||||
let db = self.get_database().await;
|
|
||||||
let literals = db.collection::<LiteralAlternative>("literal_alternatives");
|
|
||||||
|
|
||||||
literals
|
|
||||||
.update_one(
|
|
||||||
doc! { "token": literal, "variant": variant },
|
|
||||||
doc! { "$set": { "value": valuestr } },
|
|
||||||
)
|
|
||||||
.upsert(true)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_all_events(&mut self) -> DbResult<Vec<Event>> {
|
async fn get_all_events(&mut self) -> DbResult<Vec<Event>> {
|
||||||
let db = self.get_database().await;
|
let db = self.get_database().await;
|
||||||
let events = db.collection::<Event>("events");
|
let events = db.collection::<Event>("events");
|
||||||
|
|||||||
70
src/main.rs
70
src/main.rs
@ -70,7 +70,6 @@ pub enum State {
|
|||||||
Start,
|
Start,
|
||||||
Edit {
|
Edit {
|
||||||
literal: String,
|
literal: String,
|
||||||
variant: Option<String>,
|
|
||||||
lang: String,
|
lang: String,
|
||||||
is_caption_set: bool,
|
is_caption_set: bool,
|
||||||
},
|
},
|
||||||
@ -188,7 +187,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
.branch(
|
.branch(
|
||||||
dptree::case![State::Edit {
|
dptree::case![State::Edit {
|
||||||
literal,
|
literal,
|
||||||
variant,
|
|
||||||
lang,
|
lang,
|
||||||
is_caption_set
|
is_caption_set
|
||||||
}]
|
}]
|
||||||
@ -254,7 +252,6 @@ async fn button_edit_callback(
|
|||||||
dialogue
|
dialogue
|
||||||
.update(State::Edit {
|
.update(State::Edit {
|
||||||
literal,
|
literal,
|
||||||
variant: None,
|
|
||||||
lang,
|
lang,
|
||||||
is_caption_set: false,
|
is_caption_set: false,
|
||||||
})
|
})
|
||||||
@ -497,7 +494,6 @@ async fn edit_msg_cmd_handler(
|
|||||||
dialogue
|
dialogue
|
||||||
.update(State::Edit {
|
.update(State::Edit {
|
||||||
literal,
|
literal,
|
||||||
variant: None,
|
|
||||||
lang,
|
lang,
|
||||||
is_caption_set: false,
|
is_caption_set: false,
|
||||||
})
|
})
|
||||||
@ -523,7 +519,7 @@ async fn edit_msg_handler(
|
|||||||
bot: Bot,
|
bot: Bot,
|
||||||
mut db: DB,
|
mut db: DB,
|
||||||
dialogue: BotDialogue,
|
dialogue: BotDialogue,
|
||||||
(literal, variant, lang, is_caption_set): (String, Option<String>, String, bool),
|
(literal, lang, is_caption_set): (String, String, bool),
|
||||||
msg: Message,
|
msg: Message,
|
||||||
) -> BotResult<()> {
|
) -> BotResult<()> {
|
||||||
use teloxide::utils::render::Renderer;
|
use teloxide::utils::render::Renderer;
|
||||||
@ -537,28 +533,6 @@ async fn edit_msg_handler(
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(variant) = variant {
|
|
||||||
if let MediaKind::Text(text) = msg.media_kind {
|
|
||||||
let html_text = Renderer::new(&text.text, &text.entities).as_html();
|
|
||||||
|
|
||||||
db.set_literal_alternative(&literal, &variant, &html_text)
|
|
||||||
.await?;
|
|
||||||
bot.send_message(chat_id, "Updated text of variant!")
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
dialogue.exit().await?;
|
|
||||||
return Ok(());
|
|
||||||
} else {
|
|
||||||
bot.send_message(
|
|
||||||
chat_id,
|
|
||||||
"On variants only text alternating supported. Try to send text only",
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
match msg.media_kind {
|
match msg.media_kind {
|
||||||
MediaKind::Text(text) => {
|
MediaKind::Text(text) => {
|
||||||
db.drop_media(&literal).await?;
|
db.drop_media(&literal).await?;
|
||||||
@ -610,7 +584,6 @@ async fn edit_msg_handler(
|
|||||||
dialogue
|
dialogue
|
||||||
.update(State::Edit {
|
.update(State::Edit {
|
||||||
literal,
|
literal,
|
||||||
variant: None,
|
|
||||||
lang,
|
lang,
|
||||||
is_caption_set: true,
|
is_caption_set: true,
|
||||||
})
|
})
|
||||||
@ -659,7 +632,6 @@ async fn edit_msg_handler(
|
|||||||
dialogue
|
dialogue
|
||||||
.update(State::Edit {
|
.update(State::Edit {
|
||||||
literal,
|
literal,
|
||||||
variant: None,
|
|
||||||
lang,
|
lang,
|
||||||
is_caption_set: true,
|
is_caption_set: true,
|
||||||
})
|
})
|
||||||
@ -735,17 +707,12 @@ async fn user_command_handler(
|
|||||||
if !meta.is_empty() {
|
if !meta.is_empty() {
|
||||||
user.insert_meta(&mut db, &meta).await?;
|
user.insert_meta(&mut db, &meta).await?;
|
||||||
}
|
}
|
||||||
let variant = match meta.as_str() {
|
|
||||||
"" => None,
|
|
||||||
variant => Some(variant),
|
|
||||||
};
|
|
||||||
let mut db2 = db.clone();
|
let mut db2 = db.clone();
|
||||||
answer_message_varianted(
|
answer_message(
|
||||||
&bot,
|
&bot,
|
||||||
msg.chat.id.0,
|
msg.chat.id.0,
|
||||||
&mut db,
|
&mut db,
|
||||||
"start",
|
"start",
|
||||||
variant,
|
|
||||||
Some(make_start_buttons(&mut db2).await?),
|
Some(make_start_buttons(&mut db2).await?),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
@ -766,35 +733,10 @@ async fn answer_message<RM: Into<ReplyMarkup>>(
|
|||||||
literal: &str,
|
literal: &str,
|
||||||
keyboard: Option<RM>,
|
keyboard: Option<RM>,
|
||||||
) -> BotResult<()> {
|
) -> BotResult<()> {
|
||||||
answer_message_varianted(bot, chat_id, db, literal, None, keyboard).await
|
let text = db
|
||||||
}
|
.get_literal_value(literal)
|
||||||
|
.await?
|
||||||
async fn answer_message_varianted<RM: Into<ReplyMarkup>>(
|
.unwrap_or("Please, set content of this message".into());
|
||||||
bot: &Bot,
|
|
||||||
chat_id: i64,
|
|
||||||
db: &mut DB,
|
|
||||||
literal: &str,
|
|
||||||
variant: Option<&str>,
|
|
||||||
keyboard: Option<RM>,
|
|
||||||
) -> BotResult<()> {
|
|
||||||
let variant = match variant {
|
|
||||||
Some(variant) => {
|
|
||||||
let value = db.get_literal_alternative_value(literal, variant).await?;
|
|
||||||
if value.is_none() {
|
|
||||||
notify_admin(&format!("variant {variant} for literal {literal} is not found! falling back to just literal")).await;
|
|
||||||
}
|
|
||||||
value
|
|
||||||
}
|
|
||||||
None => None,
|
|
||||||
};
|
|
||||||
let text = match variant {
|
|
||||||
Some(text) => text,
|
|
||||||
None => db
|
|
||||||
.get_literal_value(literal)
|
|
||||||
.await?
|
|
||||||
.unwrap_or("Please, set content of this message".into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
let media = db.get_media(literal).await?;
|
let media = db.get_media(literal).await?;
|
||||||
let (chat_id, msg_id) = match media.len() {
|
let (chat_id, msg_id) = match media.len() {
|
||||||
// just a text
|
// just a text
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user