const TelegramBot = require('node-telegram-bot-api'); const config = require('../config'); const queueManager = require('../queue/queueManager'); const AnnouncementManager = require('../utils/announcementManager'); const CommandRegistry = require('./telegrambot/commandRegistry'); class StagehandBot { constructor() { this.bot = new TelegramBot(config.botToken, { polling: true }); this.serviceName = 'telegram'; this.channelId = config.channelId; this.announcements = new AnnouncementManager(this); this.commandRegistry = new CommandRegistry(this.bot, this.announcements); this.init(); } async init() { await this.announcements.init(); this.registerCommands(); console.log('Telegram bot started...'); } registerCommands() { // Use the command registry to register all commands and handlers this.commandRegistry.registerAll(); } /** * Post media (image or video) to the Telegram channel * This method is used by the scheduler and external services * @param {Object} mediaData - The media data to post * @returns {Promise} - Whether posting was successful */ async postMedia(mediaData) { return await this.commandRegistry.getMediaHelper().postMedia(mediaData); } /** * Check if a user is authorized * @param {number} userId - The user ID to check * @returns {boolean} - Whether the user is authorized */ isAuthorized(userId) { return this.commandRegistry.getAuthHelper().isAuthorized(userId); } /** * Check if the user is the owner of the bot * @param {number} userId - The Telegram user ID to check * @returns {boolean} - Whether the user is the owner */ isOwner(userId) { return this.commandRegistry.getAuthHelper().isOwner(userId); } /** * Display a page of the queue with interactive buttons * @param {number} chatId - Telegram chat ID * @param {number} page - Page number to display (1-based) * @param {number} pageSize - Number of items per page */ async displayQueuePage(chatId, page, pageSize) { return await this.commandRegistry.getQueueHelper().displayQueuePage(chatId, page, pageSize); } /** * Shutdown the bot gracefully * @returns {Promise} */ async shutdown() { try { console.log('Stopping Telegram bot polling...'); await this.bot.stopPolling(); console.log('Telegram bot polling stopped'); return true; } catch (error) { console.error('Error shutting down bot:', error); return false; } } } module.exports = StagehandBot;