From a5962fa7b9b939f21cfac69b87038350743fbba7 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Fri, 24 Apr 2020 18:52:54 +0300 Subject: [PATCH] feat: add launch options --- README.md | 18 ++++++++++++++++-- lib/interfaces/telegraf-options.interface.ts | 10 +++++++++- lib/telegraf.provider.ts | 5 ++++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f81abb4..732fbf7 100644 --- a/README.md +++ b/README.md @@ -158,9 +158,23 @@ const telegrafProvider = app.get('TelegrafProvider'); Now you can connect middleware: ```typescript app.use(telegrafService.webhookCallback('/secret-path')); +``` -// set up a webhook immediately if necessary -telegrafService.telegram.setWebhook('https://server.tld:3000/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 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.provider.ts b/lib/telegraf.provider.ts index 95f1adf..145d7b5 100644 --- a/lib/telegraf.provider.ts +++ b/lib/telegraf.provider.ts @@ -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) {