Skip to content

Commit 0a975a2

Browse files
committed
final touches
1 parent cc37e26 commit 0a975a2

6 files changed

Lines changed: 141 additions & 13 deletions

File tree

‎.env.example‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
TOKEN=
2-
HOST=
3-
MODE=
1+
TELOXIDE_TOKEN=
2+
GITHUB_TOKEN=

‎source.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
},
1313
{
1414
"name": "Rust Cookbook",
15-
"desc": "https://rust-lang-nursery.github.io/rust-cookbook/",
16-
"link": "Learn Rust good practices with practical code samples"
15+
"desc": "Learn Rust good practices with practical code samples",
16+
"link": "https://rust-lang-nursery.github.io/rust-cookbook/"
1717
},
1818
{
1919
"name": "CIS 198",

‎src/functions/mod.rs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub async fn commands(
3232
Command::Help => crate::functions::help::command(&bot, &msg, &cmd).await,
3333
Command::Rules => crate::functions::rules::command(&bot, &msg).await,
3434
Command::About => crate::functions::about::command(&bot, &msg).await,
35-
Command::Groups => crate::functions::groups::command(&bot, &msg, &groups).await,
35+
Command::Group => crate::functions::groups::command(&bot, &msg, &groups).await,
3636
Command::Latest => crate::functions::latest::command(&bot, github, &msg).await,
3737
Command::Version => crate::functions::version::command(&bot, github, &msg).await,
3838
Command::Off => crate::functions::offtop::command(&bot, &msg, &me).await,
@@ -47,6 +47,7 @@ pub async fn callback(
4747
q: CallbackQuery,
4848
github: GitHub,
4949
groups: Groups,
50+
resources: Resources,
5051
) -> Result<(), Box<dyn Error + Send + Sync>> {
5152
let mut args: Vec<&str> = Vec::new();
5253

@@ -64,6 +65,14 @@ pub async fn callback(
6465
"changelog" => {
6566
crate::functions::version::callback_detail(&bot, &q, &args, github).await
6667
}
68+
"useful" => crate::functions::useful::callback_list(&bot, &q, &resources).await,
69+
"category" => {
70+
crate::functions::useful::callback_category_list(&bot, &q, &args, &resources).await
71+
}
72+
"material" => {
73+
crate::functions::useful::callback_material_detail(&bot, &q, &args, &resources)
74+
.await
75+
}
6776
_ => Ok(()),
6877
};
6978
}

‎src/functions/useful.rs‎

Lines changed: 119 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::utils::{keyboard::Keyboard, resources::Resources};
1+
use crate::utils::{
2+
keyboard::Keyboard,
3+
resources::{Resource, Resources},
4+
};
25
use teloxide::{
36
payloads::SendMessageSetters,
47
prelude::*,
@@ -15,27 +18,138 @@ pub async fn command(bot: &Bot, msg: &Message, resources: &Resources) -> Respons
1518

1619
bot.send_message(msg.chat.id, TEXT)
1720
.parse_mode(ParseMode::Html)
18-
.reply_markup(keyboard_list(1, categories))
21+
.reply_markup(keyboard_list(categories))
1922
.disable_web_page_preview(true)
2023
.await?;
2124

2225
Ok(())
2326
}
2427

25-
pub fn keyboard_list(page: u32, categories: Vec<String>) -> InlineKeyboardMarkup {
28+
pub async fn callback_list(
29+
bot: &Bot,
30+
q: &CallbackQuery,
31+
resources: &Resources,
32+
) -> ResponseResult<()> {
33+
let categories = resources.get_keys();
34+
35+
if let Some(Message { id, chat, .. }) = q.message.clone() {
36+
bot.edit_message_text(chat.id, id, TEXT)
37+
.parse_mode(ParseMode::Html)
38+
.reply_markup(keyboard_list(categories))
39+
.disable_web_page_preview(true)
40+
.await?;
41+
} else if let Some(id) = q.inline_message_id.clone() {
42+
bot.edit_message_text_inline(id, "Oopsie, something went wrong...")
43+
.await?;
44+
}
45+
46+
Ok(())
47+
}
48+
49+
pub async fn callback_category_list(
50+
bot: &Bot,
51+
q: &CallbackQuery,
52+
args: &Vec<&str>,
53+
resources: &Resources,
54+
) -> ResponseResult<()> {
55+
let find = resources.get_materials(args.join("_").as_str()).unwrap();
56+
57+
if !args.is_empty() {
58+
if let Some(Message { id, chat, .. }) = q.message.clone() {
59+
bot.edit_message_text(chat.id, id, view_category_list(&args.join(" ")))
60+
.parse_mode(ParseMode::Html)
61+
.reply_markup(keyboard_category_list(find, args.join("_")))
62+
.await?;
63+
} else if let Some(id) = q.inline_message_id.clone() {
64+
bot.edit_message_text_inline(id, "Oopsie, something went wrong...")
65+
.await?;
66+
}
67+
}
68+
69+
Ok(())
70+
}
71+
72+
pub async fn callback_material_detail(
73+
bot: &Bot,
74+
q: &CallbackQuery,
75+
args: &Vec<&str>,
76+
resources: &Resources,
77+
) -> ResponseResult<()> {
78+
let find = resources
79+
.get_material(args[1..].join("_").as_str(), args[0].parse().unwrap())
80+
.unwrap();
81+
82+
if !args.is_empty() {
83+
if let Some(Message { id, chat, .. }) = q.message.clone() {
84+
bot.edit_message_text(chat.id, id, view_material_detail(find))
85+
.parse_mode(ParseMode::Html)
86+
.reply_markup(keyboard_material_detail(find, args[1..].join("_")))
87+
.await?;
88+
} else if let Some(id) = q.inline_message_id.clone() {
89+
bot.edit_message_text_inline(id, "Oopsie, something went wrong...")
90+
.await?;
91+
}
92+
}
93+
94+
Ok(())
95+
}
96+
97+
pub fn view_category_list(category: &str) -> String {
98+
format!("<b>Siz hozirda {}{} kategoriyasi ichida turibsiz.</b>\nIltimos, keltirilgan materiallardan birini tanlang...",
99+
&category[0..1].to_uppercase(), &category[1..].replace('_', " "))
100+
}
101+
102+
pub fn view_material_detail(material: &Resource) -> String {
103+
format!(
104+
"<b>{}</b>\n\n<i>{}</i>\n\n<b>Ushbu pastdagi tugmacha orqali lavha ga o'tib oling:</b>",
105+
material.name, material.desc
106+
)
107+
}
108+
109+
pub fn keyboard_list(categories: Vec<String>) -> InlineKeyboardMarkup {
26110
let mut keyboard = Keyboard::new();
27111

28112
for category in categories {
29113
keyboard.text(
30114
&format!(
31115
"{}{}",
32116
&category[0..1].to_uppercase(),
33-
&category[1..].replace("_", " ")
117+
&category[1..].replace('_', " ")
118+
),
119+
&format!("category_{}", category),
120+
);
121+
keyboard.row();
122+
}
123+
124+
keyboard.get()
125+
}
126+
127+
pub fn keyboard_category_list(material: &[Resource], category: String) -> InlineKeyboardMarkup {
128+
let mut keyboard = Keyboard::new();
129+
130+
for (index, value) in material.iter().enumerate() {
131+
keyboard.text(
132+
&format!(
133+
"{}{}",
134+
&value.name[0..1].to_uppercase(),
135+
&value.name[1..].replace('_', " ")
34136
),
35-
&format!("useful_{}_{}", page, category),
137+
&format!("material_{}_{}", index, category),
36138
);
37139
keyboard.row();
38140
}
39141

142+
keyboard.text("🔙 Orqaga", "useful");
143+
144+
keyboard.get()
145+
}
146+
147+
pub fn keyboard_material_detail(resource: &Resource, category: String) -> InlineKeyboardMarkup {
148+
let mut keyboard = Keyboard::new();
149+
150+
keyboard.url("Web Sahifasi", &resource.link);
151+
keyboard.row();
152+
keyboard.text("🔙 Orqaga", &format!("category_{}", category));
153+
40154
keyboard.get()
41155
}

‎src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub enum Command {
2525
About,
2626

2727
/// Available groups
28-
Groups,
28+
Group,
2929

3030
/// Latest version
3131
Latest,

‎src/utils/resources.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use serde::{Deserialize, Serialize};
22
use std::collections::HashMap;
33

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

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

18+
impl Default for Resources {
19+
fn default() -> Self {
20+
Self::new()
21+
}
22+
}
23+
1824
impl Resources {
1925
pub fn new() -> Self {
2026
Resources {

0 commit comments

Comments
 (0)