Skip to content

Commit

Permalink
Update to Telegraf v4.0.0 (#212)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Morb0 authored Jan 11, 2021
1 parent b16f0b9 commit 663ebf1
Show file tree
Hide file tree
Showing 21 changed files with 185 additions and 239 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ If you want to dive fully into NestJS Telegraf then don't waste your time in thi
## Installation

```bash
$ npm i nestjs-telegraf
$ npm i nestjs-telegraf telegraf
```

## Usage
Expand Down
6 changes: 2 additions & 4 deletions lib/decorators/listeners/cashtag.decorator.ts
Original file line number Diff line number Diff line change
@@ -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');
6 changes: 2 additions & 4 deletions lib/decorators/listeners/email.decorator.ts
Original file line number Diff line number Diff line change
@@ -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');
6 changes: 2 additions & 4 deletions lib/decorators/listeners/hashtag.decorator.ts
Original file line number Diff line number Diff line change
@@ -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');
7 changes: 2 additions & 5 deletions lib/decorators/listeners/inline-query.decorator.ts
Original file line number Diff line number Diff line change
@@ -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');
6 changes: 2 additions & 4 deletions lib/decorators/listeners/mention.decorator.ts
Original file line number Diff line number Diff line change
@@ -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');
6 changes: 2 additions & 4 deletions lib/decorators/listeners/phone.decorator.ts
Original file line number Diff line number Diff line change
@@ -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');
4 changes: 2 additions & 2 deletions lib/decorators/listeners/settings.decorator.ts
Original file line number Diff line number Diff line change
@@ -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');
6 changes: 2 additions & 4 deletions lib/decorators/listeners/text-link.decorator.ts
Original file line number Diff line number Diff line change
@@ -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');
6 changes: 2 additions & 4 deletions lib/decorators/listeners/text-mention.decorator.ts
Original file line number Diff line number Diff line change
@@ -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');
4 changes: 2 additions & 2 deletions lib/decorators/listeners/url.decorator.ts
Original file line number Diff line number Diff line change
@@ -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');
5 changes: 4 additions & 1 deletion lib/decorators/scene/scene-enter.decorator.ts
Original file line number Diff line number Diff line change
@@ -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',
);
5 changes: 4 additions & 1 deletion lib/decorators/scene/scene-leave.decorator.ts
Original file line number Diff line number Diff line change
@@ -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',
);
17 changes: 5 additions & 12 deletions lib/interfaces/telegraf-options.interface.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions lib/services/listeners-explorer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand All @@ -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());
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions lib/types/index.ts
Original file line number Diff line number Diff line change
@@ -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 []
? []
Expand All @@ -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];
4 changes: 2 additions & 2 deletions lib/utils/create-bot-factory.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 7 additions & 19 deletions lib/utils/create-listener-decorator.util.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Loading

1 comment on commit 663ebf1

@vercel
Copy link

@vercel vercel bot commented on 663ebf1 Jan 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.