mirror of
https://github.com/Steffo99/patched-porobot.git
synced 2025-01-03 15:34:18 +00:00
Introudce a "crystal" segment in Telegram inline query ids
To work around the bizzare caching mechanisms Telegram seems to have.
This commit is contained in:
parent
72694053f2
commit
93451b10ec
5 changed files with 22 additions and 7 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1048,6 +1048,7 @@ dependencies = [
|
|||
"log",
|
||||
"md5",
|
||||
"pretty_env_logger",
|
||||
"rand",
|
||||
"regex",
|
||||
"reqwest",
|
||||
"serde",
|
||||
|
|
|
@ -36,6 +36,7 @@ teloxide = { version = "0.10.1", optional = true }
|
|||
reqwest = { version = "0.11.11", optional = true }
|
||||
tokio = { version = "1.20.1", features = ["rt-multi-thread", "macros"], optional = true }
|
||||
md5 = { version = "0.7.0", optional = true }
|
||||
rand = { version = "0.8.5", optional = true }
|
||||
# discord
|
||||
# matrix
|
||||
|
||||
|
@ -44,7 +45,7 @@ md5 = { version = "0.7.0", optional = true }
|
|||
# data = [] # Always included
|
||||
exec = ["pretty_env_logger", "glob"]
|
||||
search = ["tantivy"]
|
||||
telegram = ["exec", "search", "teloxide", "reqwest", "tokio", "md5"]
|
||||
telegram = ["exec", "search", "teloxide", "reqwest", "tokio", "md5", "rand"]
|
||||
discord = ["exec", "search"]
|
||||
matrix = ["exec", "search"]
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@ use regex::Regex;
|
|||
|
||||
/// Handle inline queries by searching cards on the [CardSearchEngine].
|
||||
pub fn inline_query_handler(
|
||||
crystal: String,
|
||||
engine: CardSearchEngine,
|
||||
) -> Handler<'static, DependencyMap, ResponseResult<()>, DpHandlerDescription> {
|
||||
Update::filter_inline_query().chain(dptree::endpoint(move |query: InlineQuery, bot: Bot| {
|
||||
|
@ -48,7 +49,7 @@ pub fn inline_query_handler(
|
|||
|
||||
break AnswerInlineQuery {
|
||||
inline_query_id: query.id.clone(),
|
||||
results: vec![deck_to_inlinequeryresult(&engine.cards, &deck, &name)],
|
||||
results: vec![deck_to_inlinequeryresult(&crystal, &engine.cards, &deck, &name)],
|
||||
cache_time: None,
|
||||
is_personal: Some(false),
|
||||
next_offset: None,
|
||||
|
@ -95,7 +96,7 @@ pub fn inline_query_handler(
|
|||
inline_query_id: query.id.clone(),
|
||||
results: results
|
||||
.iter()
|
||||
.map(|card| card_to_inlinequeryresult(&engine.globals, card))
|
||||
.map(|card| card_to_inlinequeryresult(&crystal, &engine.globals, card))
|
||||
.collect_vec(),
|
||||
cache_time: Some(300),
|
||||
is_personal: Some(false),
|
||||
|
|
|
@ -14,11 +14,12 @@ use teloxide::types::{
|
|||
|
||||
/// Convert a [Card] into a [InlineQueryResult].
|
||||
pub fn card_to_inlinequeryresult(
|
||||
crystal: &str,
|
||||
globals: &LocalizedGlobalsIndexes,
|
||||
card: &Card,
|
||||
) -> InlineQueryResult {
|
||||
InlineQueryResult::Photo(InlineQueryResultPhoto {
|
||||
id: card.code.full.to_owned(),
|
||||
id: format!("{}:{}", &crystal, &card.code.full),
|
||||
title: Some(card.name.to_owned()),
|
||||
caption: Some(display_card(&globals, &card)),
|
||||
parse_mode: Some(ParseMode::Html),
|
||||
|
@ -44,13 +45,18 @@ pub fn card_to_inlinequeryresult(
|
|||
}
|
||||
|
||||
/// Convert a [Deck] with an optional name into a [InlineQueryResult].
|
||||
pub fn deck_to_inlinequeryresult(index: &CardIndex, deck: &Deck, name: &Option<&str>) -> InlineQueryResult {
|
||||
pub fn deck_to_inlinequeryresult(
|
||||
crystal: &str,
|
||||
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)),
|
||||
id: format!("{}:{:x}", &crystal, md5::compute(&code)),
|
||||
title: match &name {
|
||||
Some(name) => format!(r#"Deck "{}" with {} cards"#, name, deck.contents.len()),
|
||||
None => format!("Deck with {} cards", deck.contents.len())
|
||||
|
|
|
@ -9,6 +9,7 @@ use crate::telegram::handler::{inline_query_handler, message_handler};
|
|||
use glob::glob;
|
||||
use log::*;
|
||||
use std::path::PathBuf;
|
||||
use rand::Rng;
|
||||
use teloxide::prelude::*;
|
||||
|
||||
/// The main function that `patched_porobot_telegram` should run when it's started.
|
||||
|
@ -63,9 +64,14 @@ pub async fn main() {
|
|||
.expect("Telegram bot parameters to be valid");
|
||||
debug!("Created Telegram bot!");
|
||||
|
||||
debug!("Generating crystal for this run...");
|
||||
let rng = rand::thread_rng();
|
||||
let crystal: String = String::from_utf8(rng.sample_iter(&rand::distributions::Alphanumeric).take(6).collect()).unwrap();
|
||||
debug!("Generated crystal: {}", &crystal);
|
||||
|
||||
debug!("Creating handlers...");
|
||||
let handler = dptree::entry()
|
||||
.branch(inline_query_handler(engine))
|
||||
.branch(inline_query_handler(crystal, engine))
|
||||
.branch(message_handler());
|
||||
debug!("Created handlers!");
|
||||
|
||||
|
|
Loading…
Reference in a new issue