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",
|
"log",
|
||||||
"md5",
|
"md5",
|
||||||
"pretty_env_logger",
|
"pretty_env_logger",
|
||||||
|
"rand",
|
||||||
"regex",
|
"regex",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
|
|
|
@ -36,6 +36,7 @@ teloxide = { version = "0.10.1", optional = true }
|
||||||
reqwest = { version = "0.11.11", optional = true }
|
reqwest = { version = "0.11.11", optional = true }
|
||||||
tokio = { version = "1.20.1", features = ["rt-multi-thread", "macros"], optional = true }
|
tokio = { version = "1.20.1", features = ["rt-multi-thread", "macros"], optional = true }
|
||||||
md5 = { version = "0.7.0", optional = true }
|
md5 = { version = "0.7.0", optional = true }
|
||||||
|
rand = { version = "0.8.5", optional = true }
|
||||||
# discord
|
# discord
|
||||||
# matrix
|
# matrix
|
||||||
|
|
||||||
|
@ -44,7 +45,7 @@ md5 = { version = "0.7.0", optional = true }
|
||||||
# data = [] # Always included
|
# data = [] # Always included
|
||||||
exec = ["pretty_env_logger", "glob"]
|
exec = ["pretty_env_logger", "glob"]
|
||||||
search = ["tantivy"]
|
search = ["tantivy"]
|
||||||
telegram = ["exec", "search", "teloxide", "reqwest", "tokio", "md5"]
|
telegram = ["exec", "search", "teloxide", "reqwest", "tokio", "md5", "rand"]
|
||||||
discord = ["exec", "search"]
|
discord = ["exec", "search"]
|
||||||
matrix = ["exec", "search"]
|
matrix = ["exec", "search"]
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ 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(
|
||||||
|
crystal: String,
|
||||||
engine: CardSearchEngine,
|
engine: CardSearchEngine,
|
||||||
) -> Handler<'static, DependencyMap, ResponseResult<()>, DpHandlerDescription> {
|
) -> Handler<'static, DependencyMap, ResponseResult<()>, DpHandlerDescription> {
|
||||||
Update::filter_inline_query().chain(dptree::endpoint(move |query: InlineQuery, bot: Bot| {
|
Update::filter_inline_query().chain(dptree::endpoint(move |query: InlineQuery, bot: Bot| {
|
||||||
|
@ -48,7 +49,7 @@ pub fn inline_query_handler(
|
||||||
|
|
||||||
break AnswerInlineQuery {
|
break AnswerInlineQuery {
|
||||||
inline_query_id: query.id.clone(),
|
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,
|
cache_time: None,
|
||||||
is_personal: Some(false),
|
is_personal: Some(false),
|
||||||
next_offset: None,
|
next_offset: None,
|
||||||
|
@ -95,7 +96,7 @@ pub fn inline_query_handler(
|
||||||
inline_query_id: query.id.clone(),
|
inline_query_id: query.id.clone(),
|
||||||
results: results
|
results: results
|
||||||
.iter()
|
.iter()
|
||||||
.map(|card| card_to_inlinequeryresult(&engine.globals, card))
|
.map(|card| card_to_inlinequeryresult(&crystal, &engine.globals, card))
|
||||||
.collect_vec(),
|
.collect_vec(),
|
||||||
cache_time: Some(300),
|
cache_time: Some(300),
|
||||||
is_personal: Some(false),
|
is_personal: Some(false),
|
||||||
|
|
|
@ -14,11 +14,12 @@ use teloxide::types::{
|
||||||
|
|
||||||
/// Convert a [Card] into a [InlineQueryResult].
|
/// Convert a [Card] into a [InlineQueryResult].
|
||||||
pub fn card_to_inlinequeryresult(
|
pub fn card_to_inlinequeryresult(
|
||||||
|
crystal: &str,
|
||||||
globals: &LocalizedGlobalsIndexes,
|
globals: &LocalizedGlobalsIndexes,
|
||||||
card: &Card,
|
card: &Card,
|
||||||
) -> InlineQueryResult {
|
) -> InlineQueryResult {
|
||||||
InlineQueryResult::Photo(InlineQueryResultPhoto {
|
InlineQueryResult::Photo(InlineQueryResultPhoto {
|
||||||
id: card.code.full.to_owned(),
|
id: format!("{}:{}", &crystal, &card.code.full),
|
||||||
title: Some(card.name.to_owned()),
|
title: Some(card.name.to_owned()),
|
||||||
caption: Some(display_card(&globals, &card)),
|
caption: Some(display_card(&globals, &card)),
|
||||||
parse_mode: Some(ParseMode::Html),
|
parse_mode: Some(ParseMode::Html),
|
||||||
|
@ -44,13 +45,18 @@ pub fn card_to_inlinequeryresult(
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert a [Deck] with an optional name into a [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
|
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}", &crystal, md5::compute(&code)),
|
||||||
title: match &name {
|
title: match &name {
|
||||||
Some(name) => format!(r#"Deck "{}" with {} cards"#, name, deck.contents.len()),
|
Some(name) => format!(r#"Deck "{}" with {} cards"#, name, deck.contents.len()),
|
||||||
None => format!("Deck with {} cards", 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 glob::glob;
|
||||||
use log::*;
|
use log::*;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use rand::Rng;
|
||||||
use teloxide::prelude::*;
|
use teloxide::prelude::*;
|
||||||
|
|
||||||
/// The main function that `patched_porobot_telegram` should run when it's started.
|
/// 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");
|
.expect("Telegram bot parameters to be valid");
|
||||||
debug!("Created Telegram bot!");
|
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...");
|
debug!("Creating handlers...");
|
||||||
let handler = dptree::entry()
|
let handler = dptree::entry()
|
||||||
.branch(inline_query_handler(engine))
|
.branch(inline_query_handler(crystal, engine))
|
||||||
.branch(message_handler());
|
.branch(message_handler());
|
||||||
debug!("Created handlers!");
|
debug!("Created handlers!");
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue