Skip to content

Commit

Permalink
useful command added
Browse files Browse the repository at this point in the history
  • Loading branch information
orzklv committed Nov 19, 2023
1 parent dd035c5 commit cc37e26
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/functions/about.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{hooks, utils::keyboard_manager::Keyboard};
use crate::{hooks, utils::keyboard::Keyboard};
use teloxide::{
payloads::SendMessageSetters,
prelude::*,
Expand Down
4 changes: 2 additions & 2 deletions src/functions/groups.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::utils::{
group_manager::{Group, Groups},
keyboard_manager::Keyboard,
groups::{Group, Groups},
keyboard::Keyboard,
};
use teloxide::{
payloads::SendMessageSetters,
Expand Down
2 changes: 1 addition & 1 deletion src/functions/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crates_io_api::{AsyncClient, Crate, CratesQuery};
use std::error::Error;
use teloxide::{prelude::*, types::*};

use crate::utils::inline_manager::*;
use crate::utils::inlines::*;

pub async fn inline(
bot: Bot,
Expand Down
2 changes: 1 addition & 1 deletion src/functions/latest.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{github::GitHub, keyboard_manager::Keyboard};
use crate::utils::{github::GitHub, keyboard::Keyboard};
use octocrab::models::repos::Release;
use teloxide::{
payloads::SendMessageSetters,
Expand Down
9 changes: 5 additions & 4 deletions src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ pub mod latest;
pub mod offtop;
pub mod rules;
pub mod start;
pub mod useful;
pub mod version;

pub use inline::inline;

use crate::utils::github::GitHub;
use crate::utils::group_manager::Groups;
use crate::utils::{github::GitHub, groups::Groups, resources::Resources};
use crate::Command;
use std::error::Error;
use teloxide::prelude::*;
use teloxide::types::*;
use teloxide::{prelude::*, types::*};

pub async fn commands(
bot: Bot,
Expand All @@ -26,6 +25,7 @@ pub async fn commands(
cmd: Command,
github: GitHub,
groups: Groups,
resources: Resources,
) -> Result<(), Box<dyn Error + Send + Sync>> {
let _ = match cmd {
Command::Start => crate::functions::start::command(&bot, &msg).await,
Expand All @@ -36,6 +36,7 @@ pub async fn commands(
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,
Command::Useful => crate::functions::useful::command(&bot, &msg, &resources).await,
};

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/functions/rules.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{hooks, utils::keyboard_manager::Keyboard};
use crate::{hooks, utils::keyboard::Keyboard};
use teloxide::{
payloads::SendMessageSetters,
prelude::*,
Expand Down
2 changes: 1 addition & 1 deletion src/functions/start.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::keyboard_manager::Keyboard;
use crate::utils::keyboard::Keyboard;
use teloxide::{
payloads::SendMessageSetters,
prelude::*,
Expand Down
41 changes: 41 additions & 0 deletions src/functions/useful.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::utils::{keyboard::Keyboard, resources::Resources};
use teloxide::{
payloads::SendMessageSetters,
prelude::*,
types::{InlineKeyboardMarkup, ParseMode},
};

static TEXT: &str = "<b>Rustga oid foydali materiallar:</b>\n\
Agar o'zingizdan material qo'shmoqchi bo'lsangiz, bizni \
<a href='https://github.com/rust-lang-uz/rustina/blob/main/source.json'>\
source.json</a> ni yangilang!";

pub async fn command(bot: &Bot, msg: &Message, resources: &Resources) -> ResponseResult<()> {
let categories = resources.get_keys();

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

Ok(())
}

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

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

keyboard.get()
}
2 changes: 1 addition & 1 deletion src/functions/version.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{github::GitHub, keyboard_manager::Keyboard};
use crate::utils::{github::GitHub, keyboard::Keyboard};
use octocrab::models::repos::Release;
use teloxide::{
payloads::SendMessageSetters,
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/is_private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use teloxide::{
types::{InlineKeyboardMarkup, ParseMode},
};

use crate::utils::keyboard_manager::Keyboard;
use crate::utils::keyboard::Keyboard;

static TEXT: &str = "⚠️ <b>Bu komanda faqat shaxsiy chat uchun!</b>";

Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pub enum Command {

/// Report offtopic
Off,

/// Useful resources
Useful,
}

pub fn handler() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>> {
Expand Down
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crates_io_api::AsyncClient;
use rustina::{
handler,
utils::{github::GitHub, group_manager::Groups},
utils::{github::GitHub, groups::Groups, resources::Resources},
};
use std::error::Error;
use teloxide::prelude::*;
Expand All @@ -13,16 +13,17 @@ async fn main() -> Result<(), Box<dyn Error>> {

let bot = Bot::from_env();

let groups: Groups = Groups::new();
let groups = Groups::new();
let github = GitHub::new();
let crates_client = AsyncClient::new(
"Rustina Assistant ([email protected])",
std::time::Duration::from_millis(100),
)
.unwrap();
let resources = Resources::new();

Dispatcher::builder(bot, handler())
.dependencies(dptree::deps![crates_client, github, groups])
.dependencies(dptree::deps![crates_client, github, groups, resources])
// 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
File renamed without changes.
2 changes: 1 addition & 1 deletion src/utils/inline_manager.rs → src/utils/inlines.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::keyboard_manager::Keyboard;
use super::keyboard::Keyboard;
use crates_io_api::Crate;
use teloxide::types::*;

Expand Down
File renamed without changes.
7 changes: 4 additions & 3 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod github;
pub mod group_manager;
pub mod inline_manager;
pub mod keyboard_manager;
pub mod groups;
pub mod inlines;
pub mod keyboard;
pub mod resources;
36 changes: 36 additions & 0 deletions src/utils/resources.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

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

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Resource {
pub name: String,
pub desc: String,
pub link: String,
}

#[derive(Clone, Debug)]
pub struct Resources {
materials: HashMap<String, Vec<Resource>>,
}

impl Resources {
pub fn new() -> Self {
Resources {
materials: serde_json::from_str(RESOURCE).unwrap(),
}
}

pub fn get_keys(&self) -> Vec<String> {
self.materials.keys().map(|k| k.to_string()).collect()
}

pub fn get_materials(&self, key: &str) -> Option<&Vec<Resource>> {
self.materials.get(key)
}

pub fn get_material(&self, key: &str, index: usize) -> Option<&Resource> {
self.materials.get(key).and_then(|v| v.get(index))
}
}

0 comments on commit cc37e26

Please sign in to comment.