UNPKG

@wasserstoff/mangi-tg-bot

Version:

A powerful Telegram Bot SDK with built-in authentication, session management, and database integration

97 lines 3.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.logger = exports.TelegramManager = exports.RedisManager = exports.Bot = void 0; const node_graceful_shutdown_1 = require("node-graceful-shutdown"); const logger_1 = require("./logger"); Object.defineProperty(exports, "logger", { enumerable: true, get: function () { return logger_1.logger; } }); const BotManager_1 = require("./bot/BotManager"); const RedisManager_1 = require("./database/RedisManager"); const telegram_1 = require("telegram"); class Bot { botManager; redisManager; config; constructor(config) { this.config = config; this.redisManager = RedisManager_1.RedisManager.getInstance(); } /** * Initialize the bot and connect to all required services */ async initialize() { try { // Connect to Redis first await this.redisManager.connect(this.config.redisUrl); // Now create BotManager with the connected Redis client this.botManager = new BotManager_1.BotManager(this.config.botToken, this.redisManager.getClient(), this.config); // Start bot await this.botManager.start(); // Setup graceful shutdown // this.setupShutdown(); logger_1.logger.info("Bot initialized successfully"); } catch (error) { logger_1.logger.error("Error initializing Bot:", error); throw error; } } /** * Get the Redis client instance */ getRedisClient() { return this.redisManager.getClient(); } setupShutdown() { (0, node_graceful_shutdown_1.onShutdown)(async () => { logger_1.logger.info("Initiating graceful shutdown..."); try { // Stop bot await this.botManager?.stop(); // Disconnect from Redis await this.redisManager.disconnect(); logger_1.logger.info("Shutdown completed successfully"); } catch (error) { logger_1.logger.error("Error during shutdown:", error); process.exit(1); } }); } MangiClient(apiId, apiHash, stringSession) { const client = new telegram_1.TelegramClient(stringSession ?? "", apiId, apiHash, { connectionRetries: 5, }); return client; } /** * Cleanup and disconnect from all services */ async cleanup() { try { // Stop bot await this.botManager?.stop(); // Disconnect from Redis await this.redisManager.disconnect(); logger_1.logger.info("Bot cleanup completed successfully"); } catch (error) { logger_1.logger.error("Error during cleanup:", error); throw error; } } /** * Get the bot manager instance */ getBotManager() { if (!this.botManager) { throw new Error("BotManager not initialized. Call initialize() first."); } return this.botManager; } } exports.Bot = Bot; var RedisManager_2 = require("./database/RedisManager"); Object.defineProperty(exports, "RedisManager", { enumerable: true, get: function () { return RedisManager_2.RedisManager; } }); var BotClient_1 = require("./bot/BotClient"); Object.defineProperty(exports, "TelegramManager", { enumerable: true, get: function () { return BotClient_1.TelegramManager; } }); //# sourceMappingURL=index.js.map