Skip to content

Commit

Permalink
final touches
Browse files Browse the repository at this point in the history
  • Loading branch information
orzklv committed Nov 20, 2023
1 parent cc37e26 commit 0a975a2
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 13 deletions.
5 changes: 2 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
TOKEN=
HOST=
MODE=
TELOXIDE_TOKEN=
GITHUB_TOKEN=
4 changes: 2 additions & 2 deletions source.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
},
{
"name": "Rust Cookbook",
"desc": "https://rust-lang-nursery.github.io/rust-cookbook/",
"link": "Learn Rust good practices with practical code samples"
"desc": "Learn Rust good practices with practical code samples",
"link": "https://rust-lang-nursery.github.io/rust-cookbook/"
},
{
"name": "CIS 198",
Expand Down
11 changes: 10 additions & 1 deletion src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub async fn commands(
Command::Help => crate::functions::help::command(&bot, &msg, &cmd).await,
Command::Rules => crate::functions::rules::command(&bot, &msg).await,
Command::About => crate::functions::about::command(&bot, &msg).await,
Command::Groups => crate::functions::groups::command(&bot, &msg, &groups).await,
Command::Group => crate::functions::groups::command(&bot, &msg, &groups).await,
Command::Latest => crate::functions::latest::command(&bot, github, &msg).await,
Command::Version => crate::functions::version::command(&bot, github, &msg).await,
Command::Off => crate::functions::offtop::command(&bot, &msg, &me).await,
Expand All @@ -47,6 +47,7 @@ pub async fn callback(
q: CallbackQuery,
github: GitHub,
groups: Groups,
resources: Resources,
) -> Result<(), Box<dyn Error + Send + Sync>> {
let mut args: Vec<&str> = Vec::new();

Expand All @@ -64,6 +65,14 @@ pub async fn callback(
"changelog" => {
crate::functions::version::callback_detail(&bot, &q, &args, github).await
}
"useful" => crate::functions::useful::callback_list(&bot, &q, &resources).await,
"category" => {
crate::functions::useful::callback_category_list(&bot, &q, &args, &resources).await
}
"material" => {
crate::functions::useful::callback_material_detail(&bot, &q, &args, &resources)
.await
}
_ => Ok(()),
};
}
Expand Down
124 changes: 119 additions & 5 deletions src/functions/useful.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::utils::{keyboard::Keyboard, resources::Resources};
use crate::utils::{
keyboard::Keyboard,
resources::{Resource, Resources},
};
use teloxide::{
payloads::SendMessageSetters,
prelude::*,
Expand All @@ -15,27 +18,138 @@ pub async fn command(bot: &Bot, msg: &Message, resources: &Resources) -> Respons

bot.send_message(msg.chat.id, TEXT)
.parse_mode(ParseMode::Html)
.reply_markup(keyboard_list(1, categories))
.reply_markup(keyboard_list(categories))
.disable_web_page_preview(true)
.await?;

Ok(())
}

pub fn keyboard_list(page: u32, categories: Vec<String>) -> InlineKeyboardMarkup {
pub async fn callback_list(
bot: &Bot,
q: &CallbackQuery,
resources: &Resources,
) -> ResponseResult<()> {
let categories = resources.get_keys();

if let Some(Message { id, chat, .. }) = q.message.clone() {
bot.edit_message_text(chat.id, id, TEXT)
.parse_mode(ParseMode::Html)
.reply_markup(keyboard_list(categories))
.disable_web_page_preview(true)
.await?;
} else if let Some(id) = q.inline_message_id.clone() {
bot.edit_message_text_inline(id, "Oopsie, something went wrong...")
.await?;
}

Ok(())
}

pub async fn callback_category_list(
bot: &Bot,
q: &CallbackQuery,
args: &Vec<&str>,
resources: &Resources,
) -> ResponseResult<()> {
let find = resources.get_materials(args.join("_").as_str()).unwrap();

if !args.is_empty() {
if let Some(Message { id, chat, .. }) = q.message.clone() {
bot.edit_message_text(chat.id, id, view_category_list(&args.join(" ")))
.parse_mode(ParseMode::Html)
.reply_markup(keyboard_category_list(find, args.join("_")))
.await?;
} else if let Some(id) = q.inline_message_id.clone() {
bot.edit_message_text_inline(id, "Oopsie, something went wrong...")
.await?;
}
}

Ok(())
}

pub async fn callback_material_detail(
bot: &Bot,
q: &CallbackQuery,
args: &Vec<&str>,
resources: &Resources,
) -> ResponseResult<()> {
let find = resources
.get_material(args[1..].join("_").as_str(), args[0].parse().unwrap())
.unwrap();

if !args.is_empty() {
if let Some(Message { id, chat, .. }) = q.message.clone() {
bot.edit_message_text(chat.id, id, view_material_detail(find))
.parse_mode(ParseMode::Html)
.reply_markup(keyboard_material_detail(find, args[1..].join("_")))
.await?;
} else if let Some(id) = q.inline_message_id.clone() {
bot.edit_message_text_inline(id, "Oopsie, something went wrong...")
.await?;
}
}

Ok(())
}

pub fn view_category_list(category: &str) -> String {
format!("<b>Siz hozirda {}{} kategoriyasi ichida turibsiz.</b>\nIltimos, keltirilgan materiallardan birini tanlang...",
&category[0..1].to_uppercase(), &category[1..].replace('_', " "))
}

pub fn view_material_detail(material: &Resource) -> String {
format!(
"<b>{}</b>\n\n<i>{}</i>\n\n<b>Ushbu pastdagi tugmacha orqali lavha ga o'tib oling:</b>",
material.name, material.desc
)
}

pub fn keyboard_list(categories: Vec<String>) -> InlineKeyboardMarkup {
let mut keyboard = Keyboard::new();

for category in categories {
keyboard.text(
&format!(
"{}{}",
&category[0..1].to_uppercase(),
&category[1..].replace("_", " ")
&category[1..].replace('_', " ")
),
&format!("category_{}", category),
);
keyboard.row();
}

keyboard.get()
}

pub fn keyboard_category_list(material: &[Resource], category: String) -> InlineKeyboardMarkup {
let mut keyboard = Keyboard::new();

for (index, value) in material.iter().enumerate() {
keyboard.text(
&format!(
"{}{}",
&value.name[0..1].to_uppercase(),
&value.name[1..].replace('_', " ")
),
&format!("useful_{}_{}", page, category),
&format!("material_{}_{}", index, category),
);
keyboard.row();
}

keyboard.text("🔙 Orqaga", "useful");

keyboard.get()
}

pub fn keyboard_material_detail(resource: &Resource, category: String) -> InlineKeyboardMarkup {
let mut keyboard = Keyboard::new();

keyboard.url("Web Sahifasi", &resource.link);
keyboard.row();
keyboard.text("🔙 Orqaga", &format!("category_{}", category));

keyboard.get()
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub enum Command {
About,

/// Available groups
Groups,
Group,

/// Latest version
Latest,
Expand Down
8 changes: 7 additions & 1 deletion src/utils/resources.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

static RESOURCE: &'static str = include_str!("../../source.json");
static RESOURCE: &str = include_str!("../../source.json");

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Resource {
Expand All @@ -15,6 +15,12 @@ pub struct Resources {
materials: HashMap<String, Vec<Resource>>,
}

impl Default for Resources {
fn default() -> Self {
Self::new()
}
}

impl Resources {
pub fn new() -> Self {
Resources {
Expand Down

0 comments on commit 0a975a2

Please sign in to comment.