Skip to content

Commit

Permalink
mvp inline
Browse files Browse the repository at this point in the history
  • Loading branch information
orzklv committed Jun 25, 2024
1 parent 52d837f commit 28c6c15
Show file tree
Hide file tree
Showing 8 changed files with 711 additions and 37 deletions.
496 changes: 461 additions & 35 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion bots/xinuxmgr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ url = "2.4.1"
uuid = { version = "1.5.0", features = ["v4"] }
serde_json = "1.0.108"
serde = { version = "1.0.192", features = ["derive"] }
orzklv = { version = "0.0.5", features = ["full"] }
orzklv = { version = "0.0.8", features = ["full"] }
libxinux = { version = "0.2.3", features = ["pkgs-async"] }
157 changes: 157 additions & 0 deletions bots/xinuxmgr/src/functions/inline.rs
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(())
}
3 changes: 3 additions & 0 deletions bots/xinuxmgr/src/functions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
pub mod about;
pub mod check;
pub mod help;
pub mod inline;
pub mod joined;
pub mod rules;
pub mod start;
pub mod warn;

pub use inline::inline;

use crate::utils::topics::Topics;
use crate::Command;
use std::error::Error;
Expand Down
2 changes: 2 additions & 0 deletions bots/xinuxmgr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub enum Command {

pub fn handler() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>> {
dptree::entry()
// Inline
.branch(Update::filter_inline_query().endpoint(functions::inline))
// Callbacks
.branch(Update::filter_callback_query().endpoint(functions::callback))
// Commands
Expand Down
4 changes: 3 additions & 1 deletion bots/xinuxmgr/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use libxinux::pkgs::any::Any as Pkgs;
use std::error::Error;
use teloxide::{prelude::*, update_listeners::webhooks};
use xinuxmgr::utils::topics::Topics;
Expand All @@ -10,10 +11,11 @@ async fn main() -> Result<(), Box<dyn Error>> {

let bot = Bot::from_env();
let topics = Topics::new();
let pkgs = Pkgs::new().unwrap();

// Dispatcher flow control
let mut dispatcher = Dispatcher::builder(bot.clone(), handler())
.dependencies(dptree::deps![topics])
.dependencies(dptree::deps![topics, pkgs])
// If no handler succeeded to handle an update, this closure will be called
.default_handler(|upd| async move {
log::warn!("Unhandled update: {:?}", upd);
Expand Down
82 changes: 82 additions & 0 deletions bots/xinuxmgr/src/utils/inlines.rs
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 &lt;distributiv&gt; &lt;nomi&gt;</code>
"#;

pub static NOT_ENOUGH: &str = r#"
<b>Foydalanuvchi, siz yetarlicha parametrlar kiritmadingiz!</b>
Iltimos, quyidagi parametrlarni kiriting:
&lt;distributiv&gt; &lt;paket nomi&gt;
"#;

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")
}
1 change: 1 addition & 0 deletions bots/xinuxmgr/src/utils/mod.rs
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) {
Expand Down

0 comments on commit 28c6c15

Please sign in to comment.