mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-23 22:52:59 +03:00
feat(client): add simple telegram client
This commit is contained in:
parent
d36c326ee9
commit
d2aa6eb3d7
25
lib/TelegramClient.ts
Normal file
25
lib/TelegramClient.ts
Normal 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)
|
||||
}
|
||||
}
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user