diff --git a/README.md b/README.md index 33c3b98..732fbf7 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,36 @@ TelegrafModule.forRootAsync({ }); ``` +## Webhooks +If you want to configure a telegram bot webhook, you need to get a middleware from `TelegrafProvider` for connect it in your `main.ts` file. + +To access it, you must use the `app.get()` method, followed by the provider reference: +```typescript +const telegrafProvider = app.get('TelegrafProvider'); +``` + +Now you can connect middleware: +```typescript +app.use(telegrafService.webhookCallback('/secret-path')); +``` + +The last step is to specify launchOptions in `forRoot` method: +```typescript +TelegrafModule.forRootAsync({ + imports: [ConfigModule], + useFactory: async (configService: ConfigService) => ({ + token: configService.get('TELEGRAM_BOT_TOKEN'), + launchOptions: { + webhook: { + domain: 'domain.tld', + hookPath: '/secret-path', + } + } + }), + inject: [ConfigService], +}); +``` + ## Support Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). diff --git a/lib/interfaces/telegraf-options.interface.ts b/lib/interfaces/telegraf-options.interface.ts index e3b1969..cd4229f 100644 --- a/lib/interfaces/telegraf-options.interface.ts +++ b/lib/interfaces/telegraf-options.interface.ts @@ -1,9 +1,17 @@ import { ModuleMetadata, Type } from '@nestjs/common/interfaces'; -import { TelegrafOptions } from 'telegraf'; +import { + TelegrafOptions, + LaunchPollingOptions, + LaunchWebhookOptions, +} from 'telegraf'; export interface TelegrafModuleOptions { token: string; options?: TelegrafOptions; + launchOptions?: { + polling?: LaunchPollingOptions; + webhook?: LaunchWebhookOptions; + }; } export interface TelegrafOptionsFactory { diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index c70b4b9..59f27a2 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -1,5 +1,5 @@ export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS'; -export const TELEGRAF_PROVIDER = 'TELEGRAF_PROVIDER'; +export const TELEGRAF_PROVIDER = 'TelegrafProvider'; export const DECORATORS_PREFIX = 'TELEGRAF'; export const DECORATORS = { diff --git a/lib/telegraf.provider.ts b/lib/telegraf.provider.ts index 14d02f1..145d7b5 100644 --- a/lib/telegraf.provider.ts +++ b/lib/telegraf.provider.ts @@ -5,7 +5,7 @@ import { Logger, OnApplicationShutdown, } from '@nestjs/common'; -import { Telegraf, ContextMessageUpdate } from 'telegraf'; +import Telegraf, { ContextMessageUpdate } from 'telegraf'; import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants'; import { TelegrafModuleOptions } from './interfaces'; @@ -15,16 +15,19 @@ export class TelegrafProvider extends Telegraf implements OnApplicationBootstrap, OnApplicationShutdown { private logger = new Logger('Telegraf'); + private launchOptions; constructor(@Inject(TELEGRAF_MODULE_OPTIONS) options: TelegrafModuleOptions) { super(options.token, options.options); + this.launchOptions = options.launchOptions; } onApplicationBootstrap() { this.catch((e) => { this.logger.error(e); }); - this.startPolling(); + + this.launch(this.launchOptions); } async onApplicationShutdown(signal?: string) { diff --git a/package-lock.json b/package-lock.json index 4a696f1..0b3c6d3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "nestjs-telegraf", - "version": "1.0.2", + "version": "1.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7128ab1..14b0e63 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nestjs-telegraf", - "version": "1.0.2", + "version": "1.1.0", "description": "Telegraf module for NestJS", "keywords": [ "nest",