1
Fork 0
mirror of https://github.com/Steffo99/patched-porobot.git synced 2024-10-16 09:37:27 +00:00

Allow deck codes to be sent with a name (merge #5)

Allow deck codes to be sent with a name
This commit is contained in:
Steffo 2022-10-18 23:46:18 +02:00 committed by GitHub
commit 0b438479b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 19 deletions

View file

@ -72,7 +72,7 @@
//! //!
//! Since [@patchedporobot] uses [`tantivy`] internally, you might find more information on even more advanced queries in the [documentation of their `QueryParser`](tantivy::query::QueryParser)! //! Since [@patchedporobot] uses [`tantivy`] internally, you might find more information on even more advanced queries in the [documentation of their `QueryParser`](tantivy::query::QueryParser)!
//! //!
//! ### Deck queries //! ### Deck parsing
//! //!
//! You can have [@patchedporobot] display a deck and its cards by pasting the deck code after the bot's username: //! You can have [@patchedporobot] display a deck and its cards by pasting the deck code after the bot's username:
//! //!
@ -82,6 +82,15 @@
//! //!
//! Then, select the "Deck with N cards" option to send the deck's card list in the chat! //! Then, select the "Deck with N cards" option to send the deck's card list in the chat!
//! //!
//! #### Named decks
//!
//! Optionally, you may add a name to your deck, which will be displayed above the deck code:
//!
//! ```text
//! @patchedporobot CIBQCAICAQAQGBQIBEBAMBAJBMGBUHJNGE4AEAIBAIYQEAQGEU2QCAIBAIUQ Gimbo's Depths
//! ```
//!
//! If entered correctly, the bot will display a slightly different option containing the deck's name (_Deck "NAME" with N cards_), which you can check before the message is sent to the chat.
//! //!
//! [@patchedporobot]: https://t.me/patchedporobot //! [@patchedporobot]: https://t.me/patchedporobot

View file

@ -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 /// [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 // TODO: optimize this
let cards = deck let cards = deck
.contents .contents
@ -182,5 +182,8 @@ pub fn display_deck(index: &CardIndex, deck: &Deck, code: String) -> String {
}) })
.join("\n"); .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),
}
} }

View file

@ -10,6 +10,8 @@ use teloxide::payloads::{AnswerInlineQuery, SendMessage};
use teloxide::prelude::*; use teloxide::prelude::*;
use teloxide::requests::{JsonRequest, ResponseResult}; use teloxide::requests::{JsonRequest, ResponseResult};
use teloxide::types::{ParseMode, Recipient}; use teloxide::types::{ParseMode, Recipient};
use lazy_static::lazy_static;
use regex::Regex;
/// Handle inline queries by searching cards on the [CardSearchEngine]. /// Handle inline queries by searching cards on the [CardSearchEngine].
pub fn inline_query_handler( 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()) { lazy_static! {
debug!("Parsed deck successfully!"); static ref DECK_RE: Regex = Regex::new(r#"^(?P<code>[ABCDEFGHIJKLMNOPQRSTUVWXYZ234567]+)(?:\s+(?P<name>.+?))?\s*$"#).unwrap();
break AnswerInlineQuery { }
inline_query_id: query.id.clone(),
results: vec![deck_to_inlinequeryresult(&engine.cards, &deck)], if let Some(deck_captures) = DECK_RE.captures(&query.query) {
cache_time: None, if let Some(deck_code) = deck_captures.name("code") {
is_personal: Some(false), if let Ok(deck) = Deck::from_code(&deck_code.as_str()) {
next_offset: None,
switch_pm_text: None, debug!("Parsed deck successfully!");
switch_pm_parameter: None, 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..."); debug!("Querying the card search engine...");

View file

@ -43,17 +43,20 @@ pub fn card_to_inlinequeryresult(
}) })
} }
/// Convert a [Deck] into a [InlineQueryResult]. /// Convert a [Deck] with an optional name into a [InlineQueryResult].
pub fn deck_to_inlinequeryresult(index: &CardIndex, deck: &Deck) -> InlineQueryResult { pub fn deck_to_inlinequeryresult(index: &CardIndex, deck: &Deck, name: &Option<&str>) -> InlineQueryResult {
let code = deck let code = deck
.to_code(DeckCodeFormat::F1) .to_code(DeckCodeFormat::F1)
.expect("serialized deck to deserialize properly"); .expect("serialized deck to deserialize properly");
InlineQueryResult::Article(InlineQueryResultArticle { InlineQueryResult::Article(InlineQueryResultArticle {
id: format!("{:x}", md5::compute(&code)), 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 { 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), parse_mode: Some(ParseMode::Html),
entities: None, entities: None,
disable_web_page_preview: Some(true), disable_web_page_preview: Some(true),