Skip to content

Commit

Permalink
group manager
Browse files Browse the repository at this point in the history
  • Loading branch information
orzklv committed Nov 17, 2023
1 parent 21d0161 commit 3612438
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 18 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ tokio = { version = "1.8", features = ["rt-multi-thread", "macros"] }
url = "2.4.1"
crates_io_api = "0.8.2"
uuid = { version = "1.5.0", features = ["v4"] }
serde_json = "1.0.108"
serde = { version = "1.0.192", features = ["derive"] }
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::kbmng::Keyboard};
use crate::{hooks, utils::keyboard_manager::Keyboard};
use teloxide::{
payloads::SendMessageSetters,
prelude::*,
Expand Down
48 changes: 39 additions & 9 deletions src/functions/groups.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::kbmng::Keyboard;
use crate::utils::{group_manager::Groups, keyboard_manager::Keyboard};
use teloxide::{
payloads::SendMessageSetters,
prelude::*,
Expand All @@ -7,27 +7,31 @@ use teloxide::{

static TEXT: &str = "<b>Telegramdagi Rust Hamjamiyatlari yoki Guruhlari:</b>\nAgar o'zingizni guruhingizni qo'shmoqchi bo'lsangiz, bizni <a href='https://github.com/rust-lang-uz/rustina/blob/main/communities.json'>community.json</a> ni yangilang!";

pub fn keyboard() -> InlineKeyboardMarkup {
let mut keyboard = Keyboard::new();
keyboard.url("Jamiyat", "https://t.me/rustlanguz");
keyboard.url("Web Sahifa", "https://rust-lang.uz")
}

pub async fn command(bot: &Bot, msg: &Message) -> ResponseResult<()> {
let groups: Groups = Groups::new();

bot.send_message(msg.chat.id, TEXT)
.parse_mode(ParseMode::Html)
.reply_markup(keyboard())
.reply_markup(keyboard(&groups, 1))
.await?;

Ok(())
}

pub async fn callback(bot: &Bot, q: &CallbackQuery, args: &Vec<&str>) -> ResponseResult<()> {
let groups: Groups = Groups::new();

// Parse page
let page: i32 = match args[1].parse() {
Ok(page) => page,
Err(_) => 1,
};

if args.is_empty() {
if let Some(Message { id, chat, .. }) = q.message.clone() {
bot.edit_message_text(chat.id, id, TEXT)
.parse_mode(ParseMode::Html)
.reply_markup(keyboard())
.reply_markup(keyboard(&groups, page))
.await?;
} else if let Some(id) = q.inline_message_id.clone() {
bot.edit_message_text_inline(id, "Oopsie, something went wrong...")
Expand All @@ -37,3 +41,29 @@ pub async fn callback(bot: &Bot, q: &CallbackQuery, args: &Vec<&str>) -> Respons

Ok(())
}

pub fn keyboard(groups: &Groups, page: i32) -> InlineKeyboardMarkup {
let mut keyboard = Keyboard::new();

for group in groups.get_groups(page, 5) {
keyboard.text(
&group.name,
&format!(
"detail_{}_{}",
page,
group.telegram.clone().replace("@", "")
),
);
keyboard.row();
}

if !groups.get_groups(page + 1, 5).is_empty() {
keyboard.text(&"Keyingi ➡️", &format!("group_{}", page + 1));
}

if page > 1 {
keyboard.text(&"⬅️ Oldingi", &format!("group_{}", page - 1));
}

keyboard.get()
}
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::inmgr::*;
use crate::utils::inline_manager::*;

pub async fn inline(
bot: Bot,
Expand Down
4 changes: 4 additions & 0 deletions src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ pub async fn commands(
pub async fn callback(bot: Bot, q: CallbackQuery) -> Result<(), Box<dyn Error + Send + Sync>> {
let mut args: Vec<&str> = Vec::new();

println!("Receiving callback: {:?}", q.data);

if let Some(data) = q.data.clone() {
if data.contains("_") {
args = data.split("_").collect();
} else {
args.push(&data);
}

println!("Parsed arguments: {:?}", args);

let _ = match args[0] {
"group" => crate::functions::groups::callback(&bot, &q, &args).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::kbmng::Keyboard};
use crate::{hooks, utils::keyboard_manager::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::kbmng::Keyboard;
use crate::utils::keyboard_manager::Keyboard;
use teloxide::{
payloads::SendMessageSetters,
prelude::*,
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::kbmng::Keyboard;
use crate::utils::keyboard_manager::Keyboard;

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

Expand Down
45 changes: 45 additions & 0 deletions src/utils/group_manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use serde::{Deserialize, Serialize};

static GROUPS: &str = include_str!("../../communities.json");

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Group {
pub name: String,
pub about: String,
pub telegram: String,
pub link: Option<String>,
}

#[derive(Debug, Clone)]
pub struct Groups {
groups: Vec<Group>,
}

impl Groups {
pub fn new() -> Self {
let json: Vec<Group> = serde_json::from_str(GROUPS).unwrap();

Self { groups: json }
}

pub fn get_groups(&self, page_number: i32, page_size: i32) -> Vec<Group> {
let start = (page_number - 1) * page_size;
let end = page_number * page_size;

println!("start: {}, end: {}", start, end);

if start < 0 || end < 0 {
return Vec::new();
}

if start as usize > self.groups.len() {
return Vec::new();
}

if end as usize > self.groups.len() {
return self.groups[start as usize..].to_vec();
}

self.groups[start as usize..end as usize].to_vec()
}
}
Empty file removed src/utils/grpmgr.rs
Empty file.
2 changes: 1 addition & 1 deletion src/utils/inmgr.rs → src/utils/inline_manager.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::kbmng::Keyboard;
use super::keyboard_manager::Keyboard;
use crates_io_api::Crate;
use teloxide::types::*;

Expand Down
2 changes: 1 addition & 1 deletion src/utils/kbmng.rs → src/utils/keyboard_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Keyboard {
}

/// Add a text callback to keyboard
pub fn text(&mut self, text: &String, callback: &String) -> InlineKeyboardMarkup {
pub fn text(&mut self, text: &str, callback: &str) -> InlineKeyboardMarkup {
self.keyboard
.last_mut()
.unwrap()
Expand Down
5 changes: 3 additions & 2 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod inmgr;
pub mod kbmng;
pub mod group_manager;
pub mod inline_manager;
pub mod keyboard_manager;

0 comments on commit 3612438

Please sign in to comment.