mirror of
https://github.com/Steffo99/patched-porobot.git
synced 2024-12-22 17:44:22 +00:00
Allow deck names to be specified in the Telegram bot
This commit is contained in:
parent
0119dbc515
commit
961ab2cea8
3 changed files with 37 additions and 18 deletions
|
@ -153,10 +153,10 @@ fn display_levelup(levelup: &String) -> String {
|
|||
}
|
||||
}
|
||||
|
||||
/// Render a [Deck] in [Telegram Bot HTML].
|
||||
/// Render a [Deck] in [Telegram Bot HTML], with an optional `name`.
|
||||
///
|
||||
/// [Telegram Bot HTML]: https://core.telegram.org/bots/api#html-style
|
||||
pub fn display_deck(index: &CardIndex, deck: &Deck, code: String) -> String {
|
||||
pub fn display_deck(index: &CardIndex, deck: &Deck, code: &str, name: &Option<&str>) -> String {
|
||||
// TODO: optimize this
|
||||
let cards = deck
|
||||
.contents
|
||||
|
@ -182,5 +182,8 @@ pub fn display_deck(index: &CardIndex, deck: &Deck, code: String) -> String {
|
|||
})
|
||||
.join("\n");
|
||||
|
||||
format!("<code>{}</code>\n\n{}", &code, &cards)
|
||||
match name {
|
||||
Some(name) => format!("<b>{}</b>\n<code>{}</code>\n\n{}", &name, &code, &cards),
|
||||
None => format!("<code>{}</code>\n\n{}", &code, &cards),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,6 +10,8 @@ use teloxide::payloads::{AnswerInlineQuery, SendMessage};
|
|||
use teloxide::prelude::*;
|
||||
use teloxide::requests::{JsonRequest, ResponseResult};
|
||||
use teloxide::types::{ParseMode, Recipient};
|
||||
use lazy_static::lazy_static;
|
||||
use regex::Regex;
|
||||
|
||||
/// Handle inline queries by searching cards on the [CardSearchEngine].
|
||||
pub fn inline_query_handler(
|
||||
|
@ -33,17 +35,28 @@ pub fn inline_query_handler(
|
|||
};
|
||||
}
|
||||
|
||||
if let Ok(deck) = Deck::from_code(&query.query.to_ascii_uppercase()) {
|
||||
debug!("Parsed deck successfully!");
|
||||
break AnswerInlineQuery {
|
||||
inline_query_id: query.id.clone(),
|
||||
results: vec![deck_to_inlinequeryresult(&engine.cards, &deck)],
|
||||
cache_time: None,
|
||||
is_personal: Some(false),
|
||||
next_offset: None,
|
||||
switch_pm_text: None,
|
||||
switch_pm_parameter: None,
|
||||
};
|
||||
lazy_static! {
|
||||
static ref DECK_RE: Regex = Regex::new(r#"^(?P<code>[ABCDEFGHIJKLMNOPQRSTUVWXYZ234567]+)(?:\s+(?P<name>.+?))?\s*$"#).unwrap();
|
||||
}
|
||||
|
||||
if let Some(deck_captures) = DECK_RE.captures(&query.query) {
|
||||
if let Some(deck_code) = deck_captures.name("code") {
|
||||
if let Ok(deck) = Deck::from_code(&deck_code.as_str()) {
|
||||
|
||||
debug!("Parsed deck successfully!");
|
||||
let name = deck_captures.name("name").map(|m| m.as_str());
|
||||
|
||||
break AnswerInlineQuery {
|
||||
inline_query_id: query.id.clone(),
|
||||
results: vec![deck_to_inlinequeryresult(&engine.cards, &deck, &name)],
|
||||
cache_time: None,
|
||||
is_personal: Some(false),
|
||||
next_offset: None,
|
||||
switch_pm_text: None,
|
||||
switch_pm_parameter: None,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
debug!("Querying the card search engine...");
|
||||
|
|
|
@ -43,17 +43,20 @@ pub fn card_to_inlinequeryresult(
|
|||
})
|
||||
}
|
||||
|
||||
/// Convert a [Deck] into a [InlineQueryResult].
|
||||
pub fn deck_to_inlinequeryresult(index: &CardIndex, deck: &Deck) -> InlineQueryResult {
|
||||
/// Convert a [Deck] with an optional name into a [InlineQueryResult].
|
||||
pub fn deck_to_inlinequeryresult(index: &CardIndex, deck: &Deck, name: &Option<&str>) -> InlineQueryResult {
|
||||
let code = deck
|
||||
.to_code(DeckCodeFormat::F1)
|
||||
.expect("serialized deck to deserialize properly");
|
||||
|
||||
InlineQueryResult::Article(InlineQueryResultArticle {
|
||||
id: format!("{:x}", md5::compute(&code)),
|
||||
title: format!("Deck with {} cards", deck.contents.len()),
|
||||
title: match &name {
|
||||
Some(name) => format!(r#"Deck "{}" with {} cards"#, name, deck.contents.len()),
|
||||
None => format!("Deck with {} cards", deck.contents.len())
|
||||
},
|
||||
input_message_content: InputMessageContent::Text(InputMessageContentText {
|
||||
message_text: display_deck(index, deck, code),
|
||||
message_text: display_deck(index, deck, &code, &name),
|
||||
parse_mode: Some(ParseMode::Html),
|
||||
entities: None,
|
||||
disable_web_page_preview: Some(true),
|
||||
|
|
Loading…
Reference in a new issue