Update to Telegraf v4.0.0 (#212)

* feat(telegraf): update to v4.0.0

* chore(lock): update package-lock

* chore(deps): remove telegraf from dependencies

* chore(readme): update installation command

* chore(deps): add telegraf to dev dependencies
This commit is contained in:
Morbo
2021-01-11 16:17:32 +03:00
committed by GitHub
parent b16f0b9fa5
commit 663ebf1a62
21 changed files with 185 additions and 239 deletions

View File

@@ -1,10 +1,8 @@
import { createMissedListenerDecorator } from '../../utils';
import { createListenerDecorator } from '../../utils';
/**
* Cashtag handling.
*
* @see https://telegraf.js.org/#/?id=cashtag
*/
export const Cashtag = createMissedListenerDecorator<[string | string[]]>(
'cashtag',
);
export const Cashtag = createListenerDecorator('cashtag');

View File

@@ -1,10 +1,8 @@
import { createMissedListenerDecorator } from '../../utils';
import { createListenerDecorator } from '../../utils';
/**
* Registers middleware for handling messages with email entity.
*
* @see https://telegraf.js.org/#/?id=telegraf-email
*/
export const Email = createMissedListenerDecorator<[string | string[]]>(
'email',
);
export const Email = createListenerDecorator('email');

View File

@@ -1,10 +1,8 @@
import { createMissedListenerDecorator } from '../../utils';
import { createListenerDecorator } from '../../utils';
/**
* Hashtag handling.
*
* @see https://telegraf.js.org/#/?id=hashtag
*/
export const Hashtag = createMissedListenerDecorator<[string | string[]]>(
'hashtag',
);
export const Hashtag = createListenerDecorator('hashtag');

View File

@@ -1,11 +1,8 @@
import { createMissedListenerDecorator } from '../../utils';
import { HearsTriggers } from 'telegraf/typings/composer';
import { createListenerDecorator } from '../../utils';
/**
* Registers middleware for handling inline_query actions with regular expressions.
*
* @see https://telegraf.js.org/#/?id=inlinequery
*/
export const InlineQuery = createMissedListenerDecorator<
[HearsTriggers<unknown>]
>('inlineQuery');
export const InlineQuery = createListenerDecorator('inlineQuery');

View File

@@ -1,10 +1,8 @@
import { createMissedListenerDecorator } from '../../utils';
import { createListenerDecorator } from '../../utils';
/**
* Mention handling.
*
* @see https://telegraf.js.org/#/?id=mention
*/
export const Mention = createMissedListenerDecorator<[string | string[]]>(
'mention',
);
export const Mention = createListenerDecorator('mention');

View File

@@ -1,10 +1,8 @@
import { createMissedListenerDecorator } from '../../utils';
import { createListenerDecorator } from '../../utils';
/**
* Phone number handling.
*
* @see https://telegraf.js.org/#/?id=phone
*/
export const Phone = createMissedListenerDecorator<[string | string[]]>(
'phone',
);
export const Phone = createListenerDecorator('phone');

View File

@@ -1,8 +1,8 @@
import { createMissedListenerDecorator } from '../../utils';
import { createListenerDecorator } from '../../utils';
/**
* Handler for /settings command.
*
* @see https://telegraf.js.org/#/?id=settings
*/
export const Settings = createMissedListenerDecorator<[]>('settings');
export const Settings = createListenerDecorator('settings');

View File

@@ -1,10 +1,8 @@
import { createMissedListenerDecorator } from '../../utils';
import { createListenerDecorator } from '../../utils';
/**
* Registers middleware for handling messages with text_link entity.
*
* @see https://telegraf.js.org/#/?id=telegraf-textlink
*/
export const TextLink = createMissedListenerDecorator<[string | string[]]>(
'textLink',
);
export const TextLink = createListenerDecorator('textLink');

View File

@@ -1,10 +1,8 @@
import { createMissedListenerDecorator } from '../../utils';
import { createListenerDecorator } from '../../utils';
/**
* Registers middleware for handling messages with text_mention entity.
*
* @see https://telegraf.js.org/#/?id=telegraf-textlink
*/
export const TextMention = createMissedListenerDecorator<[string | string[]]>(
'textMention',
);
export const TextMention = createListenerDecorator('textMention');

View File

@@ -1,8 +1,8 @@
import { createMissedListenerDecorator } from '../../utils';
import { createListenerDecorator } from '../../utils';
/**
* Registers middleware for handling messages with url entity.
*
* @see https://telegraf.js.org/#/?id=telegraf-url
*/
export const Url = createMissedListenerDecorator<[string | string[]]>('url');
export const Url = createListenerDecorator('url');

View File

@@ -1,3 +1,6 @@
import { createListenerDecorator } from '../../utils';
import { Scenes } from 'telegraf';
export const SceneEnter = createListenerDecorator('enter');
export const SceneEnter = createListenerDecorator<Scenes.BaseScene<never>>(
'enter',
);

View File

@@ -1,3 +1,6 @@
import { createListenerDecorator } from '../../utils';
import { Scenes } from 'telegraf';
export const SceneLeave = createListenerDecorator('leave');
export const SceneLeave = createListenerDecorator<Scenes.BaseScene<never>>(
'leave',
);

View File

@@ -1,21 +1,14 @@
import { ModuleMetadata, Type } from '@nestjs/common/interfaces';
import { Middleware, Context } from 'telegraf';
import {
TelegrafOptions,
LaunchPollingOptions,
LaunchWebhookOptions,
} from 'telegraf/typings/telegraf';
import { Middleware } from 'telegraf';
import { LaunchOptions, TelegrafOptions } from '../types';
export interface TelegrafModuleOptions<C extends Context = Context> {
export interface TelegrafModuleOptions {
token: string;
botName?: string;
options?: TelegrafOptions;
launchOptions?: {
polling?: LaunchPollingOptions;
webhook?: LaunchWebhookOptions;
};
launchOptions?: LaunchOptions;
include?: Function[];
middlewares?: ReadonlyArray<Middleware<C>>;
middlewares?: ReadonlyArray<Middleware<any>>;
}
export interface TelegrafOptionsFactory {

View File

@@ -5,7 +5,7 @@ import { MetadataScanner } from '@nestjs/core/metadata-scanner';
import { Module } from '@nestjs/core/injector/module';
import { ParamMetadata } from '@nestjs/core/helpers/interfaces';
import { ExternalContextCreator } from '@nestjs/core/helpers/external-context-creator';
import { BaseScene, Composer, Context, Stage, Telegraf } from 'telegraf';
import { Composer, Context, Scenes, Telegraf } from 'telegraf';
import { MetadataAccessorService } from './metadata-accessor.service';
import {
@@ -23,7 +23,7 @@ export class ListenersExplorerService
extends BaseExplorerService
implements OnModuleInit {
private readonly telegrafParamsFactory = new TelegrafParamsFactory();
private readonly stage = new Stage([]);
private readonly stage = new Scenes.Stage();
private bot: Telegraf<any>;
constructor(
@@ -42,7 +42,7 @@ export class ListenersExplorerService
}
onModuleInit(): void {
this.bot = this.moduleRef.get<Telegraf<never>>(this.botName, {
this.bot = this.moduleRef.get<Telegraf<any>>(this.botName, {
strict: false,
});
this.bot.use(this.stage.middleware());
@@ -75,7 +75,7 @@ export class ListenersExplorerService
const sceneId = this.metadataAccessor.getSceneMetadata(
wrapper.instance.constructor,
);
const scene = new BaseScene(sceneId);
const scene = new Scenes.BaseScene<any>(sceneId);
this.stage.register(scene);
this.registerListeners(scene, wrapper);

View File

@@ -1,4 +1,4 @@
import { BaseScene, Composer, Middleware } from 'telegraf';
import { Composer, Middleware, Telegraf } from 'telegraf';
export type Filter<T extends any[], F> = T extends []
? []
@@ -17,5 +17,5 @@ export type ComposerMethodArgs<
U extends OnlyFunctionPropertyNames<T> = OnlyFunctionPropertyNames<T>
> = Filter<Parameters<T[U]>, Middleware<never>>;
export type ComposerMethods = OnlyFunctionPropertyNames<Composer<never>>;
export type SceneMethods = OnlyFunctionPropertyNames<BaseScene<never>>;
export type LaunchOptions = Parameters<Telegraf['launch']>[0];
export type TelegrafOptions = ConstructorParameters<typeof Telegraf>[1];

View File

@@ -3,8 +3,8 @@ import { TelegrafModuleOptions } from '../interfaces';
export async function createBotFactory(
options: TelegrafModuleOptions,
): Promise<Telegraf<never>> {
const bot = new Telegraf<never>(options.token, options.options);
): Promise<Telegraf<any>> {
const bot = new Telegraf<any>(options.token, options.options);
bot.use(...(options.middlewares ?? []));
await bot.launch(options.launchOptions);

View File

@@ -1,26 +1,14 @@
import { SetMetadata } from '@nestjs/common';
import { BaseScene as Scene } from 'telegraf';
import { ComposerMethodArgs, SceneMethods } from '../types';
import { Composer } from 'telegraf';
import { ComposerMethodArgs, OnlyFunctionPropertyNames } from '../types';
import { LISTENER_METADATA } from '../telegraf.constants';
import { ListenerMetadata } from '../interfaces';
export function createListenerDecorator<TMethod extends SceneMethods>(
method: TMethod,
) {
return (
...args: ComposerMethodArgs<Scene<never>, TMethod>
): MethodDecorator => {
return SetMetadata(LISTENER_METADATA, {
method,
args,
} as ListenerMetadata);
};
}
export function createMissedListenerDecorator<TArgs extends any[]>(
method: string,
) {
return (...args: TArgs): MethodDecorator => {
export function createListenerDecorator<
TComposer extends Composer<never>,
TMethod extends OnlyFunctionPropertyNames<TComposer> = OnlyFunctionPropertyNames<TComposer>
>(method: TMethod) {
return (...args: ComposerMethodArgs<TComposer, TMethod>): MethodDecorator => {
return SetMetadata(LISTENER_METADATA, {
method,
args,