Укорощение длинных сообщений в telegram боте
This commit is contained in:
parent
f068c9d6ab
commit
ddb5df9254
2 changed files with 13 additions and 4 deletions
|
|
@ -7,7 +7,7 @@ import {
|
||||||
CurrentIssuesEccmReportService,
|
CurrentIssuesEccmReportService,
|
||||||
} from 'src/reports/current-issues-eccm.report.service';
|
} from 'src/reports/current-issues-eccm.report.service';
|
||||||
import { UserMetaInfoService } from 'src/user-meta-info/user-meta-info.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';
|
import { TelegramBotHandlerInterface } from '../telegram.bot-handler.interface';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
|
|
@ -45,7 +45,7 @@ export class CurrentIssuesEccmBotHandlerService
|
||||||
fields: CurrentIssuesEccmReport.Defaults.currentIssuesFields,
|
fields: CurrentIssuesEccmReport.Defaults.currentIssuesFields,
|
||||||
});
|
});
|
||||||
this.logger.debug(`Current issues eccm report: ${report}`);
|
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',
|
parse_mode: 'HTML',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,15 @@ import { CurrentIssuesEccmBotHandlerService } from './handlers/current-issues-ec
|
||||||
import { TelegramBotHandlerInterface } from './telegram.bot-handler.interface';
|
import { TelegramBotHandlerInterface } from './telegram.bot-handler.interface';
|
||||||
import { SetDailyEccmUserCommentBotHandlerService } from './handlers/set-daily-eccm-user-comment.bot-handler.service';
|
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()
|
@Injectable()
|
||||||
export class TelegramBotService {
|
export class TelegramBotService {
|
||||||
private logger = new Logger(TelegramBotService.name);
|
private logger = new Logger(TelegramBotService.name);
|
||||||
|
|
@ -94,7 +103,7 @@ export class TelegramBotService {
|
||||||
`message = ${helpMessage}`,
|
`message = ${helpMessage}`,
|
||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
this.bot.sendMessage(msg.chat.id, helpMessage);
|
this.bot.sendMessage(msg.chat.id, cutMessage(helpMessage));
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
this.logger.error(`Error at send help message - ${ex?.message}`);
|
this.logger.error(`Error at send help message - ${ex?.message}`);
|
||||||
}
|
}
|
||||||
|
|
@ -111,7 +120,7 @@ export class TelegramBotService {
|
||||||
);
|
);
|
||||||
if (!userMetaInfo) return false;
|
if (!userMetaInfo) return false;
|
||||||
const chatId = userMetaInfo.telegram_chat_id;
|
const chatId = userMetaInfo.telegram_chat_id;
|
||||||
await this.bot.sendMessage(chatId, msg, options);
|
await this.bot.sendMessage(chatId, cutMessage(msg), options);
|
||||||
this.logger.debug(
|
this.logger.debug(
|
||||||
`Sent message for redmineUserId = ${redmineId}, ` +
|
`Sent message for redmineUserId = ${redmineId}, ` +
|
||||||
`telegramChatId = ${chatId}, ` +
|
`telegramChatId = ${chatId}, ` +
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue