Update message handler

This commit is contained in:
2022-08-06 14:16:40 +03:00
parent 6d36d2c90e
commit 8cb80909d9

View File

@@ -32,6 +32,9 @@ export default class BotsManager {
if (this.syncInterval === null) { if (this.syncInterval === null) {
this.syncInterval = setInterval(() => this.sync(), 30_000); this.syncInterval = setInterval(() => this.sync(), 30_000);
} }
process.once('SIGINT', () => this.stop());
process.once('SIGTERM', () => this.stop());
} }
static async sync() { static async sync() {
@@ -76,6 +79,8 @@ export default class BotsManager {
this.bots[state.id] = bot; this.bots[state.id] = bot;
this.botsStates[state.id] = state; this.botsStates[state.id] = state;
this.restartApplication();
} }
static async _checkPendingUpdates(bot: Telegraf, state: BotState) { static async _checkPendingUpdates(bot: Telegraf, state: BotState) {
@@ -117,13 +122,11 @@ export default class BotsManager {
return false; return false;
} }
static async handleUpdate(req: Request, res: Response, next: NextFunction) { static getBotHandlers() {
const botIdStr = req.url.split("/")[1]; return Object.keys(this.bots).map((index) => {
const bot = this.bots[parseInt(botIdStr)]; const bot = this.bots[parseInt(index)];
return bot.webhookCallback(`/${index}/${bot.telegram.token}`);
if (bot === undefined) return; });
await bot.webhookCallback(`/${botIdStr}/${bot.telegram.token}`)(req, res);
} }
private static async launch() { private static async launch() {
@@ -139,13 +142,12 @@ export default class BotsManager {
}); });
}); });
application.use((req: Request, res: Response, next: NextFunction) => this.handleUpdate(req, res, next)); const handlers = this.getBotHandlers();
if (handlers.length !== 0) application.use(handlers);
this.server = application.listen(env.WEBHOOK_PORT); this.server = application.listen(env.WEBHOOK_PORT);
console.log("Server started!");
process.once('SIGINT', () => this.stop()); console.log("Server started!");
process.once('SIGTERM', () => this.stop());
} }
static stop() { static stop() {
@@ -158,5 +160,16 @@ export default class BotsManager {
this.server?.close(); this.server?.close();
this.server = null; this.server = null;
console.log("Server stopped!")
}
static restartApplication() {
this.server?.close();
this.server = null;
this.launch();
console.log("Server restarted!");
} }
} }