feat: upgrade telegraf package to the latest version

This commit is contained in:
Aleksandr Bukhalo 2020-05-03 09:46:28 +03:00
parent 2133594688
commit aece1d0bca
10 changed files with 43 additions and 42 deletions

View File

@ -1 +0,0 @@
lib/telegraf.provider.ts

View File

@ -1,8 +1,9 @@
import { SetMetadata } from '@nestjs/common'; import { SetMetadata } from '@nestjs/common';
import { DECORATORS } from '../telegraf.constants'; import { DECORATORS } from '../telegraf.constants';
import { HearsTriggers } from 'telegraf'; import { HearsTriggers } from 'telegraf/typings/composer';
import { Context } from '../interfaces';
export type TelegrafActionTriggers = HearsTriggers; export type TelegrafActionTriggers = HearsTriggers<Context>;
export interface TelegrafActionMetadata { export interface TelegrafActionMetadata {
triggers: TelegrafActionTriggers; triggers: TelegrafActionTriggers;

View File

@ -1,8 +1,9 @@
import { SetMetadata } from '@nestjs/common'; import { SetMetadata } from '@nestjs/common';
import { DECORATORS } from '../telegraf.constants'; import { DECORATORS } from '../telegraf.constants';
import { HearsTriggers } from 'telegraf'; import { HearsTriggers } from 'telegraf/typings/composer';
import { Context } from '../interfaces';
export type TelegrafHearsTriggers = HearsTriggers; export type TelegrafHearsTriggers = HearsTriggers<Context>;
export interface TelegrafHearsMetadata { export interface TelegrafHearsMetadata {
triggers: TelegrafHearsTriggers; triggers: TelegrafHearsTriggers;

View File

@ -0,0 +1,5 @@
import { TelegrafContext } from 'telegraf/typings/context';
export interface Context extends TelegrafContext {
[key: string]: any;
}

View File

@ -1,2 +1,2 @@
export { ContextMessageUpdate } from 'telegraf'; export * from './context.interface';
export * from './telegraf-options.interface'; export * from './telegraf-options.interface';

View File

@ -3,7 +3,7 @@ import {
TelegrafOptions, TelegrafOptions,
LaunchPollingOptions, LaunchPollingOptions,
LaunchWebhookOptions, LaunchWebhookOptions,
} from 'telegraf'; } from 'telegraf/typings/telegraf';
export interface TelegrafModuleOptions { export interface TelegrafModuleOptions {
token: string; token: string;

View File

@ -5,7 +5,6 @@ import { MetadataScanner } from '@nestjs/core/metadata-scanner';
import { TelegrafMetadataAccessor } from './telegraf-metadata.accessor'; import { TelegrafMetadataAccessor } from './telegraf-metadata.accessor';
import { TelegrafProvider } from './telegraf.provider'; import { TelegrafProvider } from './telegraf.provider';
import { TELEGRAF_PROVIDER } from './telegraf.constants'; import { TELEGRAF_PROVIDER } from './telegraf.constants';
import { Telegraf, ContextMessageUpdate } from 'telegraf';
import { import {
TelegrafActionMetadata, TelegrafActionMetadata,
TelegrafCashtagMetadata, TelegrafCashtagMetadata,
@ -41,9 +40,11 @@ export class TelegrafExplorer implements OnModuleInit {
return; return;
} }
const telegraf = this.moduleRef.get<TelegrafProvider<any>>( const telegraf: TelegrafProvider = this.moduleRef.get<TelegrafProvider>(
TELEGRAF_PROVIDER, TELEGRAF_PROVIDER,
{ strict: false }, {
strict: false,
},
); );
this.metadataScanner.scanFromPrototype( this.metadataScanner.scanFromPrototype(
@ -118,18 +119,14 @@ export class TelegrafExplorer implements OnModuleInit {
}); });
} }
handleTelegrafUse( handleTelegrafUse(instance: object, key: string, telegraf: TelegrafProvider) {
instance: object,
key: string,
telegraf: Telegraf<ContextMessageUpdate>,
) {
telegraf.use(instance[key].bind(instance)); telegraf.use(instance[key].bind(instance));
} }
handleTelegrafOn( handleTelegrafOn(
instance: object, instance: object,
key: string, key: string,
telegraf: Telegraf<ContextMessageUpdate>, telegraf: TelegrafProvider,
metadata: TelegrafOnMetadata, metadata: TelegrafOnMetadata,
) { ) {
telegraf.on(metadata.updateTypes, instance[key].bind(instance)); telegraf.on(metadata.updateTypes, instance[key].bind(instance));
@ -138,7 +135,7 @@ export class TelegrafExplorer implements OnModuleInit {
handleTelegrafHears( handleTelegrafHears(
instance: object, instance: object,
key: string, key: string,
telegraf: Telegraf<ContextMessageUpdate>, telegraf: TelegrafProvider,
metadata: TelegrafHearsMetadata, metadata: TelegrafHearsMetadata,
) { ) {
telegraf.hears(metadata.triggers, instance[key].bind(instance)); telegraf.hears(metadata.triggers, instance[key].bind(instance));
@ -147,7 +144,7 @@ export class TelegrafExplorer implements OnModuleInit {
handleTelegrafCommand( handleTelegrafCommand(
instance: object, instance: object,
key: string, key: string,
telegraf: Telegraf<ContextMessageUpdate>, telegraf: TelegrafProvider,
metadata: TelegrafCommandMetadata, metadata: TelegrafCommandMetadata,
) { ) {
telegraf.command(metadata.commands, instance[key].bind(instance)); telegraf.command(metadata.commands, instance[key].bind(instance));
@ -156,7 +153,7 @@ export class TelegrafExplorer implements OnModuleInit {
handleTelegrafStart( handleTelegrafStart(
instance: object, instance: object,
key: string, key: string,
telegraf: Telegraf<ContextMessageUpdate>, telegraf: TelegrafProvider,
) { ) {
telegraf.start(instance[key].bind(instance)); telegraf.start(instance[key].bind(instance));
} }
@ -164,7 +161,7 @@ export class TelegrafExplorer implements OnModuleInit {
handleTelegrafHelp( handleTelegrafHelp(
instance: object, instance: object,
key: string, key: string,
telegraf: Telegraf<ContextMessageUpdate>, telegraf: TelegrafProvider,
) { ) {
telegraf.help(instance[key].bind(instance)); telegraf.help(instance[key].bind(instance));
} }
@ -172,7 +169,7 @@ export class TelegrafExplorer implements OnModuleInit {
handleTelegrafSettings( handleTelegrafSettings(
instance: object, instance: object,
key: string, key: string,
telegraf: Telegraf<ContextMessageUpdate>, telegraf: TelegrafProvider,
) { ) {
// @ts-ignore // @ts-ignore
telegraf.settings(instance[key].bind(instance)); telegraf.settings(instance[key].bind(instance));
@ -181,7 +178,7 @@ export class TelegrafExplorer implements OnModuleInit {
handleTelegrafEntity( handleTelegrafEntity(
instance: object, instance: object,
key: string, key: string,
telegraf: Telegraf<ContextMessageUpdate>, telegraf: TelegrafProvider,
metadata: TelegrafEntityMetadata, metadata: TelegrafEntityMetadata,
) { ) {
// @ts-ignore // @ts-ignore
@ -191,7 +188,7 @@ export class TelegrafExplorer implements OnModuleInit {
handleTelegrafMention( handleTelegrafMention(
instance: object, instance: object,
key: string, key: string,
telegraf: Telegraf<ContextMessageUpdate>, telegraf: TelegrafProvider,
metadata: TelegrafMentionMetadata, metadata: TelegrafMentionMetadata,
) { ) {
// @ts-ignore // @ts-ignore
@ -201,7 +198,7 @@ export class TelegrafExplorer implements OnModuleInit {
handleTelegrafPhone( handleTelegrafPhone(
instance: object, instance: object,
key: string, key: string,
telegraf: Telegraf<ContextMessageUpdate>, telegraf: TelegrafProvider,
metadata: TelegrafPhoneMetadata, metadata: TelegrafPhoneMetadata,
) { ) {
// @ts-ignore // @ts-ignore
@ -211,7 +208,7 @@ export class TelegrafExplorer implements OnModuleInit {
handleTelegrafHashtag( handleTelegrafHashtag(
instance: object, instance: object,
key: string, key: string,
telegraf: Telegraf<ContextMessageUpdate>, telegraf: TelegrafProvider,
metadata: TelegrafHashtagMetadata, metadata: TelegrafHashtagMetadata,
) { ) {
// @ts-ignore // @ts-ignore
@ -221,7 +218,7 @@ export class TelegrafExplorer implements OnModuleInit {
handleTelegrafCashtag( handleTelegrafCashtag(
instance: object, instance: object,
key: string, key: string,
telegraf: Telegraf<ContextMessageUpdate>, telegraf: TelegrafProvider,
metadata: TelegrafCashtagMetadata, metadata: TelegrafCashtagMetadata,
) { ) {
// @ts-ignore // @ts-ignore
@ -231,7 +228,7 @@ export class TelegrafExplorer implements OnModuleInit {
handleTelegrafAction( handleTelegrafAction(
instance: object, instance: object,
key: string, key: string,
telegraf: Telegraf<ContextMessageUpdate>, telegraf: TelegrafProvider,
metadata: TelegrafActionMetadata, metadata: TelegrafActionMetadata,
) { ) {
telegraf.action(metadata.triggers, instance[key].bind(instance)); telegraf.action(metadata.triggers, instance[key].bind(instance));
@ -240,7 +237,7 @@ export class TelegrafExplorer implements OnModuleInit {
handleTelegrafInlineQuery( handleTelegrafInlineQuery(
instance: object, instance: object,
key: string, key: string,
telegraf: Telegraf<ContextMessageUpdate>, telegraf: TelegrafProvider,
metadata: TelegrafInlineQueryMetadata, metadata: TelegrafInlineQueryMetadata,
) { ) {
// @ts-ignore // @ts-ignore
@ -250,7 +247,7 @@ export class TelegrafExplorer implements OnModuleInit {
handleTelegrafGameQuery( handleTelegrafGameQuery(
instance: object, instance: object,
key: string, key: string,
telegraf: Telegraf<ContextMessageUpdate>, telegraf: TelegrafProvider,
) { ) {
telegraf.gameQuery(instance[key].bind(instance)); telegraf.gameQuery(instance[key].bind(instance));
} }

View File

@ -5,32 +5,30 @@ import {
Logger, Logger,
OnApplicationShutdown, OnApplicationShutdown,
} from '@nestjs/common'; } from '@nestjs/common';
import Telegraf, { ContextMessageUpdate } from 'telegraf'; import { Telegraf } from 'telegraf';
import { Context, TelegrafModuleOptions } from './interfaces';
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants'; import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants';
import { TelegrafModuleOptions } from './interfaces';
@Injectable() @Injectable()
export class TelegrafProvider<TContext extends ContextMessageUpdate> export class TelegrafProvider extends Telegraf<Context>
// @ts-ignore
extends Telegraf<TContext>
implements OnApplicationBootstrap, OnApplicationShutdown { implements OnApplicationBootstrap, OnApplicationShutdown {
private logger = new Logger('Telegraf'); private logger = new Logger('Telegraf');
private launchOptions; private readonly 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; this.launchOptions = options.launchOptions;
} }
onApplicationBootstrap() { async onApplicationBootstrap() {
this.catch((e) => { this.catch((e) => {
this.logger.error(e); this.logger.error(e);
}); });
this.launch(this.launchOptions); await this.launch(this.launchOptions);
} }
async onApplicationShutdown(signal?: string) { async onApplicationShutdown() {
await this.stop(); await this.stop();
} }
} }

6
package-lock.json generated
View File

@ -2845,9 +2845,9 @@
} }
}, },
"telegraf": { "telegraf": {
"version": "3.37.0", "version": "3.38.0",
"resolved": "https://registry.npmjs.org/telegraf/-/telegraf-3.37.0.tgz", "resolved": "https://registry.npmjs.org/telegraf/-/telegraf-3.38.0.tgz",
"integrity": "sha512-V3448qwfOolBqkIc87yxjW4zMvR2P6AIF24pPTlX9WhZPwA1TF/x3nQhnWPRLtGh2SJuvDcr83iTkXPXT7Opnw==", "integrity": "sha512-va4VlrKWp64JrowFoZX/NPzzA6q38kvaIukVXOWFO1V+jR1G8+hCfgJy4TX8Z3rwLJzwaBEet1QhikHDRZWl3A==",
"requires": { "requires": {
"debug": "^4.0.1", "debug": "^4.0.1",
"minimist": "^1.2.0", "minimist": "^1.2.0",

View File

@ -33,7 +33,7 @@
"test": "" "test": ""
}, },
"dependencies": { "dependencies": {
"telegraf": "3.37.0" "telegraf": "3.38.0"
}, },
"devDependencies": { "devDependencies": {
"@nestjs/common": "7.0.9", "@nestjs/common": "7.0.9",