1
Fork 0
mirror of https://github.com/Steffo99/patched-porobot.git synced 2024-10-16 17:47:29 +00:00

Allow deck names to be specified in the Telegram bot

This commit is contained in:
Steffo 2022-10-18 15:15:32 +00:00 committed by GitHub
parent 0119dbc515
commit 961ab2cea8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 18 deletions

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,11 +35,20 @@ pub fn inline_query_handler(
}; };
} }
if let Ok(deck) = Deck::from_code(&query.query.to_ascii_uppercase()) { 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!"); debug!("Parsed deck successfully!");
let name = deck_captures.name("name").map(|m| m.as_str());
break AnswerInlineQuery { break AnswerInlineQuery {
inline_query_id: query.id.clone(), inline_query_id: query.id.clone(),
results: vec![deck_to_inlinequeryresult(&engine.cards, &deck)], results: vec![deck_to_inlinequeryresult(&engine.cards, &deck, &name)],
cache_time: None, cache_time: None,
is_personal: Some(false), is_personal: Some(false),
next_offset: None, next_offset: None,
@ -45,6 +56,8 @@ pub fn inline_query_handler(
switch_pm_parameter: None, switch_pm_parameter: None,
}; };
} }
}
}
debug!("Querying the card search engine..."); debug!("Querying the card search engine...");
let results = engine.query(&query.query, 50); let results = engine.query(&query.query, 50);

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),