1
0
Fork 0
mirror of https://github.com/muerwre/vk-tg-bot.git synced 2025-04-24 22:46:41 +07:00

#5 fixed bot stopping

This commit is contained in:
Fedor Katurov 2021-05-12 13:49:16 +07:00
parent 6344f6dc5a
commit 2bcf8ff772

View file

@ -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<boolean> => {
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);
};
}