From 5b034dc220ed765c5eec158a57e65ae3f263166a Mon Sep 17 00:00:00 2001 From: Sokhibjon Orzikulov Date: Fri, 17 Nov 2023 04:47:37 +0500 Subject: [PATCH] attempt on adding group --- src/functions/groups.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/functions/inline.rs | 35 +++++++++++++++++++---------------- src/functions/mod.rs | 21 +++++++++++++++++++++ src/lib.rs | 5 +++++ src/utils/grpmgr.rs | 0 5 files changed, 85 insertions(+), 16 deletions(-) create mode 100644 src/functions/groups.rs create mode 100644 src/utils/grpmgr.rs diff --git a/src/functions/groups.rs b/src/functions/groups.rs new file mode 100644 index 0000000..bcc8969 --- /dev/null +++ b/src/functions/groups.rs @@ -0,0 +1,40 @@ +use crate::utils::kbmng::Keyboard; +use teloxide::{ + payloads::SendMessageSetters, + prelude::*, + types::{InlineKeyboardMarkup, ParseMode}, +}; + +static TEXT: &str = r#"Telegramdagi Rust Hamjamiyatlari yoki Guruhlari:\nAgar o'zingizni guruhingizni qo'shmoqchi bo'lsangiz, bizni community.json 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<()> { + bot.send_message(msg.chat.id, TEXT) + .parse_mode(ParseMode::Html) + .reply_markup(keyboard()) + .await?; + + Ok(()) +} + +pub async fn callback(bot: &Bot, q: &CallbackQuery, args: &Vec<&str>) -> ResponseResult<()> { + 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()) + .await?; + } else if let Some(id) = q.inline_message_id.clone() { + bot.edit_message_text_inline(id, "Oopsie, something went wrong...").await?; + } + } + + + + Ok(()) +} \ No newline at end of file diff --git a/src/functions/inline.rs b/src/functions/inline.rs index 433d3e9..ca4b39f 100644 --- a/src/functions/inline.rs +++ b/src/functions/inline.rs @@ -1,4 +1,4 @@ -use crates_io_api::{AsyncClient, CratesQuery, Crate}; +use crates_io_api::{AsyncClient, Crate, CratesQuery}; use std::error::Error; use teloxide::{prelude::*, types::*}; @@ -62,22 +62,25 @@ pub async fn inline( }; } - let request: Vec = request.iter().map(|c: &Crate| { - InlineQueryResult::Article( - InlineQueryResultArticle::new( - uuid::Uuid::new_v4(), - c.name.clone(), - InputMessageContent::Text( - InputMessageContentText::new(view_generate(c)) - .parse_mode(ParseMode::Html) - .disable_web_page_preview(true), - ), + let request: Vec = request + .iter() + .map(|c: &Crate| { + InlineQueryResult::Article( + InlineQueryResultArticle::new( + uuid::Uuid::new_v4(), + c.name.clone(), + InputMessageContent::Text( + InputMessageContentText::new(view_generate(c)) + .parse_mode(ParseMode::Html) + .disable_web_page_preview(true), + ), + ) + .description(c.description.clone().unwrap()) + .url(url::Url::parse(&format!("https://crates.io/crates/{}", c.id)).unwrap()) + .reply_markup(kb_generate(c)), ) - .description(c.description.clone().unwrap()) - .url(url::Url::parse(&format!("https://crates.io/crates/{}", c.id)).unwrap()) - .reply_markup(kb_generate(c)), - ) - }).collect(); + }) + .collect(); bot.answer_inline_query(q.id, request).send().await?; Ok(()) diff --git a/src/functions/mod.rs b/src/functions/mod.rs index 30a7650..98fe7f6 100644 --- a/src/functions/mod.rs +++ b/src/functions/mod.rs @@ -3,6 +3,7 @@ pub mod help; pub mod inline; pub mod rules; pub mod start; +pub mod groups; pub use inline::inline; @@ -21,7 +22,27 @@ 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).await, }; Ok(()) } + +pub async fn callback(bot: Bot, q: CallbackQuery) -> Result<(), Box> { + let mut args: Vec<&str> = Vec::new(); + + if let Some(data) = q.data.clone() { + if data.contains("_") { + args = data.split("_").collect(); + } else { + args.push(&data); + } + + let _ = match args[0] { + "group" => crate::functions::groups::callback(&bot, &q, &args).await, + _ => Ok(()) + }; + } + + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs index d5f1ff8..d690ac6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,12 +19,17 @@ pub enum Command { /// About the bot About, + + /// Available groups + Groups, } pub fn handler() -> UpdateHandler> { dptree::entry() // Inline Queries .branch(Update::filter_inline_query().endpoint(functions::inline)) + // Callbacks + .branch(Update::filter_callback_query().endpoint(functions::callback)) // Commands .branch( Update::filter_message() diff --git a/src/utils/grpmgr.rs b/src/utils/grpmgr.rs new file mode 100644 index 0000000..e69de29