From b2a2535b93f306cf15838e4b88ed3392b9c4c235 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 7 Aug 2022 17:50:35 +0200 Subject: [PATCH] Add a way to convert `Card` into `InlineQueryResult` --- src/telegram/display.rs | 2 +- src/telegram/inline.rs | 42 +++++++++++++++++++++++++++++++++++++++++ src/telegram/mod.rs | 1 + 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/telegram/inline.rs diff --git a/src/telegram/display.rs b/src/telegram/display.rs index e819148..9fb1c07 100644 --- a/src/telegram/display.rs +++ b/src/telegram/display.rs @@ -20,7 +20,7 @@ use crate::data::setbundle::supertype::CardSupertype; /// Render a [Card] in [Telegram Bot HTML]. /// /// [Telegram Bot HTML]: https://core.telegram.org/bots/api#html-style -pub fn display_card(card: &Card, globals: &LocalizedGlobalsIndexes) -> String { +pub fn display_card(globals: &LocalizedGlobalsIndexes, card: &Card) -> String { let title = format!( r#"{}"#, &card.main_art().expect("Card to have at least one illustration").card_png, diff --git a/src/telegram/inline.rs b/src/telegram/inline.rs new file mode 100644 index 0000000..168a810 --- /dev/null +++ b/src/telegram/inline.rs @@ -0,0 +1,42 @@ +//! Module defining helper functions for returning [inline mode] results. +//! +//! [inline mode]: https://core.telegram.org/bots/api#inline-mode + +use teloxide::types::{InlineQueryResult, InlineQueryResultPhoto, InputMessageContent, InputMessageContentText, ParseMode}; +use crate::data::corebundle::globals::LocalizedGlobalsIndexes; +use crate::data::setbundle::card::Card; +use crate::telegram::display::display_card; + + +/// Converts a [Card] into a [InlineQueryResult]. +pub fn card_to_inlinequeryresult(globals: &LocalizedGlobalsIndexes, card: &Card) -> InlineQueryResult { + InlineQueryResult::Photo(InlineQueryResultPhoto { + id: card.code.to_owned(), + title: Some(card.name.to_owned()), + photo_url: card + .main_art() + .expect("Card to have at least one illustration") + .card_jpg().parse() + .expect("Card to have a valid card_jpg URL"), + thumb_url: card + .main_art() + .expect("Card to have at least one illustration") + .card_jpg().parse() + .expect("Card to have a valid card_jpg URL"), + photo_width: Some(680), + photo_height: Some(1024), + + input_message_content: Some(InputMessageContent::Text(InputMessageContentText { + message_text: display_card(&globals, &card), + parse_mode: Some(ParseMode::Html), + entities: None, + disable_web_page_preview: Some(true) + })), + + description: None, + caption: None, + parse_mode: None, + caption_entities: None, + reply_markup: None, + }) +} diff --git a/src/telegram/mod.rs b/src/telegram/mod.rs index e8e0a0a..97e0c15 100644 --- a/src/telegram/mod.rs +++ b/src/telegram/mod.rs @@ -3,3 +3,4 @@ //! Remember while adding new features to this module that binaries [can only access the public API of the crate](https://doc.rust-lang.org/cargo/reference/cargo-targets.html#binaries). pub mod display; +pub mod inline;