nestjs-telegraf/lib/telegraf.provider.ts

37 lines
942 B
TypeScript
Raw Normal View History

2020-03-19 16:21:35 +03:00
import {
Injectable,
Inject,
OnApplicationBootstrap,
Logger,
OnApplicationShutdown,
2020-03-19 16:21:35 +03:00
} from '@nestjs/common';
2020-04-24 16:09:11 +03:00
import Telegraf, { ContextMessageUpdate } from 'telegraf';
2020-03-19 16:21:35 +03:00
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants';
import { TelegrafModuleOptions } from './interfaces';
@Injectable()
export class TelegrafProvider<TContext extends ContextMessageUpdate>
// @ts-ignore
2020-03-19 16:21:35 +03:00
extends Telegraf<TContext>
implements OnApplicationBootstrap, OnApplicationShutdown {
2020-03-19 16:21:35 +03:00
private logger = new Logger('Telegraf');
2020-04-24 18:52:54 +03:00
private launchOptions;
2020-03-19 16:21:35 +03:00
constructor(@Inject(TELEGRAF_MODULE_OPTIONS) options: TelegrafModuleOptions) {
super(options.token, options.options);
2020-04-24 18:52:54 +03:00
this.launchOptions = options.launchOptions;
2020-03-19 16:21:35 +03:00
}
onApplicationBootstrap() {
this.catch((e) => {
2020-03-19 16:21:35 +03:00
this.logger.error(e);
});
2020-04-24 18:52:54 +03:00
this.launch(this.launchOptions);
2020-03-19 16:21:35 +03:00
}
async onApplicationShutdown(signal?: string) {
await this.stop();
}
2020-03-19 16:21:35 +03:00
}