mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-23 14:42:59 +03:00
update docs
This commit is contained in:
parent
7845196cb8
commit
40490c3f3b
16
README.md
16
README.md
@ -81,3 +81,19 @@ export class AppUpdate {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Telegraf instance access
|
||||
If you want to use `Telegraf` instance directly, you can use `@InjectBot` for that.
|
||||
```typescript
|
||||
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>) {}
|
||||
...
|
||||
}
|
||||
```
|
||||
See more on a docs page: https://nestjs-telegraf.vercel.app/docs/extras/bot-injection
|
@ -1,33 +0,0 @@
|
||||
# Website
|
||||
|
||||
This website is built using [Docusaurus 2](https://v2.docusaurus.io/), a modern static website generator.
|
||||
|
||||
### Installation
|
||||
|
||||
```
|
||||
$ yarn
|
||||
```
|
||||
|
||||
### Local Development
|
||||
|
||||
```
|
||||
$ yarn start
|
||||
```
|
||||
|
||||
This command starts a local development server and open up a browser window. Most changes are reflected live without having to restart the server.
|
||||
|
||||
### Build
|
||||
|
||||
```
|
||||
$ yarn build
|
||||
```
|
||||
|
||||
This command generates static content into the `build` directory and can be served using any static contents hosting service.
|
||||
|
||||
### Deployment
|
||||
|
||||
```
|
||||
$ GIT_USER=<Your GitHub username> USE_SSH=true yarn deploy
|
||||
```
|
||||
|
||||
If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch.
|
@ -5,27 +5,12 @@ module.exports = {
|
||||
baseUrl: '/',
|
||||
onBrokenLinks: 'throw',
|
||||
favicon: 'img/favicon.ico',
|
||||
organizationName: 'bukhalo', // Usually your GitHub org/user name.
|
||||
projectName: 'nestjs-telegraf', // Usually your repo name.
|
||||
organizationName: 'bukhalo',
|
||||
projectName: 'nestjs-telegraf',
|
||||
themeConfig: {
|
||||
navbar: {
|
||||
title: 'NestJS Telegraf',
|
||||
// logo: {
|
||||
// alt: 'My Site Logo',
|
||||
// src: 'img/logo.svg',
|
||||
// },
|
||||
items: [
|
||||
{
|
||||
type: 'docsVersionDropdown',
|
||||
position: 'left',
|
||||
},
|
||||
{
|
||||
to: 'docs/',
|
||||
activeBasePath: 'docs',
|
||||
label: 'Docs',
|
||||
position: 'left',
|
||||
},
|
||||
// {to: 'blog', label: 'Blog', position: 'left'},
|
||||
{
|
||||
href: 'https://github.com/bukhalo/nestjs-telegraf',
|
||||
label: 'GitHub',
|
||||
@ -41,7 +26,7 @@ module.exports = {
|
||||
items: [
|
||||
{
|
||||
label: 'Getting Started',
|
||||
to: 'docs/',
|
||||
to: '/',
|
||||
},
|
||||
],
|
||||
},
|
||||
@ -72,7 +57,7 @@ module.exports = {
|
||||
],
|
||||
},
|
||||
],
|
||||
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>.`,
|
||||
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> and <a target="_blank" href="https://github.com/bukhalo/nestjs-telegraf/graphs/contributors">Others</a>.`,
|
||||
},
|
||||
},
|
||||
presets: [
|
||||
@ -81,17 +66,12 @@ module.exports = {
|
||||
{
|
||||
docs: {
|
||||
sidebarPath: require.resolve('./sidebars.js'),
|
||||
routeBasePath: '/',
|
||||
editUrl:
|
||||
'https://github.com/bukhalo/nestjs-telegraf/edit/master/website/',
|
||||
showLastUpdateAuthor: true,
|
||||
showLastUpdateTime: true,
|
||||
},
|
||||
// blog: {
|
||||
// showReadingTime: true,
|
||||
// // Please change this to your repo.
|
||||
// editUrl:
|
||||
// 'https://github.com/bukhalo/nestjs-telegraf/edit/master/website/blog/',
|
||||
// },
|
||||
theme: {
|
||||
customCss: require.resolve('./src/css/custom.css'),
|
||||
},
|
||||
|
@ -1,98 +0,0 @@
|
||||
import React from 'react';
|
||||
import clsx from 'clsx';
|
||||
import Layout from '@theme/Layout';
|
||||
import Link from '@docusaurus/Link';
|
||||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
||||
import useBaseUrl from '@docusaurus/useBaseUrl';
|
||||
import styles from './styles.module.css';
|
||||
|
||||
const features = [
|
||||
{
|
||||
title: 'Easy to Use',
|
||||
imageUrl: 'img/undraw_docusaurus_mountain.svg',
|
||||
description: (
|
||||
<>
|
||||
Docusaurus was designed from the ground up to be easily installed and
|
||||
used to get your website up and running quickly.
|
||||
</>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: 'Focus on What Matters',
|
||||
imageUrl: 'img/undraw_docusaurus_tree.svg',
|
||||
description: (
|
||||
<>
|
||||
Docusaurus lets you focus on your docs, and we'll do the chores. Go
|
||||
ahead and move your docs into the <code>docs</code> directory.
|
||||
</>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: 'Powered by React',
|
||||
imageUrl: 'img/undraw_docusaurus_react.svg',
|
||||
description: (
|
||||
<>
|
||||
Extend or customize your website layout by reusing React. Docusaurus can
|
||||
be extended while reusing the same header and footer.
|
||||
</>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
function Feature({imageUrl, title, description}) {
|
||||
const imgUrl = useBaseUrl(imageUrl);
|
||||
return (
|
||||
<div className={clsx('col col--4', styles.feature)}>
|
||||
{imgUrl && (
|
||||
<div className="text--center">
|
||||
<img className={styles.featureImage} src={imgUrl} alt={title} />
|
||||
</div>
|
||||
)}
|
||||
<h3>{title}</h3>
|
||||
<p>{description}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function Home() {
|
||||
const context = useDocusaurusContext();
|
||||
const {siteConfig = {}} = context;
|
||||
return (
|
||||
<Layout
|
||||
title={`Hello from ${siteConfig.title}`}
|
||||
description="Description will go into a meta tag in <head />">
|
||||
<header className={clsx('hero hero--primary', styles.heroBanner)}>
|
||||
<div className="container">
|
||||
<h1 className="hero__title">{siteConfig.title}</h1>
|
||||
<p className="hero__subtitle">{siteConfig.tagline}</p>
|
||||
<div className={styles.buttons}>
|
||||
<Link
|
||||
className={clsx(
|
||||
'button button--outline button--secondary button--lg',
|
||||
styles.getStarted,
|
||||
)}
|
||||
to={useBaseUrl('docs/')}>
|
||||
Get Started
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
<main>
|
||||
{/* {features && features.length > 0 && (
|
||||
<section className={styles.features}>
|
||||
<div className="container">
|
||||
<div className="row">
|
||||
{features.map((props, idx) => (
|
||||
<Feature key={idx} {...props} />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
)}
|
||||
*/}
|
||||
</main>
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
|
||||
export default Home;
|
@ -1,37 +0,0 @@
|
||||
/* stylelint-disable docusaurus/copyright-header */
|
||||
|
||||
/**
|
||||
* CSS files with the .module.css suffix will be treated as CSS modules
|
||||
* and scoped locally.
|
||||
*/
|
||||
|
||||
.heroBanner {
|
||||
padding: 4rem 0;
|
||||
text-align: center;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
@media screen and (max-width: 966px) {
|
||||
.heroBanner {
|
||||
padding: 2rem;
|
||||
}
|
||||
}
|
||||
|
||||
.buttons {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.features {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 2rem 0;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.featureImage {
|
||||
height: 200px;
|
||||
width: 200px;
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
---
|
||||
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,
|
||||
});
|
||||
```
|
@ -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 } from 'nestjs-telegraf';
|
||||
|
||||
@Injectable()
|
||||
export class BotSettingsService {
|
||||
constructor(@InjectBot() private bot: TelegrafProvider) {}
|
||||
}
|
||||
```
|
@ -1,30 +0,0 @@
|
||||
---
|
||||
id: installation
|
||||
title: Installation
|
||||
sidebar_label: Installation
|
||||
slug: /
|
||||
---
|
||||
|
||||
```bash
|
||||
$ npm i nestjs-telegraf
|
||||
```
|
||||
|
||||
Once the installation process is complete, we can import the TelegrafModule into the root AppModule.
|
||||
|
||||
```typescript
|
||||
/* 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).
|
@ -1,46 +0,0 @@
|
||||
---
|
||||
id: telegraf-methods
|
||||
title: Telegraf methods
|
||||
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 [`@TelegrafHears`](https://telegraf.js.org/#/?id=hears), [`@TelegrafOn`](https://telegraf.js.org/#/?id=on), [`@TelegrafAction`](https://telegraf.js.org/#/?id=action) and so on.
|
||||
|
||||
Now let's try to repeat the example from the Telegraf [documentation page](https://telegraf.js.org/#/?id=example).
|
||||
|
||||
```typescript
|
||||
/* app.service.ts */
|
||||
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import {
|
||||
TelegrafStart,
|
||||
TelegrafHelp,
|
||||
TelegrafOn,
|
||||
TelegrafHears,
|
||||
Context,
|
||||
} from 'nestjs-telegraf';
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
@TelegrafStart()
|
||||
start(ctx: Context) {
|
||||
ctx.reply('Welcome');
|
||||
}
|
||||
|
||||
@TelegrafHelp()
|
||||
help(ctx: Context) {
|
||||
ctx.reply('Send me a sticker');
|
||||
}
|
||||
|
||||
@TelegrafOn('sticker')
|
||||
on(ctx: Context) {
|
||||
ctx.reply('👍');
|
||||
}
|
||||
|
||||
@TelegrafHears('hi')
|
||||
hears(ctx: Context) {
|
||||
ctx.reply('Hey there');
|
||||
}
|
||||
}
|
||||
```
|
@ -1,35 +0,0 @@
|
||||
---
|
||||
id: webhooks
|
||||
title: Webhooks
|
||||
sidebar_label: Webhooks
|
||||
slug: webhooks
|
||||
---
|
||||
|
||||
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.
|
||||
|
||||
To access it, you must use the `app.get()` method, followed by the provider reference:
|
||||
```typescript
|
||||
const telegrafProvider = app.get('TelegrafProvider');
|
||||
```
|
||||
|
||||
Now you can connect middleware:
|
||||
```typescript
|
||||
app.use(telegrafProvider.webhookCallback('/secret-path'));
|
||||
```
|
||||
|
||||
The last step is to specify launchOptions in `forRoot` method:
|
||||
```typescript
|
||||
TelegrafModule.forRootAsync({
|
||||
imports: [ConfigModule.forFeature(telegrafModuleConfig)],
|
||||
useFactory: async (configService: ConfigService) => ({
|
||||
token: configService.get<string>('TELEGRAM_BOT_TOKEN'),
|
||||
launchOptions: {
|
||||
webhook: {
|
||||
domain: 'domain.tld',
|
||||
hookPath: '/secret-path',
|
||||
}
|
||||
}
|
||||
}),
|
||||
inject: [ConfigService],
|
||||
});
|
||||
```
|
@ -1,23 +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). Reserved for future use.
|
||||
|
||||
```typescript {3}
|
||||
import { Update, Context } from 'nestjs-telegraf';
|
||||
|
||||
@Update()
|
||||
export class SomeBotService {
|
||||
...
|
||||
}
|
||||
```
|
@ -1,60 +0,0 @@
|
||||
---
|
||||
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,
|
||||
});
|
||||
```
|
@ -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 } from 'nestjs-telegraf';
|
||||
|
||||
@Injectable()
|
||||
export class BotSettingsService {
|
||||
constructor(@InjectBot() private bot: TelegrafProvider) {}
|
||||
}
|
||||
```
|
@ -1,30 +0,0 @@
|
||||
---
|
||||
id: installation
|
||||
title: Installation
|
||||
sidebar_label: Installation
|
||||
slug: /
|
||||
---
|
||||
|
||||
```bash
|
||||
$ npm i nestjs-telegraf
|
||||
```
|
||||
|
||||
Once the installation process is complete, we can import the TelegrafModule into the root AppModule.
|
||||
|
||||
```typescript
|
||||
/* 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).
|
@ -1,46 +0,0 @@
|
||||
---
|
||||
id: telegraf-methods
|
||||
title: Telegraf methods
|
||||
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.
|
||||
|
||||
Now let's try to repeat the example from the Telegraf [documentation page](https://telegraf.js.org/#/?id=example).
|
||||
|
||||
```typescript
|
||||
/* app.service.ts */
|
||||
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import {
|
||||
Start,
|
||||
Help,
|
||||
On,
|
||||
Hears,
|
||||
Context,
|
||||
} from 'nestjs-telegraf';
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
@Start()
|
||||
start(ctx: Context) {
|
||||
ctx.reply('Welcome');
|
||||
}
|
||||
|
||||
@Help()
|
||||
help(ctx: Context) {
|
||||
ctx.reply('Send me a sticker');
|
||||
}
|
||||
|
||||
@On('sticker')
|
||||
on(ctx: Context) {
|
||||
ctx.reply('👍');
|
||||
}
|
||||
|
||||
@Hears('hi')
|
||||
hears(ctx: Context) {
|
||||
ctx.reply('Hey there');
|
||||
}
|
||||
}
|
||||
```
|
@ -1,35 +0,0 @@
|
||||
---
|
||||
id: webhooks
|
||||
title: Webhooks
|
||||
sidebar_label: Webhooks
|
||||
slug: webhooks
|
||||
---
|
||||
|
||||
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.
|
||||
|
||||
To access it, you must use the `app.get()` method, followed by the provider reference:
|
||||
```typescript
|
||||
const telegrafProvider = app.get('TelegrafProvider');
|
||||
```
|
||||
|
||||
Now you can connect middleware:
|
||||
```typescript
|
||||
app.use(telegrafProvider.webhookCallback('/secret-path'));
|
||||
```
|
||||
|
||||
The last step is to specify launchOptions in `forRoot` method:
|
||||
```typescript
|
||||
TelegrafModule.forRootAsync({
|
||||
imports: [ConfigModule.forFeature(telegrafModuleConfig)],
|
||||
useFactory: async (configService: ConfigService) => ({
|
||||
token: configService.get<string>('TELEGRAM_BOT_TOKEN'),
|
||||
launchOptions: {
|
||||
webhook: {
|
||||
domain: 'domain.tld',
|
||||
hookPath: '/secret-path',
|
||||
}
|
||||
}
|
||||
}),
|
||||
inject: [ConfigService],
|
||||
});
|
||||
```
|
@ -1,60 +0,0 @@
|
||||
---
|
||||
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,
|
||||
});
|
||||
```
|
@ -1,36 +0,0 @@
|
||||
---
|
||||
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>) {}
|
||||
...
|
||||
}
|
||||
```
|
@ -1,13 +0,0 @@
|
||||
---
|
||||
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()],
|
||||
}),
|
||||
```
|
@ -1,74 +0,0 @@
|
||||
---
|
||||
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],
|
||||
}),
|
||||
```
|
@ -1,27 +0,0 @@
|
||||
---
|
||||
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,44 +0,0 @@
|
||||
---
|
||||
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],
|
||||
});
|
||||
```
|
@ -1,28 +0,0 @@
|
||||
---
|
||||
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).
|
@ -1,41 +0,0 @@
|
||||
---
|
||||
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.
|
@ -1,45 +0,0 @@
|
||||
---
|
||||
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');
|
||||
}
|
||||
}
|
||||
```
|
@ -1,31 +0,0 @@
|
||||
{
|
||||
"version-1.2.1/docs": [
|
||||
{
|
||||
"collapsed": true,
|
||||
"type": "category",
|
||||
"label": "Getting Started",
|
||||
"items": [
|
||||
{
|
||||
"type": "doc",
|
||||
"id": "version-1.2.1/installation"
|
||||
},
|
||||
{
|
||||
"type": "doc",
|
||||
"id": "version-1.2.1/telegraf-methods"
|
||||
},
|
||||
{
|
||||
"type": "doc",
|
||||
"id": "version-1.2.1/bot-injection"
|
||||
},
|
||||
{
|
||||
"type": "doc",
|
||||
"id": "version-1.2.1/async-configuration"
|
||||
},
|
||||
{
|
||||
"type": "doc",
|
||||
"id": "version-1.2.1/webhooks"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
{
|
||||
"version-1.3.0/docs": [
|
||||
{
|
||||
"collapsed": false,
|
||||
"type": "category",
|
||||
"label": "Getting Started",
|
||||
"items": [
|
||||
{
|
||||
"type": "doc",
|
||||
"id": "version-1.3.0/installation"
|
||||
},
|
||||
{
|
||||
"type": "doc",
|
||||
"id": "version-1.3.0/telegraf-methods"
|
||||
},
|
||||
{
|
||||
"type": "doc",
|
||||
"id": "version-1.3.0/bot-injection"
|
||||
},
|
||||
{
|
||||
"type": "doc",
|
||||
"id": "version-1.3.0/async-configuration"
|
||||
},
|
||||
{
|
||||
"type": "doc",
|
||||
"id": "version-1.3.0/webhooks"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"collapsed": true,
|
||||
"type": "category",
|
||||
"label": "API Reference",
|
||||
"items": [
|
||||
{
|
||||
"type": "doc",
|
||||
"id": "version-1.3.0/api-reference/decorators"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
{
|
||||
"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"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
[
|
||||
"2.0.0",
|
||||
"1.3.0",
|
||||
"1.2.1"
|
||||
]
|
Loading…
Reference in New Issue
Block a user