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

1
.husky/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
_

View File

@ -15,7 +15,7 @@ This package uses the best of the NodeJS world under the hood. [Telegraf](https:
- Scenes support.
- Telegraf plugins and custom plugins support.
- Ability to run multiple bots simultaneously.
- Full support of NestJS guards, interceptors, filters and pipes! (*in progress...*)
- Full support of NestJS guards, interceptors, filters and pipes!
## Documentation
If you want to dive fully into NestJS Telegraf then don't waste your time in this dump, check out the [documentation site](https://nestjs-telegraf.vercel.app).

5
package-lock.json generated
View File

@ -1,11 +1,11 @@
{
"name": "nestjs-telegraf",
"version": "2.0.0-beta.4",
"version": "2.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"version": "2.0.0-beta.4",
"version": "2.0.0",
"funding": [
{
"type": "individual",
@ -2130,6 +2130,7 @@
"minimist": "^1.2.5",
"neo-async": "^2.6.0",
"source-map": "^0.6.1",
"uglify-js": "^3.1.4",
"wordwrap": "^1.0.0"
},
"bin": {

View File

@ -1,6 +1,6 @@
{
"name": "nestjs-telegraf",
"version": "2.0.0-beta.4",
"version": "2.0.0",
"description": "Telegraf module for NestJS",
"keywords": [
"nest",

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');
}
}
```

View File

@ -35,52 +35,44 @@ module.exports = {
},
footer: {
style: 'dark',
// links: [
// {
// title: 'Docs',
// items: [
// {
// label: 'Style Guide',
// to: 'docs/',
// },
// {
// label: 'Second Doc',
// to: 'docs/doc2/',
// },
// ],
// },
// {
// title: 'Community',
// items: [
// {
// label: 'Stack Overflow',
// href: 'https://stackoverflow.com/questions/tagged/docusaurus',
// },
// {
// label: 'Discord',
// href: 'https://discordapp.com/invite/docusaurus',
// },
// {
// label: 'Twitter',
// href: 'https://twitter.com/docusaurus',
// },
// ],
// },
// {
// title: 'More',
// items: [
// {
// label: 'Blog',
// to: 'blog',
// },
// {
// label: 'GitHub',
// href: 'https://github.com/facebook/docusaurus',
// },
// ],
// },
// ],
copyright: `Copyright © ${new Date().getFullYear()} NestJS Telegraf.`,
links: [
{
title: 'Docs',
items: [
{
label: 'Getting Started',
to: 'docs/',
},
],
},
{
title: 'Community',
items: [
{
label: 'Discussions',
href: 'https://github.com/bukhalo/nestjs-telegraf/discussions',
},
{
label: 'Telegram',
href: 'https://t.me/nestjs_telegraf',
},
],
},
{
title: 'More',
items: [
{
label: 'Issues',
href: 'https://github.com/bukhalo/nestjs-telegraf/issues',
},
{
label: 'Examples',
to: 'https://github.com/bukhalo/nestjs-telegraf/tree/master/sample/',
}
],
},
],
copyright: `Copyright © 2019 - ${new Date().getFullYear()}, <a target="_blank" href="https://bukhalo.com">Alexander Bukhalo</a> & <a target="_blank" href="mailto:arthur.asimov.z0@gmail.com">Arthur Asimov</a>.`,
},
},
presets: [
@ -89,7 +81,6 @@ module.exports = {
{
docs: {
sidebarPath: require.resolve('./sidebars.js'),
// Please change this to your repo.
editUrl:
'https://github.com/bukhalo/nestjs-telegraf/edit/master/website/',
showLastUpdateAuthor: true,

View File

@ -4,13 +4,14 @@ module.exports = {
'installation',
'getting-updates',
'telegraf-methods',
'bot-injection',
'async-configuration',
'multiple-bots',
'middlewares',
'error-handling',
],
Extras: ['extras/standalone-applications'],
'API Reference': ['api-reference/decorators'],
Extras: [
'extras/bot-injection',
'extras/middlewares',
'extras/multiple-bots',
'extras/standalone-applications',
],
'Migrating': ['migrating/from-v1-to-v2'],
},
};

View File

@ -7,14 +7,14 @@
/* You can override the default Infima variables here. */
:root {
--ifm-color-primary: #25c2a0;
--ifm-color-primary-dark: rgb(33, 175, 144);
--ifm-color-primary-darker: rgb(31, 165, 136);
--ifm-color-primary-darkest: rgb(26, 136, 112);
--ifm-color-primary-light: rgb(70, 203, 174);
--ifm-color-primary-lighter: rgb(102, 212, 189);
--ifm-color-primary-lightest: rgb(146, 224, 208);
--ifm-code-font-size: 95%;
--ifm-color-primary: #3578e5;
--ifm-color-primary-dark: #1d68e1;
--ifm-color-primary-darker: #1b62d4;
--ifm-color-primary-darkest: #1751af;
--ifm-color-primary-light: #4e89e8;
--ifm-color-primary-lighter: #5a91ea;
--ifm-color-primary-lightest: #80aaef;
--ifm-code-font-size: 90%;
}
.docusaurus-highlight-code-line {

View File

@ -0,0 +1,60 @@
---
id: async-configuration
title: Async configuration
sidebar_label: Async configuration
slug: /async-configuration
---
When you need to pass module options asynchronously instead of statically, use the forRootAsync() method. As with most dynamic modules, Nest provides several techniques to deal with async configuration.
One technique is to use a factory function:
```typescript
TelegrafModule.forRootAsync({
useFactory: () => ({
token: 'TELEGRAM_BOT_TOKEN',
}),
});
```
Like other [factory providers](https://docs.nestjs.com/fundamentals/custom-providers#factory-providers-usefactory), our factory function can be async and can inject dependencies through inject.
```typescript
TelegrafModule.forRootAsync({
imports: [ConfigModule.forFeature(telegrafModuleConfig)],
useFactory: async (configService: ConfigService) => ({
token: configService.get<string>('TELEGRAM_BOT_TOKEN'),
}),
inject: [ConfigService],
});
```
Alternatively, you can configure the TelegrafModule using a class instead of a factory, as shown below:
```typescript
TelegrafModule.forRootAsync({
useClass: TelegrafConfigService,
});
```
The construction above instantiates `TelegrafConfigService` inside `TelegrafModule`, using it to create the required options object. Note that in this example, the `TelegrafConfigService` has to implement the `TelegrafOptionsFactory` interface, as shown below. The `TelegrafModule` will call the `createTelegrafOptions()` method on the instantiated object of the supplied class.
```typescript
@Injectable()
class TelegrafConfigService implements TelegrafOptionsFactory {
createTelegrafOptions(): TelegrafModuleOptions {
return {
token: 'TELEGRAM_BOT_TOKEN',
};
}
}
```
If you want to reuse an existing options provider instead of creating a private copy inside the `TelegrafModule`, use the `useExisting` syntax.
```typescript
TelegrafModule.forRootAsync({
imports: [ConfigModule.forFeature(telegrafModuleConfig)],
useExisting: ConfigService,
});
```

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

@ -0,0 +1,13 @@
---
id: middlewares
title: Middlewares
sidebar_label: 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:
```typescript
TelegrafModule.forRoot({
middlewares: [session()],
}),
```

View File

@ -0,0 +1,74 @@
---
id: multiple-bots
title: Multiple bots
sidebar_label: 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.
```typescript
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TelegrafModule } from 'nestjs-telegraf';
@Module({
imports: [
ConfigModule.forRoot(),
TelegrafModule.forRootAsync({
imports: [ConfigModule],
botName: 'cat',
useFactory: (configService: ConfigService) => ({
token: configService.get<string>('CAT_BOT_TOKEN'),
}),
inject: [ConfigService],
}),
TelegrafModule.forRootAsync({
imports: [ConfigModule.forFeature(telegrafModuleConfig)],
botName: 'dog',
useFactory: async (configService: ConfigService) => ({
token: configService.get<string>('DOG_BOT_TOKEN'),
}),
inject: [ConfigService],
}),
],
})
export class AppModule {}
```
:::caution
Please note that you shouldn't have multiple bots without a name, or with the same name, otherwise they will get overridden.
:::
You can also inject the `Bot` for a given bot:
```typescript
import { Injectable } from '@nestjs/common';
import { InjectBot, Telegraf, Context } from 'nestjs-telegraf';
@Injectable()
export class EchoService {
constructor(@InjectBot('cat') private catBot: Telegraf<Context>) {}
}
```
To inject a given `Bot` to a custom provider (for example, factory provider), use the `getBotToken()` function passing the name of the bot as an argument.
```typescript
{
provide: CatsService,
useFactory: (catBot: Telegraf<Context>) => {
return new CatsService(catBot);
},
inject: [getBotToken('cat')],
}
```
Another useful feature of the `nestjs-telegraf` module is the ability to choose which modules should handle updates for each launched bot. By default, module searches for handlers throughout the whole app. To limit this scan to only a subset of modules, use the include property.
```typescript
TelegrafModule.forRootAsync({
imports: [ConfigModule],
botName: 'cat',
useFactory: (configService: ConfigService) => ({
token: configService.get<string>('CAT_BOT_TOKEN'),
include: [CatsModule],
}),
inject: [ConfigService],
}),
```

View File

@ -0,0 +1,27 @@
---
id: standalone-applications
title: Standalone applications
sidebar_label: Standalone applications
slug: standalone-applications
---
If you initialized your application with the [Nest CLI](https://docs.nestjs.com/cli/overview), [Express](https://expressjs.com/) framework will be installed by default along with Nest. Nest and NestJS Telegraf does not require Express for work. So if you don't plan to getting bot updates through webhooks, and you don't need a web server, you can remove Express.
To do this, change the `bootstrap` function in the `main.ts` file of your project on something like that:
```typescript
async function bootstrap() {
const app = await NestFactory.createApplicationContext(AppModule);
}
bootstrap();
```
This initializes Nest as a **standalone application** (without any network listeners).
All that remains is to remove unused dependencies:
```bash
npm un @nestjs/platform-express @types/express
```
:::info
More information about standalone applications located at [Nest documentation](https://docs.nestjs.com/standalone-applications)
:::

View File

@ -0,0 +1,44 @@
---
id: getting-updates
title: Getting updates
sidebar_label: Getting updates
slug: getting-updates
---
## Long polling
By default, the bot receives updates using long-polling and requires no additional action.
## Webhooks
If you want to configure a telegram bot webhook, you need to get a middleware via `getBotToken` helper in your `main.ts` file.
To access it, you must use the `app.get()` method, followed by the provider reference:
```typescript
import { getBotToken } from 'nestjs-telegraf';
// ...
const bot = app.get(getBotToken());
```
Now you can connect middleware:
```typescript
app.use(bot.webhookCallback('/secret-path'));
```
The last step is to specify launchOptions in `forRoot` method:
```typescript
TelegrafModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
token: configService.get<string>('TELEGRAM_BOT_TOKEN'),
launchOptions: {
webhook: {
domain: 'domain.tld',
hookPath: '/secret-path',
}
}
}),
inject: [ConfigService],
});
```

View File

@ -0,0 +1,28 @@
---
id: installation
title: Installation
sidebar_label: Installation
slug: /
---
```bash
$ npm i nestjs-telegraf telegraf
```
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';
@Module({
imports: [
TelegrafModule.forRoot({
token: 'TELEGRAM_BOT_TOKEN',
})
],
})
export class AppModule {}
```
The `forRoot()` method accepts the same configuration object as Telegraf class constructor from the Telegraf package, as described [here](https://telegraf.js.org/#/?id=constructor).

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

@ -0,0 +1,45 @@
---
id: telegraf-methods
title: Telegraf methods
sidebar_label: Telegraf methods
slug: /telegraf-methods
---
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 simple example:
```typescript title="src/app.update.ts"
import {
Update,
Ctx,
Start,
Help,
On,
Hears,
} from 'nestjs-telegraf';
import { TelegrafContext } from './common/interfaces/telegraf-context.interface.ts';
@Update()
export class AppUpdate {
@Start()
async start(@Ctx() ctx: TelegrafContext) {
await ctx.reply('Welcome');
}
@Help()
async help(@Ctx() ctx: TelegrafContext) {
await ctx.reply('Send me a sticker');
}
@On('sticker')
async on(@Ctx() ctx: TelegrafContext) {
await ctx.reply('👍');
}
@Hears('hi')
async hears(@Ctx() ctx: TelegrafContext) {
await ctx.reply('Hey there');
}
}
```

View File

@ -0,0 +1,61 @@
{
"version-2.0.0/docs": [
{
"collapsed": true,
"type": "category",
"label": "Getting Started",
"items": [
{
"type": "doc",
"id": "version-2.0.0/installation"
},
{
"type": "doc",
"id": "version-2.0.0/getting-updates"
},
{
"type": "doc",
"id": "version-2.0.0/telegraf-methods"
},
{
"type": "doc",
"id": "version-2.0.0/async-configuration"
}
]
},
{
"collapsed": true,
"type": "category",
"label": "Extras",
"items": [
{
"type": "doc",
"id": "version-2.0.0/extras/bot-injection"
},
{
"type": "doc",
"id": "version-2.0.0/extras/middlewares"
},
{
"type": "doc",
"id": "version-2.0.0/extras/multiple-bots"
},
{
"type": "doc",
"id": "version-2.0.0/extras/standalone-applications"
}
]
},
{
"collapsed": true,
"type": "category",
"label": "Migrating",
"items": [
{
"type": "doc",
"id": "version-2.0.0/migrating/from-v1-to-v2"
}
]
}
]
}

View File

@ -1,4 +1,5 @@
[
"2.0.0",
"1.3.0",
"1.2.1"
]