From 726596ce125062c934fff8d15be729ec495df7fb Mon Sep 17 00:00:00 2001 From: Pavel Gnedov Date: Wed, 9 Nov 2022 18:43:18 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=20help=20=D1=81=D0=BE?= =?UTF-8?q?=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=B8=D0=B7=20?= =?UTF-8?q?=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=87=D0=B8=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2=20=D0=BA=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4=20=D0=B8?= =?UTF-8?q?=D0=B7=20telegram?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.module.ts | 4 ++-- ...urrent-issues-eccm.bot-handler.service.ts} | 18 ++++++++++++++---- src/telegram-bot/telegram-bot.service.ts | 19 +++++++++++++------ .../telegram.bot-handler.interface.ts | 7 +++++++ 4 files changed, 36 insertions(+), 12 deletions(-) rename src/telegram-bot/handlers/{current-issues.bot-handler.service.ts => current-issues-eccm.bot-handler.service.ts} (75%) create mode 100644 src/telegram-bot/telegram.bot-handler.interface.ts diff --git a/src/app.module.ts b/src/app.module.ts index d2cddd0..35b18cf 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -24,7 +24,7 @@ import { UserMetaInfo } from './couchdb-datasources/user-meta-info'; import { PersonalNotificationAdapterService } from './notifications/adapters/personal-notification.adapter/personal-notification.adapter.service'; import { StatusChangeAdapterService } from './notifications/adapters/status-change.adapter.service'; import { CurrentIssuesEccmReportService } from './reports/current-issues-eccm.report.service'; -import { CurrentIssuesBotHandlerService } from './telegram-bot/handlers/current-issues.bot-handler.service'; +import { CurrentIssuesEccmBotHandlerService } from './telegram-bot/handlers/current-issues-eccm.bot-handler.service'; import { CurrentIssuesEccmReportController } from './reports/current-issues-eccm.report.controller'; import { DailyEccmReportController } from './reports/daily-eccm.report.controller'; import { DailyEccmReportService } from './reports/daily-eccm.report.service'; @@ -61,7 +61,7 @@ import { ChangesService } from './changes/changes.service'; PersonalNotificationAdapterService, StatusChangeAdapterService, CurrentIssuesEccmReportService, - CurrentIssuesBotHandlerService, + CurrentIssuesEccmBotHandlerService, DailyEccmReportService, ChangesService, ], diff --git a/src/telegram-bot/handlers/current-issues.bot-handler.service.ts b/src/telegram-bot/handlers/current-issues-eccm.bot-handler.service.ts similarity index 75% rename from src/telegram-bot/handlers/current-issues.bot-handler.service.ts rename to src/telegram-bot/handlers/current-issues-eccm.bot-handler.service.ts index 78f9e60..2392ccc 100644 --- a/src/telegram-bot/handlers/current-issues.bot-handler.service.ts +++ b/src/telegram-bot/handlers/current-issues-eccm.bot-handler.service.ts @@ -2,17 +2,23 @@ import { Injectable, Logger } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import TelegramBot from 'node-telegram-bot-api'; import { EccmConfig } from 'src/models/eccm-config.model'; -import { CurrentIssuesEccmReport, CurrentIssuesEccmReportService } from 'src/reports/current-issues-eccm.report.service'; +import { + CurrentIssuesEccmReport, + 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 { TelegramBotHandlerInterface } from '../telegram.bot-handler.interface'; @Injectable() -export class CurrentIssuesBotHandlerService { - private forName = /\/current_issues_eccm (.+) (.+)/; +export class CurrentIssuesEccmBotHandlerService + implements TelegramBotHandlerInterface +{ + private forName = /\/current_issues_eccm (.+)/; private forCurrentUser = /\/current_issues_eccm/; private service: TelegramBotService; private eccmConfig: EccmConfig.Config; - private logger = new Logger(CurrentIssuesBotHandlerService.name); + private logger = new Logger(CurrentIssuesEccmBotHandlerService.name); constructor( private configService: ConfigService, @@ -44,4 +50,8 @@ export class CurrentIssuesBotHandlerService { }); }); } + + getHelpMsg(): string { + return '/current_issues_eccm - ваши текущие задачи из проекта ECCM'; + } } diff --git a/src/telegram-bot/telegram-bot.service.ts b/src/telegram-bot/telegram-bot.service.ts index 37da5fa..5c5dfe2 100644 --- a/src/telegram-bot/telegram-bot.service.ts +++ b/src/telegram-bot/telegram-bot.service.ts @@ -5,7 +5,8 @@ import TelegramBot from 'node-telegram-bot-api'; import { UserMetaInfoService } from 'src/user-meta-info/user-meta-info.service'; import axios from 'axios'; import { UserMetaInfoModel } from 'src/models/user-meta-info.model'; -import { CurrentIssuesBotHandlerService } from './handlers/current-issues.bot-handler.service'; +import { CurrentIssuesEccmBotHandlerService } from './handlers/current-issues-eccm.bot-handler.service'; +import { TelegramBotHandlerInterface } from './telegram.bot-handler.interface'; @Injectable() export class TelegramBotService { @@ -16,16 +17,19 @@ export class TelegramBotService { private registerRe = /\/register (\d+) (.+)/; + private handlers: TelegramBotHandlerInterface[] = []; + constructor( private userMetaInfoService: UserMetaInfoService, private usersService: UsersService, private configService: ConfigService, - private currentIssuesBotHandlerService: CurrentIssuesBotHandlerService, + private currentIssuesBotHandlerService: CurrentIssuesEccmBotHandlerService, ) { this.telegramBotToken = this.configService.get('telegramBotToken'); this.redminePublicUrlPrefix = this.configService.get('redmineUrlPublic'); this.initTelegramBot(); + this.handlers.push(this.currentIssuesBotHandlerService); } private async initTelegramBot(): Promise { @@ -51,19 +55,22 @@ export class TelegramBotService { msg.chat.id, ); let helpMessage: string; + /* eslint-disable */ if (userMetaInfo) { - // eslint-disable-next-line prettier/prettier + const handlersMessages = this.handlers + .map((handler) => handler.getHelpMsg()) + .join('\n'); helpMessage = [ - `/current_issues - мои текущие задачи`, + handlersMessages, `/help` ].join('\n'); } else { - // eslint-disable-next-line prettier/prettier helpMessage = [ `/register `, - `/help` + `/help`, ].join('\n'); } + /* eslint-enable */ this.logger.debug( `Sent help message for telegramChatId = ${msg.chat.id}, ` + `message = ${helpMessage}`, diff --git a/src/telegram-bot/telegram.bot-handler.interface.ts b/src/telegram-bot/telegram.bot-handler.interface.ts new file mode 100644 index 0000000..cc20fc3 --- /dev/null +++ b/src/telegram-bot/telegram.bot-handler.interface.ts @@ -0,0 +1,7 @@ +import TelegramBot from 'node-telegram-bot-api'; +import { TelegramBotService } from './telegram-bot.service'; + +export interface TelegramBotHandlerInterface { + init(service: TelegramBotService, bot: TelegramBot): Promise; + getHelpMsg(): string; +}