mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-09-23 09:49:06 +03:00
merge refactor/v2
This commit is contained in:
@@ -8,11 +8,10 @@ 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 } from 'nestjs-telegraf';
|
||||
import { Telegraf } from 'telegraf';
|
||||
import { InjectBot, TelegrafProvider, Context } from 'nestjs-telegraf';
|
||||
|
||||
@Injectable()
|
||||
export class BotSettingsService {
|
||||
constructor(@InjectBot() private bot: Telegraf) {}
|
||||
constructor(@InjectBot() private bot: TelegrafProvider<Context>) {}
|
||||
}
|
||||
```
|
||||
|
31
website/docs/error-handling.md
Normal file
31
website/docs/error-handling.md
Normal file
@@ -0,0 +1,31 @@
|
||||
---
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
```
|
27
website/docs/extras/standalone-applications.md
Normal file
27
website/docs/extras/standalone-applications.md
Normal 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)
|
||||
:::
|
@@ -1,28 +1,36 @@
|
||||
---
|
||||
id: webhooks
|
||||
title: Webhooks
|
||||
sidebar_label: Webhooks
|
||||
slug: webhooks
|
||||
id: getting-updates
|
||||
title: Getting updates
|
||||
sidebar_label: Getting updates
|
||||
slug: getting-updates
|
||||
---
|
||||
|
||||
If you want to configure a telegram bot webhook, you need to get a middleware from `TelegrafProvider` for connect it in your `main.ts` file.
|
||||
|
||||
## 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 { Telegraf } from 'telegraf';
|
||||
const telegraf = app.get(Telegraf);
|
||||
import { getBotToken } from 'nestjs-telegraf';
|
||||
|
||||
// ...
|
||||
const bot = app.get(getBotToken());
|
||||
```
|
||||
|
||||
Now you can connect middleware:
|
||||
```typescript
|
||||
app.use(telegraf.webhookCallback('/secret-path'));
|
||||
app.use(bot.webhookCallback('/secret-path'));
|
||||
```
|
||||
|
||||
The last step is to specify launchOptions in `forRoot` method:
|
||||
```typescript
|
||||
TelegrafModule.forRootAsync({
|
||||
imports: [ConfigModule.forFeature(telegrafModuleConfig)],
|
||||
useFactory: async (configService: ConfigService) => ({
|
||||
imports: [ConfigModule],
|
||||
useFactory: (configService: ConfigService) => ({
|
||||
token: configService.get<string>('TELEGRAM_BOT_TOKEN'),
|
||||
launchOptions: {
|
||||
webhook: {
|
13
website/docs/middlewares.md
Normal file
13
website/docs/middlewares.md
Normal file
@@ -0,0 +1,13 @@
|
||||
---
|
||||
id: middlewares
|
||||
title: Middlewares
|
||||
sidebar_label: Middlewares
|
||||
slug: /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()],
|
||||
}),
|
||||
```
|
74
website/docs/multiple-bots.md
Normal file
74
website/docs/multiple-bots.md
Normal file
@@ -0,0 +1,74 @@
|
||||
---
|
||||
id: multiple-bots
|
||||
title: Multiple bots
|
||||
sidebar_label: Multiple bots
|
||||
slug: /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],
|
||||
}),
|
||||
```
|
23947
website/package-lock.json
generated
23947
website/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -11,12 +11,12 @@
|
||||
"serve": "docusaurus serve"
|
||||
},
|
||||
"dependencies": {
|
||||
"@docusaurus/core": "2.0.0-alpha.63",
|
||||
"@docusaurus/preset-classic": "2.0.0-alpha.63",
|
||||
"@mdx-js/react": "^1.5.8",
|
||||
"clsx": "^1.1.1",
|
||||
"react": "^16.8.4",
|
||||
"react-dom": "^16.8.4"
|
||||
"@docusaurus/core": "2.0.0-alpha.70",
|
||||
"@docusaurus/preset-classic": "2.0.0-alpha.70",
|
||||
"@mdx-js/react": "1.6.22",
|
||||
"clsx": "1.1.1",
|
||||
"react": "17.0.1",
|
||||
"react-dom": "17.0.1"
|
||||
},
|
||||
"browserslist": {
|
||||
"production": [
|
||||
@@ -31,11 +31,11 @@
|
||||
]
|
||||
},
|
||||
"devDependencies": {
|
||||
"@docusaurus/module-type-aliases": "^2.0.0-alpha.51",
|
||||
"@tsconfig/docusaurus": "^1.0.2",
|
||||
"@types/react": "^16.9.49",
|
||||
"@types/react-helmet": "^6.1.0",
|
||||
"@types/react-router-dom": "^5.1.5",
|
||||
"typescript": "^4.0.2"
|
||||
"@docusaurus/module-type-aliases": "2.0.0-alpha.f37987f32",
|
||||
"@tsconfig/docusaurus": "1.0.2",
|
||||
"@types/react": "17.0.0",
|
||||
"@types/react-helmet": "6.1.0",
|
||||
"@types/react-router-dom": "5.1.7",
|
||||
"typescript": "4.1.3"
|
||||
}
|
||||
}
|
||||
|
@@ -2,11 +2,15 @@ module.exports = {
|
||||
docs: {
|
||||
'Getting Started': [
|
||||
'installation',
|
||||
'getting-updates',
|
||||
'telegraf-methods',
|
||||
'bot-injection',
|
||||
'async-configuration',
|
||||
'webhooks',
|
||||
'multiple-bots',
|
||||
'middlewares',
|
||||
'error-handling',
|
||||
],
|
||||
Extras: ['extras/standalone-applications'],
|
||||
'API Reference': ['api-reference/decorators'],
|
||||
},
|
||||
};
|
||||
|
Reference in New Issue
Block a user