Skip to content

Commit

Permalink
implemented group feature
Browse files Browse the repository at this point in the history
  • Loading branch information
orzklv committed Nov 17, 2023
1 parent 3612438 commit 54fd78c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 126 deletions.
99 changes: 0 additions & 99 deletions delta/groups.ts

This file was deleted.

2 changes: 0 additions & 2 deletions delta/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import which from "./which.ts";
import { Bot } from "../deps.ts";
import channel from "./channel.ts";
import trigger from "./trigger.ts";
import groups from "./groups.ts";
import useful from "./useful.ts";
import latest from "./latest.ts";
import version from "./version.ts";

export default async (bot: Bot) => {
await bot
.use(which)
.use(groups)
.use(useful)
.use(latest)
.use(version)
Expand Down
74 changes: 59 additions & 15 deletions src/functions/groups.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{group_manager::Groups, keyboard_manager::Keyboard};
use crate::utils::{group_manager::{Groups, Group}, keyboard_manager::Keyboard};
use teloxide::{
payloads::SendMessageSetters,
prelude::*,
Expand All @@ -12,26 +12,39 @@ pub async fn command(bot: &Bot, msg: &Message) -> ResponseResult<()> {

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

Ok(())
}

pub async fn callback(bot: &Bot, q: &CallbackQuery, args: &Vec<&str>) -> ResponseResult<()> {
pub async fn callback_list(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 !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(&groups, page))
.reply_markup(keyboard_list(&groups, args[0].parse().unwrap_or(1)))
.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_detail(bot: &Bot, q: &CallbackQuery, args: &Vec<&str>) -> ResponseResult<()> {
let groups: Groups = Groups::new();
let find = groups.find_group(args[1..].join("_").to_string());

if !args.is_empty() {
if let Some(Message { id, chat, .. }) = q.message.clone() {
bot.edit_message_text(chat.id, id, view_detail(&find))
.parse_mode(ParseMode::Html)
.reply_markup(keyboard_detail( args[0].parse().unwrap_or(1), &find))
.await?;
} else if let Some(id) = q.inline_message_id.clone() {
bot.edit_message_text_inline(id, "Oopsie, something went wrong...")
Expand All @@ -42,7 +55,18 @@ pub async fn callback(bot: &Bot, q: &CallbackQuery, args: &Vec<&str>) -> Respons
Ok(())
}

pub fn keyboard(groups: &Groups, page: i32) -> InlineKeyboardMarkup {
pub fn view_detail(data: &Option<Group>) -> String {
match data {
Some(d) => {
format!("<b>{}</b>\n\n<i>{}</i>\n\n<b>Use the following buttons to get to the links:</b>", d.name, d.about)
},
None => {
"<b>Ushbu guruh mavjud emas!</b>".to_string()
}
}
}

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

for group in groups.get_groups(page, 5) {
Expand All @@ -58,11 +82,31 @@ pub fn keyboard(groups: &Groups, page: i32) -> InlineKeyboardMarkup {
}

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

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

keyboard.get()
}

pub fn keyboard_detail(page: i32, data: &Option<Group>) -> InlineKeyboardMarkup {
let mut keyboard = Keyboard::new();

if let Some(group) = data {
keyboard.url("Telegram", &format!("https://t.me/{}", group.telegram));

if group.link.is_some() {
keyboard.url("Web", &group.link.clone().unwrap());
}

keyboard.row();

keyboard.text("🔙 Orqaga", &format!("group_{}", page));
} else {
keyboard.text("🔙 Orqaga", &format!("group_{}", page));
}

keyboard.get()
Expand Down
13 changes: 5 additions & 8 deletions src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,16 @@ 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();
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,
let _ = match args.remove(0) {
"group" => crate::functions::groups::callback_list(&bot, &q, &args).await,
"detail" => crate::functions::groups::callback_detail(&bot, &q, &args).await,
_ => Ok(()),
};
}
Expand Down
12 changes: 10 additions & 2 deletions src/utils/group_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ impl Groups {
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();
}
Expand All @@ -42,4 +40,14 @@ impl Groups {

self.groups[start as usize..end as usize].to_vec()
}

pub fn find_group(&self, query: String) -> Option<Group> {
let search: Vec<&Group> = self.groups.iter().filter(|group| group.telegram[1..] == query).collect();

if search.is_empty() {
return None;
}

Some(search[0].clone())
}
}

0 comments on commit 54fd78c

Please sign in to comment.