Обработка ошибок telegram

This commit is contained in:
Gnedov Pavel 2023-02-07 08:28:23 +07:00
parent 10cc06ec8d
commit 7cde09c895

View file

@ -30,14 +30,19 @@ export class TelegramBotService {
this.telegramBotToken = this.configService.get<string>('telegramBotToken');
this.redminePublicUrlPrefix =
this.configService.get<string>('redmineUrlPublic');
this.initTelegramBot();
this.initTelegramBot().catch((ex) => {
this.logger.error(`Error at init telegram bot - ${ex}`);
});
this.handlers.push(this.currentIssuesBotHandlerService);
this.handlers.push(this.setDailyEccmUserCommentBotHandlerService);
}
private async initTelegramBot(): Promise<void> {
const Telegram = await require('node-telegram-bot-api');
if (!this.telegramBotToken) return;
this.logger.debug('Telegram bot instance creation ... ');
this.bot = new Telegram(this.telegramBotToken, { polling: true });
this.logger.debug('Telegram bot instance created');
this.bot.onText(/\/start/, async (msg) => {
await this.showHelpMessage(msg);
});
@ -50,6 +55,12 @@ export class TelegramBotService {
this.bot.onText(/\/leave/, async (msg) => {
await this.leave(msg);
});
this.bot.on('polling_error', (error) => {
this.logger.error(`polling_error from telegram bot instance - ${error}`);
});
this.bot.on('webhook_error', (error) => {
this.logger.error(`webhook_error from telegram bot instance - ${error}`);
});
for (let i = 0; i < this.handlers.length; i++) {
const handler = this.handlers[i];
await handler.init(this, this.bot);
@ -57,6 +68,7 @@ export class TelegramBotService {
}
private async showHelpMessage(msg: TelegramBot.Message): Promise<void> {
if (!this.telegramBotToken) return;
const userMetaInfo = await this.userMetaInfoService.findByTelegramId(
msg.chat.id,
);
@ -81,7 +93,11 @@ export class TelegramBotService {
`Sent help message for telegramChatId = ${msg.chat.id}, ` +
`message = ${helpMessage}`,
);
this.bot.sendMessage(msg.chat.id, helpMessage);
try {
this.bot.sendMessage(msg.chat.id, helpMessage);
} catch (ex) {
this.logger.error(`Error at send help message - ${ex?.message}`);
}
}
async sendMessageByRedmineId(
@ -89,6 +105,7 @@ export class TelegramBotService {
msg: string,
options?: TelegramBot.SendMessageOptions,
): Promise<boolean> {
if (!this.telegramBotToken) return false;
const userMetaInfo = await this.userMetaInfoService.findByRedmineId(
redmineId,
);
@ -110,6 +127,7 @@ export class TelegramBotService {
msg: string,
options?: TelegramBot.SendMessageOptions,
): Promise<boolean> {
if (!this.telegramBotToken) return false;
const user = await this.usersService.findUserByName(firstname, lastname);
if (!user) return false;
return await this.sendMessageByRedmineId(user.id, msg, options);