Made subs works
All checks were successful
Rust Docker Build / docker (push) Successful in 1h11m56s

This commit is contained in:
AB from home.homenet
2025-10-19 15:23:17 +03:00
parent dae787657c
commit c6892b1a73
11 changed files with 87 additions and 49 deletions

View File

@@ -2,12 +2,14 @@ use teloxide::{Bot, prelude::*};
use tokio::sync::oneshot;
use crate::database::DatabaseManager;
use crate::config::AppConfig;
use super::handlers::{self, Command};
/// Run the bot polling loop
pub async fn run_polling(
bot: Bot,
db: DatabaseManager,
app_config: AppConfig,
mut shutdown_rx: oneshot::Receiver<()>,
) {
tracing::info!("Starting Telegram bot polling...");
@@ -30,7 +32,7 @@ pub async fn run_polling(
);
let mut dispatcher = Dispatcher::builder(bot.clone(), handler)
.dependencies(dptree::deps![db])
.dependencies(dptree::deps![db, app_config])
.enable_ctrlc_handler()
.build();

View File

@@ -9,6 +9,7 @@ pub use types::*;
use teloxide::{prelude::*, types::CallbackQuery};
use crate::database::DatabaseManager;
use crate::config::AppConfig;
/// Handle bot commands
pub async fn handle_command(
@@ -16,6 +17,7 @@ pub async fn handle_command(
msg: Message,
cmd: Command,
db: DatabaseManager,
app_config: AppConfig,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let chat_id = msg.chat.id;
let from = &msg.from.ok_or("No user info")?;
@@ -78,6 +80,7 @@ pub async fn handle_message(
bot: Bot,
msg: Message,
db: DatabaseManager,
_app_config: AppConfig,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let chat_id = msg.chat.id;
let from = msg.from.as_ref().ok_or("No user info")?;
@@ -95,6 +98,7 @@ pub async fn handle_callback_query(
bot: Bot,
q: CallbackQuery,
db: DatabaseManager,
app_config: AppConfig,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
if let Some(data) = &q.data {
if let Some(callback_data) = CallbackData::parse(data) {
@@ -106,7 +110,7 @@ pub async fn handle_callback_query(
handle_my_configs_edit(bot, &q, &db).await?;
}
CallbackData::SubscriptionLink => {
handle_subscription_link(bot, &q, &db).await?;
handle_subscription_link(bot, &q, &db, &app_config).await?;
}
CallbackData::Support => {
handle_support(bot, &q).await?;

View File

@@ -673,6 +673,7 @@ pub async fn handle_subscription_link(
bot: Bot,
q: &CallbackQuery,
db: &DatabaseManager,
app_config: &crate::config::AppConfig,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let from = q.from.clone();
let telegram_id = from.id.0 as i64;
@@ -683,8 +684,7 @@ pub async fn handle_subscription_link(
let user_repo = UserRepository::new(db.connection());
if let Ok(Some(user)) = user_repo.get_by_telegram_id(telegram_id).await {
// Generate subscription URL
let base_url = std::env::var("BASE_URL").unwrap_or_else(|_| "http://localhost:8080".to_string());
let subscription_url = format!("{}/sub/{}", base_url, user.id);
let subscription_url = format!("{}/sub/{}", app_config.web.base_url, user.id);
let message = match lang {
Language::Russian => {

View File

@@ -7,6 +7,7 @@ use uuid::Uuid;
use crate::database::DatabaseManager;
use crate::database::repository::TelegramConfigRepository;
use crate::database::entities::telegram_config::Model as TelegramConfig;
use crate::config::AppConfig;
pub mod bot;
pub mod handlers;
@@ -18,6 +19,7 @@ pub use error::TelegramError;
/// Main Telegram service that manages the bot lifecycle
pub struct TelegramService {
db: DatabaseManager,
app_config: AppConfig,
bot: Arc<RwLock<Option<Bot>>>,
config: Arc<RwLock<Option<TelegramConfig>>>,
shutdown_signal: Arc<RwLock<Option<tokio::sync::oneshot::Sender<()>>>>,
@@ -25,9 +27,10 @@ pub struct TelegramService {
impl TelegramService {
/// Create a new Telegram service
pub fn new(db: DatabaseManager) -> Self {
pub fn new(db: DatabaseManager, app_config: AppConfig) -> Self {
Self {
db,
app_config,
bot: Arc::new(RwLock::new(None)),
config: Arc::new(RwLock::new(None)),
shutdown_signal: Arc::new(RwLock::new(None)),
@@ -83,10 +86,11 @@ impl TelegramService {
*self.shutdown_signal.write().await = Some(tx);
let db = self.db.clone();
let app_config = self.app_config.clone();
// Spawn polling task
tokio::spawn(async move {
bot::run_polling(bot, db, rx).await;
bot::run_polling(bot, db, app_config, rx).await;
});
Ok(())