From 196e734b5c215aff92cda7bf8d4d2fc8804c0487 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Fri, 24 Apr 2020 16:09:11 +0300 Subject: [PATCH 1/5] fix: wrong telegraf instance using --- lib/telegraf.provider.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/telegraf.provider.ts b/lib/telegraf.provider.ts index 14d02f1..95f1adf 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'; From ef120e4e404b47d36e37a0afe870e14d79bd1157 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Fri, 24 Apr 2020 17:43:22 +0300 Subject: [PATCH 2/5] chore: change telegraf provider name in constants --- lib/telegraf.constants.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 = { From 36c9d5a2fa90e890f7abf97acad0686fdf6d04cd Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Fri, 24 Apr 2020 17:56:55 +0300 Subject: [PATCH 3/5] docs: webhook docs added --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 33c3b98..f81abb4 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,22 @@ 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')); + +// set up a webhook immediately if necessary +telegrafService.telegram.setWebhook('https://server.tld:3000/secret-path'); +``` + ## 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). From 19f3fc7134dad4ae5e593e9f7466fd1b0c37ec5c Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Fri, 24 Apr 2020 17:59:16 +0300 Subject: [PATCH 4/5] chore: bump version --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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", From a5962fa7b9b939f21cfac69b87038350743fbba7 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Fri, 24 Apr 2020 18:52:54 +0300 Subject: [PATCH 5/5] 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) {