diff --git a/src/index.ts b/src/index.ts index a290c3d..14d8aaf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -218,6 +218,23 @@ if (Environment.OPENAI_API_KEY) { export const photoDir = path.join(Environment.DATA_PATH, "photo"); export const videoDir = path.join(Environment.DATA_PATH, "video"); +let isShuttingDown = false; + +async function shutdown(signal: NodeJS.Signals) { + if (isShuttingDown) return; + isShuttingDown = true; + + console.log(`Received ${signal}. Stopping bot polling...`); + + try { + await bot.stopPolling(); + } catch (error) { + logError(error); + } finally { + process.exit(0); + } +} + async function main() { const start = Date.now(); @@ -286,4 +303,12 @@ bot.on("message", processNewMessage); bot.on("inline_query", processInlineQuery); bot.on("callback_query", processCallbackQuery); +process.on("SIGTERM", () => { + shutdown("SIGTERM").catch(logError); +}); + +process.on("SIGINT", () => { + shutdown("SIGINT").catch(logError); +}); + main().catch(logError); \ No newline at end of file