Skip to content

Commit

Permalink
add join trigger + roadmap commadn
Browse files Browse the repository at this point in the history
  • Loading branch information
orzklv committed Nov 21, 2023
1 parent 8b9f251 commit 930f153
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 20 deletions.
3 changes: 3 additions & 0 deletions data/roadmap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<b>Salom bo'lajak Rustacean!</b>

Ushbu tutorialga xush kelibsiz!
25 changes: 25 additions & 0 deletions src/functions/check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use teloxide::{payloads::SendMessageSetters, prelude::*, types::ParseMode};

pub async fn command(bot: &Bot, msg: &Message) -> ResponseResult<()> {
println!("Command triggered: {:?}", msg);
bot.send_message(msg.chat.id, view(msg))
.parse_mode(ParseMode::Html)
// .reply_markup(keyboard())
.await?;

Ok(())
}

pub fn view(msg: &Message) -> String {
let mut message: String = String::new();

message.push_str(&format!("<b>Chat ID:</b> {}", msg.chat.id));

if msg.thread_id.is_some() {
message.push_str(&format!("\n<b>Thread ID:</b> {}", msg.thread_id.unwrap()))
}

println!("Message: {:?}", message);

message
}
2 changes: 1 addition & 1 deletion src/functions/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::utils::{
keyboard::Keyboard,
};
use teloxide::{
payloads::{SendMessageSetters, EditMessageTextSetters},
payloads::{EditMessageTextSetters, SendMessageSetters},
prelude::*,
types::{InlineKeyboardMarkup, ParseMode},
};
Expand Down
25 changes: 25 additions & 0 deletions src/functions/joined.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use teloxide::{prelude::*, types::*};

static TEXT: &str = "<b>Salom bo'lajak Rustacean!</b>\n\n\
Sizlarni bu guruhda ko'rib turganimizdan mamnunmiz. Bu guruh Rust dasturlash tiliga qaratilgan hisoblanib, \
bu yerda ushbu til haqida gaplashish, savollar berish yoki o'z fikrlaringiz bilan bo'lishishingiz mumkin. \
Hamda, agar siz ushbu dasturlash tilida butunlay yangi bo'lsangiz, /roadmap yordamida kerakli boshlang'ich \
maslahatlar, va hamda /useful yordamoda foydali resurslar olishingiz mumkin.
";

pub async fn trigger(bot: &Bot, msg: &Message) -> ResponseResult<()> {
let message = bot
.send_message(msg.chat.id, TEXT)
.parse_mode(ParseMode::Html);

if msg.thread_id.is_some() {
message
.message_thread_id(msg.thread_id.unwrap())
.send()
.await?;
} else {
message.send().await?;
}

Ok(())
}
17 changes: 16 additions & 1 deletion src/functions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#![allow(clippy::single_match)]

pub mod about;
pub mod check;
pub mod groups;
pub mod help;
pub mod inline;
pub mod joined;
pub mod latest;
pub mod offtop;
pub mod roadmap;
pub mod rules;
pub mod start;
pub mod useful;
Expand Down Expand Up @@ -37,6 +40,8 @@ pub async fn commands(
Command::Version => crate::functions::version::command(&bot, github, &msg).await,
Command::Off => crate::functions::offtop::command(&bot, &msg, &me).await,
Command::Useful => crate::functions::useful::command(&bot, &msg, &resources).await,
Command::Roadmap => crate::functions::roadmap::command(&bot, &msg).await,
Command::Check => crate::functions::check::command(&bot, &msg).await,
};

Ok(())
Expand Down Expand Up @@ -84,7 +89,7 @@ pub async fn triggers(bot: Bot, msg: Message) -> Result<(), Box<dyn Error + Send
if let Some(user) = msg.from() {
if let Some(username) = user.username.clone() {
if username == "Channel_Bot" {
// try to delete message and ignore error
// Try to delete message and ignore error
match bot.delete_message(msg.chat.id, msg.id).await {
Ok(_) => {}
Err(_) => {}
Expand All @@ -93,5 +98,15 @@ pub async fn triggers(bot: Bot, msg: Message) -> Result<(), Box<dyn Error + Send
}
}

if let Some(new_chat_members) = msg.new_chat_members() {
let bot_id = bot.get_me().send().await?.id;

if !new_chat_members.iter().any(|user| user.id == bot_id)
&& (msg.chat.is_supergroup() || msg.chat.is_group())
{
crate::functions::joined::trigger(&bot, &msg).await?;
}
}

Ok(())
}
2 changes: 1 addition & 1 deletion src/functions/offtop.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use teloxide::{prelude::*, types::*};
use crate::utils::keyboard::Keyboard;
use teloxide::{prelude::*, types::*};

static TEXT_FAIL: &str = "Ha-ha... yaxshi urinish!";
static TEXT_NON_REPLY: &str = "↪ Reply bilan ko'rsatingchi habarni!";
Expand Down
31 changes: 31 additions & 0 deletions src/functions/roadmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::hooks;
use crate::utils::keyboard::Keyboard;
use teloxide::{
payloads::SendMessageSetters,
prelude::*,
types::{InlineKeyboardMarkup, ParseMode},
};

static ROADMAP: &str = include_str!("../../data/roadmap.md");

pub async fn command(bot: &Bot, msg: &Message) -> ResponseResult<()> {
if !msg.chat.is_private() {
return {
hooks::is_private(bot, msg).await.unwrap();
Ok(())
};
}

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

Ok(())
}

pub fn keyboard() -> InlineKeyboardMarkup {
let mut keyboard = Keyboard::new();
keyboard.url("Jamiyat", "https://t.me/rustlanguz");
keyboard.url("Web Sahifa", "https://rust-lang.uz")
}
2 changes: 1 addition & 1 deletion src/hooks/is_private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ static TEXT: &str = "⚠️ <b>Bu komanda faqat shaxsiy chat uchun!</b>";

pub fn keyboard() -> InlineKeyboardMarkup {
let mut keyboard: Keyboard = Keyboard::new();
keyboard.url("Shaxsiy Chat", "https://t.me/rusttest_stage_bot")
keyboard.url("Shaxsiy Chat", "https://t.me/rustinabot")
}

pub async fn is_private(bot: &Bot, msg: &Message) -> ResponseResult<()> {
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ pub enum Command {

/// Useful resources
Useful,

/// Roadmap for newbies,
Roadmap,

/// Check for chatid
Check,
}

pub fn handler() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>> {
Expand Down
42 changes: 26 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
.unwrap();
let resources = Resources::new();

// Webhook
let addr = ([0, 0, 0, 0], 8443).into();
let url = std::env::var("WEBHOOK_URL").unwrap().parse().unwrap();
let listener = webhooks::axum(bot.clone(), webhooks::Options::new(addr, url))
.await
.expect("Couldn't setup webhook");

Dispatcher::builder(bot, handler())
// Dispatcher flow control
let mut dispatcher = Dispatcher::builder(bot.clone(), handler())
.dependencies(dptree::deps![crates_client, github, groups, resources])
// If no handler succeeded to handle an update, this closure will be called
.default_handler(|upd| async move {
Expand All @@ -40,14 +34,30 @@ async fn main() -> Result<(), Box<dyn Error>> {
"An error has occurred in the dispatcher",
))
.enable_ctrlc_handler()
.build()
// .dispatch()
// .await;
.dispatch_with_listener(
listener,
LoggingErrorHandler::with_custom_text("An error has occurred in the dispatcher"),
)
.await;
.build();

match std::env::var("WEBHOOK_URL") {
Ok(v) => {
println!("Starting webhook on {}", v);
let addr = ([0, 0, 0, 0], 8443).into(); // port 8443
let listener = webhooks::axum(bot, webhooks::Options::new(addr, v.parse().unwrap()))
.await
.expect("Couldn't setup webhook");

dispatcher
.dispatch_with_listener(
listener,
LoggingErrorHandler::with_custom_text(
"An error has occurred in the dispatcher",
),
)
.await;
}
Err(_) => {
println!("Starting polling...");
dispatcher.dispatch().await;
}
}

Ok(())
}
28 changes: 28 additions & 0 deletions src/utils/message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use teloxide::{payloads::*, prelude::*, requests::JsonRequest, types::*};

trait RequestTopicFriendly {
type Err: std::error::Error + Send;
type SendMessageTF: Request<Payload = SendMessage, Err = Self::Err>;

/// For Telegram documentation see [`SendMessage`].
fn send_message_tf<C, T>(&self, chat_id: C, text: T) -> Self::SendMessageTF
where
C: Into<Recipient>,
T: Into<String>;
}

impl RequestTopicFriendly for Bot {
type Err = teloxide::errors::RequestError;
type SendMessageTF = JsonRequest<teloxide::payloads::SendMessage>;

fn send_message_tf<C, T>(&self, chat_id: C, text: T) -> Self::SendMessageTF
where
C: Into<Recipient>,
T: Into<String>,
{
Self::SendMessageTF::new(
self.clone(),
teloxide::payloads::SendMessage::new(chat_id, text),
)
}
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pub mod github;
pub mod groups;
pub mod inlines;
pub mod keyboard;
pub mod message;
pub mod resources;

0 comments on commit 930f153

Please sign in to comment.