chore(release): v2.0.0

This commit is contained in:
Alexander Bukhalo
2021-02-14 23:59:26 +03:00
parent 7136f57995
commit 8ddc37c424
27 changed files with 587 additions and 323 deletions

View File

@@ -1,186 +0,0 @@
---
id: decorators
title: Decorators
sidebar_label: Decorators
slug: /api-reference/decorators
---
:::caution
The described functionality is under development, the functionality has not been tested and can be changed at any time!
:::
### Update
`@Update` class decorator, it's like NestJS [`@Controller`](https://docs.nestjs.com/controllers) decorator, but for [Telegram Bot API updates](https://core.telegram.org/bots/api#getting-updates).
It is required for the class that will receive updates from Telegram.
```typescript {3}
import { Update, Context } from 'nestjs-telegraf';
@Update()
export class SomeBotService {
...
}
```
### Message
Use `@Message` method decorator for handling new incoming message of any kind — text, photo, sticker, etc.
```typescript {5}
import { Update, Message, Context } from 'nestjs-telegraf';
@Update()
export class SomeBotService {
@Message()
message(ctx: Context) {
ctx.reply(`You say: ${ctx.message.text}`);
}
}
```
### EditedMessage
Use `@EditedMessage` method decorator for handling new version of a message that is known to the bot and was edited.
```typescript {5}
import { Update, EditedMessage, Context } from 'nestjs-telegraf';
@Update()
export class SomeBotService {
@EditedMessage()
editedMessage(ctx: Context) {
...
}
}
```
### ChannelPost
Use `@ChannelPost` method decorator for handling new incoming channel post of any kind — text, photo, sticker, etc.
```typescript {5}
import { Update, ChannelPost, Context } from 'nestjs-telegraf';
@Update()
export class SomeBotService {
@ChannelPost()
channelPost(ctx: Context) {
...
}
}
```
### EditedChannelPost
Use `@EditedChannelPost` method decorator for handling new version of a channel post that is known to the bot and was edited.
```typescript {5}
import { Update, EditedChannelPost, Context } from 'nestjs-telegraf';
@Update()
export class SomeBotService {
@EditedChannelPost()
editedChannelPost(ctx: Context) {
...
}
}
```
### InlineQuery
Use `@InlineQuery` method decorator for handling new incoming inline query.
```typescript {5}
import { Update, InlineQuery, Context } from 'nestjs-telegraf';
@Update()
export class SomeBotService {
@InlineQuery()
inlineQuery(ctx: Context) {
...
}
}
```
The `@InlineQuery` decorator can take a `triggers` option to handle inline query with specific value.
```typescript {6}
import { Update, InlineQuery, Context } from 'nestjs-telegraf';
@Update()
export class SomeBotService {
@InlineQuery({
triggers: 'trigger' // string/string[]/RegEx/RegEx[]
})
inlineQuery(ctx: Context) {
...
}
}
```
### ChosenInlineResult
Use `@ChosenInlineResult` method decorator for handling result of an inline query that was chosen by a user and sent to their chat partner.
```typescript {5}
import { Update, ChosenInlineResult, Context } from 'nestjs-telegraf';
@Update()
export class SomeBotService {
@ChosenInlineResult()
chosenInlineResult(ctx: Context) {
...
}
}
```
### CallbackQuery
Use `@CallbackQuery` method decorator for handling new incoming callback query.
```typescript {5}
import { Update, CallbackQuery, Context } from 'nestjs-telegraf';
@Update()
export class SomeBotService {
@CallbackQuery()
callbackQuery(ctx: Context) {
...
}
}
```
### ShippingQuery
Use `@ShippingQuery` method decorator for handling new incoming shipping query. Only for invoices with flexible price.
```typescript {5}
import { Update, ShippingQuery, Context } from 'nestjs-telegraf';
@Update()
export class SomeBotService {
@ShippingQuery()
shippingQuery(ctx: Context) {
...
}
}
```
### PreCheckoutQuery
Use `@PreCheckoutQuery` method decorator for handling new incoming pre-checkout query. Contains full information about checkout.
```typescript {5}
import { Update, PreCheckoutQuery, Context } from 'nestjs-telegraf';
@Update()
export class SomeBotService {
@PreCheckoutQuery()
preCheckoutQuery(ctx: Context) {
...
}
}
```

View File

@@ -1,17 +0,0 @@
---
id: bot-injection
title: Bot injection
sidebar_label: Bot injection
slug: /bot-injection
---
At times you may need to access the native `Telegraf` instance. For example, you may want to connect stage middleware. You can inject the Telegraf by using the `@InjectBot()` decorator as follows:
```typescript
import { Injectable } from '@nestjs/common';
import { InjectBot, TelegrafProvider, Context } from 'nestjs-telegraf';
@Injectable()
export class BotSettingsService {
constructor(@InjectBot() private bot: TelegrafProvider<Context>) {}
}
```

View File

@@ -1,31 +0,0 @@
---
id: error-handling
title: Error handling
sidebar_label: Error handling
slug: /error-handling
---
By default, `nestjs-telegraf` catches all errors using the `Logger` built into NestJS.
Use can disable global errors catch with `disableGlobalCatch`:
```typescript
TelegrafModule.forRoot({
disableGlobalCatch: true,
}),
```
After that you can override errors handling with bot instance `catch` function.
```typescript
import { Injectable } from '@nestjs/common';
import { InjectBot, TelegrafProvider, Context } from 'nestjs-telegraf';
@Injectable()
export class BotSettingsService {
constructor(@InjectBot() private bot: TelegrafProvider<Context>) {
this.bot.catch((err, ctx) => {
console.log(`Ooops, encountered an error for ${ctx.updateType}`, err);
});
}
}
```

View File

@@ -0,0 +1,36 @@
---
id: bot-injection
title: Bot injection
sidebar_label: Bot injection
slug: /extras/bot-injection
---
At times you may need to access the native `Telegraf` instance. You can inject the Telegraf by using the `@InjectBot()` decorator as follows:
```typescript {8} title="src/echo/echo.service.ts"
import { Injectable } from '@nestjs/common';
import { InjectBot } from 'nestjs-telegraf';
import { Telegraf } from 'telegraf';
import { TelegrafContext } from '../common/interfaces/telegraf-context.interface.ts';
@Injectable()
export class EchoService {
constructor(@InjectBot() private bot: Telegraf<TelegrafContext>) {}
...
}
```
If you run [multiple bots](/docs/extras/multiple-bots) in the same application, explicitly specify the bot name:
```typescript {8} title="src/echo/echo.service.ts"
import { Injectable } from '@nestjs/common';
import { InjectBot } from 'nestjs-telegraf';
import { Telegraf } from 'telegraf';
import { TelegrafContext } from '../common/interfaces/telegraf-context.interface.ts';
@Injectable()
export class EchoService {
constructor(@InjectBot('cats') private bot: Telegraf<TelegrafContext>) {}
...
}
```

View File

@@ -2,7 +2,7 @@
id: middlewares
title: Middlewares
sidebar_label: Middlewares
slug: /middlewares
slug: /extras/middlewares
---
`nestjs-telegraf` has support of the Telegraf middleware packages. To use an existing middleware package, simply import it and add it to the middlewares array:

View File

@@ -2,7 +2,7 @@
id: multiple-bots
title: Multiple bots
sidebar_label: Multiple bots
slug: /multiple-bots
slug: /extras/multiple-bots
---
In some cases, you may need to run multiple bots at the same time. This can also be achieved with this module. To work with multiple bots, first create the bots. In this case, bot naming becomes mandatory.

View File

@@ -6,14 +6,12 @@ slug: /
---
```bash
$ npm i nestjs-telegraf
$ npm i nestjs-telegraf telegraf
```
Once the installation process is complete, we can import the TelegrafModule into the root AppModule.
```typescript
/* app.module.ts */
Once the installation process is complete, we can import the `TelegrafModule` into the root `AppModule`.
```typescript title="src/app.module.ts"
import { Module } from '@nestjs/common';
import { TelegrafModule } from 'nestjs-telegraf';

View File

@@ -0,0 +1,41 @@
---
id: from-v1-to-v2
title: From v1 to v2
sidebar_label: From v1 to v2
slug: /migrating/from-v1-to-v2
---
## Remove `Telegraf` prefix
If you previously used decorators with the prefix `Telegraf` in the decorator name (such as `@TelegrafOn()` or `@TelegrafHelp()`) replace them with the same decorators but without the prefix `Telegraf`, such as `@On()`, `@Start()`, `@Command()` and so on.
## `@Update()` decorator
Since v2, `nestjs-telegraf` looks for all update handlers only inside individual classes, under the `@Update()` decorator.
Previously, you could declare a handler anywhere, for example:
```typescript title="src/cats/cats.provider.ts"
import { Injectable } from '@nestjs/common';
import { Command } from 'nestjs-telegraf';
@Injectable()
export class CatsProvider {
@Command('cats')
async helpCommand(ctx: TelegrafContext) {
await ctx.reply('Meow.');
}
}
```
Now you must explicitly bind the class, for Telegram Bot Api update handlers:
```typescript {3} title="src/cats/cats.updates.ts"
import { Update, Ctx } from 'nestjs-telegraf';
@Update()
export class HelpUpdate {
@Command('help')
async helpCommand(@Ctx() ctx: TelegrafContext) {
await ctx.reply('Help command.');
}
}
```
Treat the `@Update()` decorator like the `@Controller()` decorator, but to capture Telegram Bot Api updates.

View File

@@ -5,42 +5,41 @@ sidebar_label: Telegraf methods
slug: /telegraf-methods
---
Each Telegraf instance method described [here](https://telegraf.js.org/#/?id=telegraf) has own decorator in `nestjs-telegraf` package. The name of the decorator corresponds to the name of the Telegraf method. For example [`@Hears`](https://telegraf.js.org/#/?id=hears), [`@On`](https://telegraf.js.org/#/?id=on), [`@Action`](https://telegraf.js.org/#/?id=action) and so on.
Each Telegraf instance method has own decorator in `nestjs-telegraf` package. The name of the decorator corresponds to the name of the Telegraf method. For example [`@Hears`](https://telegraf.js.org/classes/telegraf.html#hears), [`@On`](https://telegraf.js.org/classes/telegraf.html#on), [`@Action`](https://telegraf.js.org/classes/telegraf.html#action) and so on.
Now let's try to repeat the example from the Telegraf [documentation page](https://telegraf.js.org/#/?id=example).
Now let's try simple example:
```typescript
/* app.service.ts */
import { Injectable } from '@nestjs/common';
```typescript title="src/app.update.ts"
import {
Update,
Ctx,
Start,
Help,
On,
Hears,
} from 'nestjs-telegraf';
import { Context } from 'telegraf';
import { TelegrafContext } from './common/interfaces/telegraf-context.interface.ts';
@Injectable()
export class AppService {
@Update()
export class AppUpdate {
@Start()
start(ctx: Context) {
ctx.reply('Welcome');
async start(@Ctx() ctx: TelegrafContext) {
await ctx.reply('Welcome');
}
@Help()
help(ctx: Context) {
ctx.reply('Send me a sticker');
async help(@Ctx() ctx: TelegrafContext) {
await ctx.reply('Send me a sticker');
}
@On('sticker')
on(ctx: Context) {
ctx.reply('👍');
async on(@Ctx() ctx: TelegrafContext) {
await ctx.reply('👍');
}
@Hears('hi')
hears(ctx: Context) {
ctx.reply('Hey there');
async hears(@Ctx() ctx: TelegrafContext) {
await ctx.reply('Hey there');
}
}
```