mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-10-16 16:46:49 +03:00
move from docusaurus to vite
This commit is contained in:
30
docs/extras/bot-injection.md
Normal file
30
docs/extras/bot-injection.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# 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](/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>) {}
|
||||
...
|
||||
}
|
||||
```
|
7
docs/extras/middlewares.md
Normal file
7
docs/extras/middlewares.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# 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()],
|
||||
}),
|
||||
```
|
68
docs/extras/multiple-bots.md
Normal file
68
docs/extras/multiple-bots.md
Normal file
@@ -0,0 +1,68 @@
|
||||
# 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],
|
||||
}),
|
||||
```
|
21
docs/extras/standalone-applications.md
Normal file
21
docs/extras/standalone-applications.md
Normal file
@@ -0,0 +1,21 @@
|
||||
# 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)
|
||||
:::
|
Reference in New Issue
Block a user