- Added Redis service to both docker-compose files for local development and production environments. - Updated bot configuration to utilize the Grammy framework, replacing Telegraf. - Implemented graceful shutdown for the bot, ensuring proper resource management. - Refactored bot commands and removed deprecated message handling logic. - Enhanced environment variable management for Redis connection settings. - Updated dependencies in package.json to include new Grammy-related packages.
39 lines
1017 B
TypeScript
39 lines
1017 B
TypeScript
import { createBot } from './bot';
|
|
import { env as environment } from './config/env';
|
|
import { logger } from './utils/logger';
|
|
import { getRedisInstance } from './utils/redis';
|
|
import { run } from '@grammyjs/runner';
|
|
|
|
const bot = createBot({
|
|
token: environment.BOT_TOKEN,
|
|
});
|
|
|
|
const runner = run(bot);
|
|
|
|
const redis = getRedisInstance();
|
|
|
|
// Graceful shutdown function
|
|
async function gracefulShutdown(signal: string) {
|
|
logger.info(`Received ${signal}, starting graceful shutdown...`);
|
|
|
|
try {
|
|
// Stop the bot
|
|
await runner.stop();
|
|
logger.info('Bot stopped');
|
|
|
|
// Disconnect Redis
|
|
redis.disconnect();
|
|
logger.info('Redis disconnected');
|
|
} catch (error) {
|
|
const err_ = error as Error;
|
|
logger.error('Error during graceful shutdown:' + err_.message || '');
|
|
}
|
|
}
|
|
|
|
// Stopping the bot when the Node.js process
|
|
// is about to be terminated
|
|
process.once('SIGINT', () => gracefulShutdown('SIGINT'));
|
|
process.once('SIGTERM', () => gracefulShutdown('SIGTERM'));
|
|
|
|
logger.info('Bot started');
|