diff --git a/lib/TelegramClient.ts b/lib/TelegramClient.ts new file mode 100644 index 0000000..8144147 --- /dev/null +++ b/lib/TelegramClient.ts @@ -0,0 +1,25 @@ +import { Injectable, Inject } from '@nestjs/common' +import Telegram from 'telegraf/telegram' + +import { TokenInjectionToken } from './TokenInjectionToken' +import { TelegramModuleOptionsFactory } from './TelegramModuleOptionsFactory' + +@Injectable() +export class TelegramClient { + private telegram: any + + public constructor( + @Inject(TokenInjectionToken) factory: TelegramModuleOptionsFactory, + ) { + const { token } = factory.createOptions() + + this.telegram = new Telegram(token) + } + + public async sendMessage( + chatId: string | number, + text: string, + ): Promise { + await this.telegram.sendMessage(chatId, text) + } +} diff --git a/lib/telegram.module.ts b/lib/telegram.module.ts index 633d9ba..b7eac36 100644 --- a/lib/telegram.module.ts +++ b/lib/telegram.module.ts @@ -5,9 +5,11 @@ import { DynamicModule, } from '@nestjs/common' import { ModuleMetadata, Type } from '@nestjs/common/interfaces' + import { TelegramBot } from './TelegramBot' import { TelegramModuleOptionsFactory } from './TelegramModuleOptionsFactory' import { TokenInjectionToken } from './TokenInjectionToken' +import { TelegramClient } from './TelegramClient' interface TelegramFactory extends Pick { useClass?: Type @@ -25,6 +27,7 @@ export class TelegramModule implements NestModule { module: TelegramModule, providers: [ TelegramBot, + TelegramClient, { provide: TokenInjectionToken, useClass: factory.useClass,