-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
711 additions
and
37 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
use crate::utils::inlines::*; | ||
use libxinux::pkgs::any::{Any as Pkgs, Data}; | ||
use std::error::Error; | ||
use teloxide::{prelude::*, types::*}; | ||
|
||
pub async fn inline( | ||
bot: Bot, | ||
pkgs: Pkgs, | ||
q: InlineQuery, | ||
) -> Result<(), Box<dyn Error + Send + Sync>> { | ||
let parsed: String = q.query.clone(); | ||
|
||
let parsed = parsed.split_whitespace().collect::<Vec<&str>>(); | ||
|
||
match parsed.len() { | ||
0 => { | ||
return { | ||
bot.answer_inline_query( | ||
q.id, | ||
vec![InlineQueryResultArticle::new( | ||
"101", | ||
"Qidirishni boshlang!", | ||
InputMessageContent::Text( | ||
InputMessageContentText::new(NO_INPUT) | ||
.parse_mode(ParseMode::Html) | ||
.disable_web_page_preview(true), | ||
), | ||
) | ||
.reply_markup(err_keyboard()) | ||
.into()], | ||
) | ||
.await?; | ||
Ok(()) | ||
}; | ||
} | ||
1 => { | ||
return { | ||
bot.answer_inline_query( | ||
q.id, | ||
vec![InlineQueryResultArticle::new( | ||
"102", | ||
"Parametrlar yetarli emas!", | ||
InputMessageContent::Text( | ||
InputMessageContentText::new(NOT_ENOUGH) | ||
.parse_mode(ParseMode::Html) | ||
.disable_web_page_preview(true), | ||
), | ||
) | ||
.reply_markup(err_keyboard()) | ||
.into()], | ||
) | ||
.await?; | ||
Ok(()) | ||
}; | ||
} | ||
2 => {} | ||
3.. => { | ||
return { | ||
bot.answer_inline_query( | ||
q.id, | ||
vec![InlineQueryResultArticle::new( | ||
"103", | ||
"Parametrlar haddan ko'p!", | ||
InputMessageContent::Text( | ||
InputMessageContentText::new(TOO_MANY) | ||
.parse_mode(ParseMode::Html) | ||
.disable_web_page_preview(true), | ||
), | ||
) | ||
.reply_markup(err_keyboard()) | ||
.into()], | ||
) | ||
.await?; | ||
Ok(()) | ||
}; | ||
} | ||
} | ||
|
||
let request = pkgs.search(parsed[1]).await; | ||
|
||
println!("{:?}", request); | ||
|
||
let request: Vec<Data> = match request { | ||
Ok(v) => v, | ||
Err(_) => { | ||
return { | ||
bot.answer_inline_query( | ||
q.id, | ||
vec![InlineQueryResultArticle::new( | ||
"500", | ||
"Xatolik yuz berdi!", | ||
InputMessageContent::Text( | ||
InputMessageContentText::new( | ||
format!("<b>API dan ma'lumot olishda xatolik yuz berdi!</b>\nIltimos, qayta yoki keyinroq urinib ko'ring!") | ||
) | ||
.parse_mode(ParseMode::Html) | ||
.disable_web_page_preview(true), | ||
), | ||
) | ||
.reply_markup(err_keyboard()) | ||
.into()], | ||
) | ||
.await?; | ||
Ok(()) | ||
}; | ||
} | ||
}; | ||
|
||
// get only 50 results | ||
let request: Vec<&Data> = request.iter().take(49).collect(); | ||
|
||
if request.is_empty() { | ||
return { | ||
bot.answer_inline_query( | ||
q.id, | ||
vec![InlineQueryResultArticle::new( | ||
"404", | ||
"Couldn't find!", | ||
InputMessageContent::Text( | ||
InputMessageContentText::new( | ||
format!("<b>There are no results related to {}!</b>\nPlease, Try to search with other names or parameters!", | ||
q.query.clone()) | ||
) | ||
.parse_mode(ParseMode::Html) | ||
.disable_web_page_preview(true), | ||
), | ||
) | ||
.reply_markup(err_keyboard()) | ||
.into()], | ||
) | ||
.await?; | ||
Ok(()) | ||
}; | ||
} | ||
|
||
let request: Vec<InlineQueryResult> = request | ||
.iter() | ||
.map(|d: &&Data| { | ||
InlineQueryResult::Article( | ||
InlineQueryResultArticle::new( | ||
uuid::Uuid::new_v4(), | ||
d.name.clone(), | ||
InputMessageContent::Text( | ||
InputMessageContentText::new(view_generate(d)) | ||
.parse_mode(ParseMode::Html) | ||
.disable_web_page_preview(true), | ||
), | ||
) | ||
.description(d.description.clone().unwrap()) | ||
.reply_markup(kb_generate(d)), | ||
) | ||
}) | ||
.collect(); | ||
|
||
bot.answer_inline_query(q.id, request).send().await?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use libxinux::pkgs::any::{Data, Type}; | ||
use orzklv::telegram::keyboard::Keyboard; | ||
use teloxide::types::*; | ||
|
||
pub static NO_INPUT: &str = r#" | ||
<b>Salom foydalanuvchi!</b> | ||
Siz hozir inlayn rejim ishga tushurdingiz. Bu xususiyat yordamida siz Arch Linux yoki NixOS paketlarini qidirishingiz va chatda ulashishingiz mumkin. Qidirishni boshlash uchun, quyida keltirilgan misol tariqasida yozing: | ||
<code>@xinuxmgr <distributiv> <nomi></code> | ||
"#; | ||
|
||
pub static NOT_ENOUGH: &str = r#" | ||
<b>Foydalanuvchi, siz yetarlicha parametrlar kiritmadingiz!</b> | ||
Iltimos, quyidagi parametrlarni kiriting: | ||
<distributiv> <paket nomi> | ||
"#; | ||
|
||
pub static TOO_MANY: &str = r#" | ||
<b>Foydalanuvchi, siz keraklidan ko'p parametrlar kiritdingiz!</b> | ||
"#; | ||
|
||
pub fn view_generate(d: &Data) -> String { | ||
let d = d.clone(); | ||
let mut result = String::new(); | ||
|
||
result.push_str(&format!("<b>Nomi:</b> {}\n", d.name)); | ||
|
||
result.push_str(&format!("<b>Versiyasi:</b> <code>{}</code>\n", d.version)); | ||
|
||
result.push_str(&format!( | ||
"<b>Ma'lutot:</b> {}\n", | ||
d.description.unwrap_or("Ma'lumot yo'q".to_string()) | ||
)); | ||
|
||
if d.repo.is_some() { | ||
result.push_str(&format!( | ||
"<b>Repozitoriya:</b> {}\n", | ||
d.repo.clone().unwrap() | ||
)); | ||
} | ||
|
||
result.push_str("🔌 <b>O'rnatish uchun:</b> \n"); | ||
|
||
match d.repo { | ||
Some(_) => { | ||
result.push_str(&format!("<code>pacman -S {}</code>\n", d.name)); | ||
} | ||
None => { | ||
result.push_str(&format!("<code>paru -S {}</code>\n", d.name)); | ||
} | ||
} | ||
|
||
result | ||
} | ||
|
||
pub fn kb_generate(d: &Data) -> InlineKeyboardMarkup { | ||
let d = d.clone(); | ||
let mut keyboard = Keyboard::new(); | ||
|
||
keyboard | ||
.url( | ||
"Web sahifasi", | ||
match d.types { | ||
Type::Aur => format!("https://aur.archlinux.org/packages/{}", d.name), | ||
Type::Std => format!( | ||
"https://archlinux.org/packages/{}/{}/{}", | ||
d.repo.unwrap(), | ||
d.arch, | ||
d.name | ||
), | ||
} | ||
.as_str(), | ||
) | ||
.unwrap() | ||
} | ||
|
||
pub fn err_keyboard() -> InlineKeyboardMarkup { | ||
let mut keyboard = Keyboard::new(); | ||
keyboard.switch_inline_current("Qayta urinib ko'ramizmi?", "arch linux") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod inlines; | ||
pub mod topics; | ||
|
||
pub fn clog(title: &str, message: &str) { | ||
|