chore(release): v1.1.0 (#63) from bukhalo/feature/webhooks

Webhooks support
This commit is contained in:
Aleksandr Bukhalo 2020-04-24 18:59:19 +03:00 committed by GitHub
commit 45106e74d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 6 deletions

View File

@ -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<string>('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).

View File

@ -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 {

View File

@ -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 = {

View File

@ -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<TContext extends ContextMessageUpdate>
extends Telegraf<TContext>
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) {

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "nestjs-telegraf",
"version": "1.0.2",
"version": "1.1.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -1,6 +1,6 @@
{
"name": "nestjs-telegraf",
"version": "1.0.2",
"version": "1.1.0",
"description": "Telegraf module for NestJS",
"keywords": [
"nest",