Skip to content

Commit

Permalink
adding version & latest version checker
Browse files Browse the repository at this point in the history
  • Loading branch information
orzklv committed Nov 17, 2023
1 parent 54fd78c commit 4b0f62b
Show file tree
Hide file tree
Showing 14 changed files with 712 additions and 134 deletions.
450 changes: 450 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ 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"] }
octocrab = { version = "0.32.0" }
28 changes: 0 additions & 28 deletions delta/latest.ts

This file was deleted.

4 changes: 0 additions & 4 deletions delta/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ import { Bot } from "../deps.ts";
import channel from "./channel.ts";
import trigger from "./trigger.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(useful)
.use(latest)
.use(version)
.use(trigger)
.use(channel);
};
91 changes: 0 additions & 91 deletions delta/version.ts

This file was deleted.

18 changes: 11 additions & 7 deletions src/functions/groups.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::utils::{group_manager::{Groups, Group}, keyboard_manager::Keyboard};
use crate::utils::{
group_manager::{Group, Groups},
keyboard_manager::Keyboard,
};
use teloxide::{
payloads::SendMessageSetters,
prelude::*,
Expand Down Expand Up @@ -44,7 +47,7 @@ pub async fn callback_detail(bot: &Bot, q: &CallbackQuery, args: &Vec<&str>) ->
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))
.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 @@ -58,11 +61,12 @@ pub async fn callback_detail(bot: &Bot, q: &CallbackQuery, args: &Vec<&str>) ->
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()
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(),
}
}

Expand Down Expand Up @@ -97,7 +101,7 @@ pub fn keyboard_detail(page: i32, data: &Option<Group>) -> InlineKeyboardMarkup

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());
}
Expand Down
38 changes: 38 additions & 0 deletions src/functions/latest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::utils::{github::GitHub, keyboard_manager::Keyboard};
use octocrab::models::repos::Release;
use teloxide::{
payloads::SendMessageSetters,
prelude::*,
types::{InlineKeyboardMarkup, ParseMode},
};

pub async fn command(bot: &Bot, github: GitHub, msg: &Message) -> ResponseResult<()> {
let latest = github.get_latest().await.unwrap();

bot.send_message(msg.chat.id, view(&latest))
.parse_mode(ParseMode::Html)
.reply_markup(keyboard(&latest))
.await?;

Ok(())
}

pub fn view(release: &Release) -> String {
format!(
"<b>Hozirgi eng oxirgi versiya bu <a href=\"{}\">\
{}</a> va ushbu reliz </b> <code>{}</code> da e'lon qilingan <a href=\"{}\">\
{}</a> tomonidan.\
\n\n\
",
format!("https://releases.rs/docs/{}", release.tag_name),
release.tag_name,
release.published_at.unwrap().date_naive(),
release.author.html_url,
release.author.login,
)
}

pub fn keyboard(release: &Release) -> InlineKeyboardMarkup {
let mut keyboard = Keyboard::new();
keyboard.url("Ko'proq ma'lumotlar", release.html_url.as_str())
}
14 changes: 13 additions & 1 deletion src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ pub mod about;
pub mod groups;
pub mod help;
pub mod inline;
pub mod latest;
pub mod rules;
pub mod start;
pub mod version;

pub use inline::inline;

use crate::utils::github::GitHub;
use crate::Command;
use std::error::Error;
use teloxide::prelude::*;
Expand All @@ -16,19 +19,26 @@ pub async fn commands(
_me: teloxide::types::Me,
msg: Message,
cmd: Command,
github: GitHub,
) -> Result<(), Box<dyn Error + Send + Sync>> {
let _ = match cmd {
Command::Start => crate::functions::start::command(&bot, &msg).await,
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,
Command::Latest => crate::functions::latest::command(&bot, github, &msg).await,
Command::Version => crate::functions::version::command(&bot, github, &msg).await,
};

Ok(())
}

pub async fn callback(bot: Bot, q: CallbackQuery) -> Result<(), Box<dyn Error + Send + Sync>> {
pub async fn callback(
bot: Bot,
q: CallbackQuery,
github: GitHub,
) -> Result<(), Box<dyn Error + Send + Sync>> {
let mut args: Vec<&str> = Vec::new();

if let Some(data) = q.data.clone() {
Expand All @@ -41,6 +51,8 @@ pub async fn callback(bot: Bot, q: CallbackQuery) -> Result<(), Box<dyn Error +
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,
"version" => crate::functions::version::callback_list(&bot, &q, &args, github).await,
"changelog" => crate::functions::version::callback_detail(&bot, &q, &args, github).await,
_ => Ok(()),
};
}
Expand Down
118 changes: 118 additions & 0 deletions src/functions/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
use crate::utils::{github::GitHub, keyboard_manager::Keyboard};
use octocrab::models::repos::Release;
use teloxide::{
payloads::SendMessageSetters,
prelude::*,
types::{InlineKeyboardMarkup, ParseMode},
};

static TEXT: &str = "<b>Rust Dasturlash tili versiyalari:</b>";

pub async fn command(bot: &Bot, github: GitHub, msg: &Message) -> ResponseResult<()> {
let versions = github.get_list(1).await.unwrap();
let next_page = github.get_list(2).await.unwrap();

bot.send_message(msg.chat.id, TEXT)
.parse_mode(ParseMode::Html)
.reply_markup(keyboard_list(1, versions, Some(next_page)))
.await?;

Ok(())
}

pub async fn callback_list(
bot: &Bot,
q: &CallbackQuery,
args: &Vec<&str>,
github: GitHub,
) -> ResponseResult<()> {
let page = args[0].parse::<u32>().unwrap();
let versions: Vec<Release> = github.get_list(page).await.unwrap();
let next_page = github.get_list(page + 1).await.unwrap();

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_list(page, versions, Some(next_page)))
.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>, github: GitHub) -> ResponseResult<()> {
let page = args[0].parse::<u32>().unwrap();
let version: Release = github.get_detail(args[1]).await.unwrap();

if !args.is_empty() {
if let Some(Message { id, chat, .. }) = q.message.clone() {
bot.edit_message_text(chat.id, id, view_detail(&version))
.parse_mode(ParseMode::Html)
.reply_markup(keyboard_detail(page, version))
.await?;
} else if let Some(id) = q.inline_message_id.clone() {
bot.edit_message_text_inline(id, "Oopsie, something went wrong...")
.await?;
}
}

Ok(())
}

pub fn view_detail(release: &Release) -> String {
format!(
"<b><a href=\"{}\">{}</a></b>\n\n\
<b>Yaratildi:</b> {}\n\
<b>E'lon qilindi:</b> {}\n\
<b>O'rnatish:</b> <code>rustup install {}</code>\n\n\
<b>\"Instant view\" yoki quyidagi tugma orqali ko'proq ma'lumot oling:</b>",
release.html_url,
release.name.clone().unwrap(),
release.created_at.unwrap().format("%d.%m.%Y"),
release.published_at.unwrap().format("%d.%m.%Y"),
release.tag_name
)
}

pub fn keyboard_list(
page: u32,
releases: Vec<Release>,
next_page: Option<Vec<Release>>,
) -> InlineKeyboardMarkup {
let mut keyboard = Keyboard::new();

for release in releases {
keyboard.text(&release.tag_name, &format!("changelog_{}_{}", page, release.tag_name));
keyboard.row();
}

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

if next_page.is_some() && !next_page.unwrap().is_empty() {
keyboard.text("Keyingi ➡️", &format!("version_{}", page + 1));
}

keyboard.get()
}

pub fn keyboard_detail(
page: u32,
release: Release,
) -> InlineKeyboardMarkup {
let mut keyboard = Keyboard::new();

keyboard.url("📝 GitHub da o'qish", release.html_url.as_str());
keyboard.row();
keyboard.text("🔙 Orqaga", &format!("version_{}", page));

keyboard.get()
}

Loading

0 comments on commit 4b0f62b

Please sign in to comment.