2020-03-19 16:21:35 +03:00
|
|
|
import {
|
|
|
|
Injectable,
|
|
|
|
Inject,
|
|
|
|
OnApplicationBootstrap,
|
|
|
|
Logger,
|
2020-03-19 18:20:07 +03:00
|
|
|
OnApplicationShutdown,
|
2020-03-19 16:21:35 +03:00
|
|
|
} from '@nestjs/common';
|
2020-05-03 09:46:28 +03:00
|
|
|
import { Telegraf } from 'telegraf';
|
|
|
|
import { Context, TelegrafModuleOptions } from './interfaces';
|
2020-03-19 16:21:35 +03:00
|
|
|
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants';
|
|
|
|
|
|
|
|
@Injectable()
|
2020-12-23 21:35:40 +03:00
|
|
|
export class TelegrafProvider<C extends Context = Context>
|
|
|
|
extends Telegraf<C>
|
2020-03-19 18:20:07 +03:00
|
|
|
implements OnApplicationBootstrap, OnApplicationShutdown {
|
2020-03-19 16:21:35 +03:00
|
|
|
private logger = new Logger('Telegraf');
|
2020-05-03 09:46:28 +03:00
|
|
|
private readonly 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
|
|
|
}
|
|
|
|
|
2020-12-23 21:35:40 +03:00
|
|
|
async onApplicationBootstrap(): Promise<void> {
|
|
|
|
this.catch(async (err, ctx) => {
|
2020-05-05 16:34:44 +03:00
|
|
|
this.logger.error(
|
|
|
|
`Encountered an error for ${ctx.updateType} update type`,
|
2020-12-23 21:35:40 +03:00
|
|
|
err as string,
|
2020-05-05 16:34:44 +03:00
|
|
|
);
|
2020-03-19 16:21:35 +03:00
|
|
|
});
|
2020-04-24 18:52:54 +03:00
|
|
|
|
2020-05-03 09:46:28 +03:00
|
|
|
await this.launch(this.launchOptions);
|
2020-03-19 16:21:35 +03:00
|
|
|
}
|
2020-03-19 18:20:07 +03:00
|
|
|
|
2020-12-23 21:35:40 +03:00
|
|
|
async onApplicationShutdown(): Promise<void> {
|
2020-03-19 18:20:07 +03:00
|
|
|
await this.stop();
|
|
|
|
}
|
2020-03-19 16:21:35 +03:00
|
|
|
}
|