mirror of
https://github.com/Steffo99/patched-porobot.git
synced 2024-12-23 01:54:22 +00:00
It is (mostly) done!
This commit is contained in:
parent
25df599586
commit
d8328cbd22
2 changed files with 118 additions and 40 deletions
|
@ -14,8 +14,9 @@ async fn main() {
|
||||||
use patched_porobot::data::corebundle::globals::LocalizedGlobalsIndexes;
|
use patched_porobot::data::corebundle::globals::LocalizedGlobalsIndexes;
|
||||||
use patched_porobot::search::cardsearch::CardSearchEngine;
|
use patched_porobot::search::cardsearch::CardSearchEngine;
|
||||||
use patched_porobot::telegram::inline::card_to_inlinequeryresult;
|
use patched_porobot::telegram::inline::card_to_inlinequeryresult;
|
||||||
use teloxide::payloads::AnswerInlineQuery;
|
use teloxide::payloads::{AnswerInlineQuery, SendMessage};
|
||||||
use teloxide::requests::JsonRequest;
|
use teloxide::requests::JsonRequest;
|
||||||
|
use teloxide::types::{Recipient, ParseMode};
|
||||||
use teloxide::prelude::*;
|
use teloxide::prelude::*;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
@ -62,43 +63,60 @@ async fn main() {
|
||||||
let me = bot.get_me().send().await.expect("Telegram bot parameters to be valid");
|
let me = bot.get_me().send().await.expect("Telegram bot parameters to be valid");
|
||||||
debug!("Created Telegram bot!");
|
debug!("Created Telegram bot!");
|
||||||
|
|
||||||
|
debug!("Creating message handler...");
|
||||||
|
let message_handler = Update::filter_message().chain(dptree::endpoint(move |message: Message, bot: Bot| {
|
||||||
|
info!("Handling private message: `{:?}`", &message.text());
|
||||||
|
|
||||||
|
let payload = SendMessage {
|
||||||
|
chat_id: Recipient::Id(message.chat.id.clone()),
|
||||||
|
// TODO: Add a proper message here.
|
||||||
|
text: "TODO: Introduction message!".to_string(),
|
||||||
|
parse_mode: Some(ParseMode::Html),
|
||||||
|
entities: None,
|
||||||
|
disable_web_page_preview: Some(true),
|
||||||
|
disable_notification: None,
|
||||||
|
protect_content: None,
|
||||||
|
reply_to_message_id: None,
|
||||||
|
allow_sending_without_reply: None,
|
||||||
|
reply_markup: None
|
||||||
|
};
|
||||||
|
|
||||||
|
async move {
|
||||||
|
let telegram_reply = JsonRequest::new(bot.clone(), payload).send().await;
|
||||||
|
|
||||||
|
if let Err(e) = telegram_reply {
|
||||||
|
error!("{:?}", &e);
|
||||||
|
}
|
||||||
|
|
||||||
|
respond(())
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
debug!("Created message handler!");
|
||||||
|
|
||||||
debug!("Creating inline query handler...");
|
debug!("Creating inline query handler...");
|
||||||
let handler = Update::filter_inline_query().chain(dptree::endpoint(move |query: InlineQuery, bot: Bot| {
|
let inline_query_handler = Update::filter_inline_query().chain(dptree::endpoint(move |query: InlineQuery, bot: Bot| {
|
||||||
info!("Handling inline query: `{}`", &query.query);
|
info!("Handling inline query: `{}`", &query.query);
|
||||||
|
|
||||||
debug!("Querying the search engine...");
|
// It's not a real loop, it's just to make the code flow more tolerable.
|
||||||
let performed_query = engine.query(&query.query, 50);
|
let payload: AnswerInlineQuery = loop {
|
||||||
|
if query.query.len() == 0 {
|
||||||
let payload = match performed_query {
|
break AnswerInlineQuery {
|
||||||
Ok(results) => {
|
|
||||||
if results.len() > 0 {
|
|
||||||
AnswerInlineQuery {
|
|
||||||
inline_query_id: query.id.clone(),
|
|
||||||
results: results
|
|
||||||
.iter()
|
|
||||||
.map(|card| card_to_inlinequeryresult(&engine.globals, card))
|
|
||||||
.collect_vec(),
|
|
||||||
cache_time: Some(86400),
|
|
||||||
is_personal: Some(false),
|
|
||||||
next_offset: None,
|
|
||||||
switch_pm_text: None,
|
|
||||||
switch_pm_parameter: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
AnswerInlineQuery {
|
|
||||||
inline_query_id: query.id.clone(),
|
inline_query_id: query.id.clone(),
|
||||||
results: vec![],
|
results: vec![],
|
||||||
cache_time: None,
|
cache_time: None,
|
||||||
is_personal: Some(false),
|
is_personal: Some(false),
|
||||||
next_offset: None,
|
next_offset: None,
|
||||||
switch_pm_text: Some("No results found".to_string()),
|
switch_pm_text: Some("Open help message".to_string()),
|
||||||
switch_pm_parameter: Some("err-no-results".to_string()),
|
switch_pm_parameter: Some("err-no-query".to_string()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Err(_) => {
|
debug!("Querying the search engine...");
|
||||||
AnswerInlineQuery {
|
let results = engine.query(&query.query, 50);
|
||||||
|
|
||||||
|
if let Err(_) = results {
|
||||||
|
debug!("Invalid query syntax.");
|
||||||
|
break AnswerInlineQuery {
|
||||||
inline_query_id: query.id.clone(),
|
inline_query_id: query.id.clone(),
|
||||||
results: vec![],
|
results: vec![],
|
||||||
cache_time: None,
|
cache_time: None,
|
||||||
|
@ -108,6 +126,35 @@ async fn main() {
|
||||||
switch_pm_parameter: Some("err-invalid-query".to_string()),
|
switch_pm_parameter: Some("err-invalid-query".to_string()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
let results = results.unwrap();
|
||||||
|
|
||||||
|
let len = results.len();
|
||||||
|
if len == 0 {
|
||||||
|
debug!("No cards found.");
|
||||||
|
break AnswerInlineQuery {
|
||||||
|
inline_query_id: query.id.clone(),
|
||||||
|
results: vec![],
|
||||||
|
cache_time: None,
|
||||||
|
is_personal: Some(false),
|
||||||
|
next_offset: None,
|
||||||
|
switch_pm_text: Some("No cards found".to_string()),
|
||||||
|
switch_pm_parameter: Some("err-no-results".to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
debug!("Found {} cards.", &len);
|
||||||
|
break AnswerInlineQuery {
|
||||||
|
inline_query_id: query.id.clone(),
|
||||||
|
results: results
|
||||||
|
.iter()
|
||||||
|
.map(|card| card_to_inlinequeryresult(&engine.globals, card))
|
||||||
|
.collect_vec(),
|
||||||
|
cache_time: Some(300),
|
||||||
|
is_personal: Some(false),
|
||||||
|
next_offset: None,
|
||||||
|
switch_pm_text: None,
|
||||||
|
switch_pm_parameter: None,
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
async move {
|
async move {
|
||||||
|
@ -122,6 +169,16 @@ async fn main() {
|
||||||
}));
|
}));
|
||||||
debug!("Create inline query handler!");
|
debug!("Create inline query handler!");
|
||||||
|
|
||||||
|
debug!("Merging handlers...");
|
||||||
|
let handler = dptree::entry()
|
||||||
|
.branch(inline_query_handler)
|
||||||
|
.branch(message_handler);
|
||||||
|
debug!("Merged handlers!");
|
||||||
|
|
||||||
info!("@{} is ready!", &me.username.as_ref().expect("bot to have an username"));
|
info!("@{} is ready!", &me.username.as_ref().expect("bot to have an username"));
|
||||||
Dispatcher::builder(bot, handler).enable_ctrlc_handler().build().dispatch().await;
|
Dispatcher::builder(bot, handler)
|
||||||
|
.enable_ctrlc_handler()
|
||||||
|
.build()
|
||||||
|
.dispatch()
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,17 +28,17 @@ pub fn display_card(globals: &LocalizedGlobalsIndexes, card: &Card) -> String {
|
||||||
|
|
||||||
let stats = match &card.r#type {
|
let stats = match &card.r#type {
|
||||||
CardType::Spell => format!(
|
CardType::Spell => format!(
|
||||||
"{} mana\n",
|
"{} mana\n\n",
|
||||||
escape(&card.cost.to_string()),
|
escape(&card.cost.to_string()),
|
||||||
),
|
),
|
||||||
CardType::Unit => format!(
|
CardType::Unit => format!(
|
||||||
"{} mana {}|{}\n",
|
"{} mana {}|{}\n\n",
|
||||||
escape(&card.cost.to_string()),
|
escape(&card.cost.to_string()),
|
||||||
escape(&card.attack.to_string()),
|
escape(&card.attack.to_string()),
|
||||||
escape(&card.health.to_string()),
|
escape(&card.health.to_string()),
|
||||||
),
|
),
|
||||||
CardType::Landmark => format!(
|
CardType::Landmark => format!(
|
||||||
"{} mana\n",
|
"{} mana\n\n",
|
||||||
&card.cost
|
&card.cost
|
||||||
),
|
),
|
||||||
_ => "".to_string(),
|
_ => "".to_string(),
|
||||||
|
@ -48,11 +48,12 @@ pub fn display_card(globals: &LocalizedGlobalsIndexes, card: &Card) -> String {
|
||||||
let regions = display_regions(&card.regions, &globals.regions);
|
let regions = display_regions(&card.regions, &globals.regions);
|
||||||
let r#type = display_types(&card.r#type, &card.supertype, &card.subtypes);
|
let r#type = display_types(&card.r#type, &card.supertype, &card.subtypes);
|
||||||
|
|
||||||
let breadcrumbs = format!("{} › {} › {}\n", &set, ®ions, &r#type);
|
let breadcrumbs = format!("{} › {} › {}\n\n", &set, ®ions, &r#type);
|
||||||
|
|
||||||
let keywords = display_keywords(&card.keywords, &globals.keywords);
|
let keywords = display_keywords(&card.keywords, &globals.keywords);
|
||||||
|
|
||||||
let description = format!("{}\n", escape(&card.localized_description_text));
|
let description = display_description(&card.localized_description_text);
|
||||||
|
let levelup = display_levelup(&card.localized_levelup_text);
|
||||||
|
|
||||||
let flavor = format!(
|
let flavor = format!(
|
||||||
"<i>{}</i>\n",
|
"<i>{}</i>\n",
|
||||||
|
@ -66,7 +67,7 @@ pub fn display_card(globals: &LocalizedGlobalsIndexes, card: &Card) -> String {
|
||||||
);
|
);
|
||||||
|
|
||||||
format!(
|
format!(
|
||||||
"{title}{breadcrumbs}\n{keywords}{stats}{description}\n-----\n{flavor}{artist}",
|
"{title}{breadcrumbs}{keywords}{stats}{description}{levelup}-----\n{flavor}{artist}",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,3 +151,23 @@ fn display_keywords(keywords: &[CardKeyword], hm: &LocalizedCardKeywordIndex) ->
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn display_description(description: &String) -> String {
|
||||||
|
if description == "" {
|
||||||
|
"".to_string()
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
format!("{}\n\n", escape(&description))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn display_levelup(levelup: &String) -> String {
|
||||||
|
if levelup == "" {
|
||||||
|
"".to_string()
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
format!("<u>Level up</u>: {}\n\n", escape(&levelup))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue