Skip to content

Commit

Permalink
attempt on adding group
Browse files Browse the repository at this point in the history
  • Loading branch information
orzklv committed Nov 16, 2023
1 parent 629ebf6 commit 5b034dc
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 16 deletions.
40 changes: 40 additions & 0 deletions src/functions/groups.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::utils::kbmng::Keyboard;
use teloxide::{
payloads::SendMessageSetters,
prelude::*,
types::{InlineKeyboardMarkup, ParseMode},
};

static TEXT: &str = r#"<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<()> {
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(())
}
35 changes: 19 additions & 16 deletions src/functions/inline.rs
Original file line number Diff line number Diff line change
@@ -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::*};

Expand Down Expand Up @@ -62,22 +62,25 @@ pub async fn inline(
};
}

let request: Vec<InlineQueryResult> = 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<InlineQueryResult> = 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(())
Expand Down
21 changes: 21 additions & 0 deletions src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod help;
pub mod inline;
pub mod rules;
pub mod start;
pub mod groups;

pub use inline::inline;

Expand All @@ -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<dyn Error + Send + Sync>> {
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(())
}
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ pub enum Command {

/// About the bot
About,

/// Available groups
Groups,
}

pub fn handler() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>> {
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()
Expand Down
Empty file added src/utils/grpmgr.rs
Empty file.

0 comments on commit 5b034dc

Please sign in to comment.