diff --git a/src/service/telegram/index.ts b/src/service/telegram/index.ts index 159b575..666243d 100644 --- a/src/service/telegram/index.ts +++ b/src/service/telegram/index.ts @@ -27,17 +27,18 @@ export class TelegramService { this.bot = new Telegraf(props.key, options); this.bot.use(loggerTgMiddleware); - process.once("SIGINT", () => this.bot.stop("SIGINT")); - process.once("SIGTERM", () => this.bot.stop("SIGTERM")); + process.once("SIGINT", () => this.stop("SIGINT")); + process.once("SIGTERM", () => this.stop("SIGTERM")); } + get isWebhookEnabled() { + return !!this.webhook.enabled && !!this.webhook.url; + } /** * Connects to telegram */ public async start() { - const isWebhookEnabled = await this.getWebhookAvailable(); - - if (isWebhookEnabled) { + if (this.isWebhookEnabled) { await this.bot.telegram .deleteWebhook() .then(() => this.bot.telegram.setWebhook(this.webhook.url!)) @@ -67,15 +68,6 @@ export class TelegramService { return this.bot.handleUpdate(req, res); } - /** - * Checks webhook availability - */ - private getWebhookAvailable = async (): Promise => { - const isWebhookEnabled = !!this.webhook.enabled && !!this.webhook.url; - // TODO: test this.webhook.url with axios instead of 'true' - return isWebhookEnabled && true; - }; - /** * Sends simple message to channel */ @@ -103,4 +95,12 @@ export class TelegramService { caption, }); }; + + stop = (signal: string) => { + if (!this.isWebhookEnabled) { + return; + } + + this.bot.stop(signal); + }; }