feat: add launch options

This commit is contained in:
Aleksandr Bukhalo 2020-04-24 18:52:54 +03:00
parent 19f3fc7134
commit a5962fa7b9
3 changed files with 29 additions and 4 deletions

View File

@ -158,9 +158,23 @@ const telegrafProvider = app.get('TelegrafProvider');
Now you can connect middleware: Now you can connect middleware:
```typescript ```typescript
app.use(telegrafService.webhookCallback('/secret-path')); app.use(telegrafService.webhookCallback('/secret-path'));
```
// set up a webhook immediately if necessary The last step is to specify launchOptions in `forRoot` method:
telegrafService.telegram.setWebhook('https://server.tld:3000/secret-path'); ```typescript
TelegrafModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
token: configService.get<string>('TELEGRAM_BOT_TOKEN'),
launchOptions: {
webhook: {
domain: 'domain.tld',
hookPath: '/secret-path',
}
}
}),
inject: [ConfigService],
});
``` ```
## Support ## Support

View File

@ -1,9 +1,17 @@
import { ModuleMetadata, Type } from '@nestjs/common/interfaces'; import { ModuleMetadata, Type } from '@nestjs/common/interfaces';
import { TelegrafOptions } from 'telegraf'; import {
TelegrafOptions,
LaunchPollingOptions,
LaunchWebhookOptions,
} from 'telegraf';
export interface TelegrafModuleOptions { export interface TelegrafModuleOptions {
token: string; token: string;
options?: TelegrafOptions; options?: TelegrafOptions;
launchOptions?: {
polling?: LaunchPollingOptions;
webhook?: LaunchWebhookOptions;
};
} }
export interface TelegrafOptionsFactory { export interface TelegrafOptionsFactory {

View File

@ -15,16 +15,19 @@ export class TelegrafProvider<TContext extends ContextMessageUpdate>
extends Telegraf<TContext> extends Telegraf<TContext>
implements OnApplicationBootstrap, OnApplicationShutdown { implements OnApplicationBootstrap, OnApplicationShutdown {
private logger = new Logger('Telegraf'); private logger = new Logger('Telegraf');
private launchOptions;
constructor(@Inject(TELEGRAF_MODULE_OPTIONS) options: TelegrafModuleOptions) { constructor(@Inject(TELEGRAF_MODULE_OPTIONS) options: TelegrafModuleOptions) {
super(options.token, options.options); super(options.token, options.options);
this.launchOptions = options.launchOptions;
} }
onApplicationBootstrap() { onApplicationBootstrap() {
this.catch((e) => { this.catch((e) => {
this.logger.error(e); this.logger.error(e);
}); });
this.startPolling();
this.launch(this.launchOptions);
} }
async onApplicationShutdown(signal?: string) { async onApplicationShutdown(signal?: string) {