diff --git a/data/roadmap.md b/data/roadmap.md new file mode 100644 index 0000000..5f5325c --- /dev/null +++ b/data/roadmap.md @@ -0,0 +1,3 @@ +Salom bo'lajak Rustacean! + +Ushbu tutorialga xush kelibsiz! \ No newline at end of file diff --git a/src/functions/check.rs b/src/functions/check.rs new file mode 100644 index 0000000..5bc519f --- /dev/null +++ b/src/functions/check.rs @@ -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!("Chat ID: {}", msg.chat.id)); + + if msg.thread_id.is_some() { + message.push_str(&format!("\nThread ID: {}", msg.thread_id.unwrap())) + } + + println!("Message: {:?}", message); + + message +} diff --git a/src/functions/groups.rs b/src/functions/groups.rs index d901fd9..f1a4ea7 100644 --- a/src/functions/groups.rs +++ b/src/functions/groups.rs @@ -3,7 +3,7 @@ use crate::utils::{ keyboard::Keyboard, }; use teloxide::{ - payloads::{SendMessageSetters, EditMessageTextSetters}, + payloads::{EditMessageTextSetters, SendMessageSetters}, prelude::*, types::{InlineKeyboardMarkup, ParseMode}, }; diff --git a/src/functions/joined.rs b/src/functions/joined.rs new file mode 100644 index 0000000..3c8044f --- /dev/null +++ b/src/functions/joined.rs @@ -0,0 +1,25 @@ +use teloxide::{prelude::*, types::*}; + +static TEXT: &str = "Salom bo'lajak Rustacean!\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(()) +} diff --git a/src/functions/mod.rs b/src/functions/mod.rs index 5fbd3b9..766a3e4 100644 --- a/src/functions/mod.rs +++ b/src/functions/mod.rs @@ -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; @@ -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(()) @@ -84,7 +89,7 @@ pub async fn triggers(bot: Bot, msg: Message) -> Result<(), Box {} Err(_) => {} @@ -93,5 +98,15 @@ pub async fn triggers(bot: Bot, msg: Message) -> Result<(), Box 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") +} diff --git a/src/hooks/is_private.rs b/src/hooks/is_private.rs index 16783cc..f2f5b81 100644 --- a/src/hooks/is_private.rs +++ b/src/hooks/is_private.rs @@ -10,7 +10,7 @@ static TEXT: &str = "⚠️ Bu komanda faqat shaxsiy chat uchun!"; 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<()> { diff --git a/src/lib.rs b/src/lib.rs index fbc9464..72b5c72 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,6 +38,12 @@ pub enum Command { /// Useful resources Useful, + + /// Roadmap for newbies, + Roadmap, + + /// Check for chatid + Check, } pub fn handler() -> UpdateHandler> { diff --git a/src/main.rs b/src/main.rs index 8e705d0..9916606 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,14 +22,8 @@ async fn main() -> Result<(), Box> { .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 { @@ -40,14 +34,30 @@ async fn main() -> Result<(), Box> { "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(()) } diff --git a/src/utils/message.rs b/src/utils/message.rs new file mode 100644 index 0000000..371a600 --- /dev/null +++ b/src/utils/message.rs @@ -0,0 +1,28 @@ +use teloxide::{payloads::*, prelude::*, requests::JsonRequest, types::*}; + +trait RequestTopicFriendly { + type Err: std::error::Error + Send; + type SendMessageTF: Request; + + /// For Telegram documentation see [`SendMessage`]. + fn send_message_tf(&self, chat_id: C, text: T) -> Self::SendMessageTF + where + C: Into, + T: Into; +} + +impl RequestTopicFriendly for Bot { + type Err = teloxide::errors::RequestError; + type SendMessageTF = JsonRequest; + + fn send_message_tf(&self, chat_id: C, text: T) -> Self::SendMessageTF + where + C: Into, + T: Into, + { + Self::SendMessageTF::new( + self.clone(), + teloxide::payloads::SendMessage::new(chat_id, text), + ) + } +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 81184b8..9679e1f 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -2,4 +2,5 @@ pub mod github; pub mod groups; pub mod inlines; pub mod keyboard; +pub mod message; pub mod resources;