Укорощение длинных сообщений в telegram боте

This commit is contained in:
Pavel Gnedov 2024-01-19 19:50:25 +07:00
parent f068c9d6ab
commit ddb5df9254
2 changed files with 13 additions and 4 deletions

View file

@ -7,7 +7,7 @@ import {
CurrentIssuesEccmReportService,
} from 'src/reports/current-issues-eccm.report.service';
import { UserMetaInfoService } from 'src/user-meta-info/user-meta-info.service';
import { TelegramBotService } from '../telegram-bot.service';
import { TelegramBotService, cutMessage } from '../telegram-bot.service';
import { TelegramBotHandlerInterface } from '../telegram.bot-handler.interface';
@Injectable()
@ -45,7 +45,7 @@ export class CurrentIssuesEccmBotHandlerService
fields: CurrentIssuesEccmReport.Defaults.currentIssuesFields,
});
this.logger.debug(`Current issues eccm report: ${report}`);
bot.sendMessage(msg.chat.id, report || 'empty report', {
bot.sendMessage(msg.chat.id, cutMessage(report) || 'empty report', {
parse_mode: 'HTML',
});
});

View file

@ -9,6 +9,15 @@ import { CurrentIssuesEccmBotHandlerService } from './handlers/current-issues-ec
import { TelegramBotHandlerInterface } from './telegram.bot-handler.interface';
import { SetDailyEccmUserCommentBotHandlerService } from './handlers/set-daily-eccm-user-comment.bot-handler.service';
const MAX_TELEGRAM_MESSAGE_LENGTH = 4000;
export function cutMessage(msg: string): string {
if (msg.length > MAX_TELEGRAM_MESSAGE_LENGTH) {
return msg.slice(0, 4000) + '...';
}
return msg;
}
@Injectable()
export class TelegramBotService {
private logger = new Logger(TelegramBotService.name);
@ -94,7 +103,7 @@ export class TelegramBotService {
`message = ${helpMessage}`,
);
try {
this.bot.sendMessage(msg.chat.id, helpMessage);
this.bot.sendMessage(msg.chat.id, cutMessage(helpMessage));
} catch (ex) {
this.logger.error(`Error at send help message - ${ex?.message}`);
}
@ -111,7 +120,7 @@ export class TelegramBotService {
);
if (!userMetaInfo) return false;
const chatId = userMetaInfo.telegram_chat_id;
await this.bot.sendMessage(chatId, msg, options);
await this.bot.sendMessage(chatId, cutMessage(msg), options);
this.logger.debug(
`Sent message for redmineUserId = ${redmineId}, ` +
`telegramChatId = ${chatId}, ` +