feat(client): add simple telegram client

This commit is contained in:
Igor Kamyshev 2019-05-11 20:55:15 +03:00
parent d36c326ee9
commit d2aa6eb3d7
2 changed files with 28 additions and 0 deletions

25
lib/TelegramClient.ts Normal file
View File

@ -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<void> {
await this.telegram.sendMessage(chatId, text)
}
}

View File

@ -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<ModuleMetadata, 'imports'> {
useClass?: Type<TelegramModuleOptionsFactory>
@ -25,6 +27,7 @@ export class TelegramModule implements NestModule {
module: TelegramModule,
providers: [
TelegramBot,
TelegramClient,
{
provide: TokenInjectionToken,
useClass: factory.useClass,