-
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
12 changed files
with
211 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,88 @@ | ||
use crates_io_api::AsyncClient; | ||
use crates_io_api::CratesQuery; | ||
use std::error::Error; | ||
use teloxide::prelude::*; | ||
use teloxide::types::*; | ||
|
||
use crate::utils::inmgr::*; | ||
|
||
pub async fn inline( | ||
bot: Bot, | ||
crates_client: AsyncClient, | ||
q: InlineQuery, | ||
) -> Result<(), Box<dyn Error + Send + Sync>> { | ||
if q.query.is_empty() { | ||
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(()) | ||
}; | ||
} | ||
|
||
let query = CratesQuery::builder() | ||
.search(q.query.clone()) | ||
.page(1) | ||
.page_size(50) | ||
.build(); | ||
|
||
let request = crates_client.crates(query).await.unwrap().crates; | ||
|
||
if request.is_empty() { | ||
return { | ||
bot.answer_inline_query( | ||
q.id, | ||
vec![InlineQueryResultArticle::new( | ||
"404", | ||
"Xatolik yuz berdi!", | ||
InputMessageContent::Text( | ||
InputMessageContentText::new( | ||
format!("<b>{} ga oid natija mavjud emas!</b>\n{}", | ||
q.query.clone(), "Iltimos, boshqattan ushbu qidirmoqchi bo'lgan paketingiz yozib qidirib ko'ring!") | ||
) | ||
.parse_mode(ParseMode::Html) | ||
.disable_web_page_preview(true), | ||
), | ||
) | ||
.reply_markup(err_keyboard()) | ||
.into()], | ||
) | ||
.await?; | ||
Ok(()) | ||
}; | ||
} | ||
|
||
let mut result: Vec<InlineQueryResult> = Vec::new(); | ||
|
||
for c in request.iter() { | ||
result.push(InlineQueryResult::Article( | ||
InlineQueryResultArticle::new( | ||
uuid::Uuid::new_v4(), | ||
c.name.clone(), | ||
InputMessageContent::Text( | ||
InputMessageContentText::new(view_generate(c)) | ||
.parse_mode(ParseMode::Html) | ||
.disable_web_page_preview(true), | ||
), | ||
) | ||
.description(c.description.clone().unwrap()) | ||
.url(url::Url::parse(&format!("https://crates.io/crates/{}", c.id)).unwrap()) | ||
.into(), | ||
)); | ||
} | ||
|
||
bot.answer_inline_query(q.id, result).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
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 @@ | ||
use crates_io_api::AsyncClient; | ||
use rustina::handler; | ||
use std::error::Error; | ||
use teloxide::prelude::*; | ||
|
@@ -9,7 +10,14 @@ async fn main() -> Result<(), Box<dyn Error>> { | |
|
||
let bot = Bot::from_env(); | ||
|
||
let crates_client = AsyncClient::new( | ||
"Rustina Assistant ([email protected])", | ||
std::time::Duration::from_millis(100), | ||
) | ||
.unwrap(); | ||
|
||
Dispatcher::builder(bot, handler()) | ||
.dependencies(dptree::deps![crates_client]) | ||
// If no handler succeeded to handle an update, this closure will be called | ||
.default_handler(|upd| async move { | ||
log::warn!("Unhandled update: {:?}", upd); | ||
|
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,60 @@ | ||
use super::kbmng::Keyboard; | ||
use crates_io_api::Crate; | ||
use teloxide::types::*; | ||
|
||
pub static NO_INPUT: &str = r#" | ||
<b>Salom foydalanuvchi!</b> | ||
Siz inline rejim ishga tushurdingiz. Ushbu qulaylik yordamida siz crates.io Rust dasturlash tili paketlar registridan web sahifani ishlatmasdan turib telegram o'zida kerakli paketlarni qidirishingiz mumkin! Ushbu qulaylik yozish uchun o'zimizning <a href="https://github.com/rust-lang-uz/crates.ts">API SDK</a> ishlatildi. | ||
Qidirishni boshlash uchun: | ||
<code>@rustaceanbot <nomi></code> | ||
"#; | ||
|
||
pub fn view_generate(c: &Crate) -> String { | ||
let mut result = String::from("<b>🦀 Rust Telegram Cratelari 🦀</b>\n\n"); | ||
|
||
result.push_str(&format!("📦 <b>Nomi:</b> {}\n", c.name)); | ||
result.push_str(&format!( | ||
"🚨 <b>Oxirgi versiya:</b> <code>{}</code>\n", | ||
c.max_version | ||
)); | ||
result.push_str(&format!( | ||
"🎚 <b>Yuklab olingan:</b> yaqin orada: <code>{}</code> | hammasi: <code>{}</code>\n", | ||
c.recent_downloads.unwrap(), | ||
c.downloads | ||
)); | ||
result.push_str(&format!( | ||
"⌚️ <b>Yaratilgan:</b> <code>{}</code\n>", | ||
c.created_at.naive_local().to_string() | ||
)); | ||
result.push_str(&format!( | ||
"📡 <b>Yangilangan:</b> <code>{}</code>\n", | ||
c.updated_at.date_naive().to_string() | ||
)); | ||
result.push_str(&format!( | ||
"📰 <b>Ma'lumot:</b> <code>{}{}</code>\n\n", | ||
if c.description.clone().unwrap().len() > 100 { | ||
c.description | ||
.clone() | ||
.unwrap() | ||
.chars() | ||
.take(100) | ||
.collect::<String>() | ||
} else { | ||
c.description.clone().unwrap() | ||
}, | ||
if c.description.clone().unwrap().len() > 100 { | ||
"..." | ||
} else { | ||
"" | ||
} | ||
)); | ||
|
||
result | ||
} | ||
|
||
pub fn err_keyboard() -> InlineKeyboardMarkup { | ||
let mut keyboard = Keyboard::new(); | ||
keyboard.switch_inline_current("Qayta urinib ko'ramizmi?", "rand") | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod inmgr; | ||
pub mod kbmng; |