mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-24 23:14:39 +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,
|
DynamicModule,
|
||||||
} from '@nestjs/common'
|
} from '@nestjs/common'
|
||||||
import { ModuleMetadata, Type } from '@nestjs/common/interfaces'
|
import { ModuleMetadata, Type } from '@nestjs/common/interfaces'
|
||||||
|
|
||||||
import { TelegramBot } from './TelegramBot'
|
import { TelegramBot } from './TelegramBot'
|
||||||
import { TelegramModuleOptionsFactory } from './TelegramModuleOptionsFactory'
|
import { TelegramModuleOptionsFactory } from './TelegramModuleOptionsFactory'
|
||||||
import { TokenInjectionToken } from './TokenInjectionToken'
|
import { TokenInjectionToken } from './TokenInjectionToken'
|
||||||
|
import { TelegramClient } from './TelegramClient'
|
||||||
|
|
||||||
interface TelegramFactory extends Pick<ModuleMetadata, 'imports'> {
|
interface TelegramFactory extends Pick<ModuleMetadata, 'imports'> {
|
||||||
useClass?: Type<TelegramModuleOptionsFactory>
|
useClass?: Type<TelegramModuleOptionsFactory>
|
||||||
@ -25,6 +27,7 @@ export class TelegramModule implements NestModule {
|
|||||||
module: TelegramModule,
|
module: TelegramModule,
|
||||||
providers: [
|
providers: [
|
||||||
TelegramBot,
|
TelegramBot,
|
||||||
|
TelegramClient,
|
||||||
{
|
{
|
||||||
provide: TokenInjectionToken,
|
provide: TokenInjectionToken,
|
||||||
useClass: factory.useClass,
|
useClass: factory.useClass,
|
||||||
|
Loading…
Reference in New Issue
Block a user