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:
parent
6344f6dc5a
commit
2bcf8ff772
1 changed files with 14 additions and 14 deletions
|
@ -27,17 +27,18 @@ export class TelegramService {
|
||||||
this.bot = new Telegraf(props.key, options);
|
this.bot = new Telegraf(props.key, options);
|
||||||
this.bot.use(loggerTgMiddleware);
|
this.bot.use(loggerTgMiddleware);
|
||||||
|
|
||||||
process.once("SIGINT", () => this.bot.stop("SIGINT"));
|
process.once("SIGINT", () => this.stop("SIGINT"));
|
||||||
process.once("SIGTERM", () => this.bot.stop("SIGTERM"));
|
process.once("SIGTERM", () => this.stop("SIGTERM"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get isWebhookEnabled() {
|
||||||
|
return !!this.webhook.enabled && !!this.webhook.url;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Connects to telegram
|
* Connects to telegram
|
||||||
*/
|
*/
|
||||||
public async start() {
|
public async start() {
|
||||||
const isWebhookEnabled = await this.getWebhookAvailable();
|
if (this.isWebhookEnabled) {
|
||||||
|
|
||||||
if (isWebhookEnabled) {
|
|
||||||
await this.bot.telegram
|
await this.bot.telegram
|
||||||
.deleteWebhook()
|
.deleteWebhook()
|
||||||
.then(() => this.bot.telegram.setWebhook(this.webhook.url!))
|
.then(() => this.bot.telegram.setWebhook(this.webhook.url!))
|
||||||
|
@ -67,15 +68,6 @@ export class TelegramService {
|
||||||
return this.bot.handleUpdate(req, res);
|
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
|
* Sends simple message to channel
|
||||||
*/
|
*/
|
||||||
|
@ -103,4 +95,12 @@ export class TelegramService {
|
||||||
caption,
|
caption,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
stop = (signal: string) => {
|
||||||
|
if (!this.isWebhookEnabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.bot.stop(signal);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue