Compare commits

...

23 Commits
v2.6.4 ... main

Author SHA1 Message Date
9e53fc62d9
chore(main): release 2.7.1 (#16) 2023-07-24 13:42:26 +00:00
dependabot[bot]
7d80334018
chore(deps-dev): bump @commitlint/config-angular from 17.1.0 to 17.6.7 (#8)
Bumps [@commitlint/config-angular](https://github.com/conventional-changelog/commitlint/tree/HEAD/@commitlint/config-angular) from 17.1.0 to 17.6.7.
- [Release notes](https://github.com/conventional-changelog/commitlint/releases)
- [Changelog](https://github.com/conventional-changelog/commitlint/blob/master/@commitlint/config-angular/CHANGELOG.md)
- [Commits](https://github.com/conventional-changelog/commitlint/commits/v17.6.7/@commitlint/config-angular)

---
updated-dependencies:
- dependency-name: "@commitlint/config-angular"
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-07-24 10:38:56 +00:00
dependabot[bot]
36ede272f2
chore(deps-dev): bump eslint-config-prettier from 8.5.0 to 8.8.0 (#18)
Bumps [eslint-config-prettier](https://github.com/prettier/eslint-config-prettier) from 8.5.0 to 8.8.0.
- [Changelog](https://github.com/prettier/eslint-config-prettier/blob/main/CHANGELOG.md)
- [Commits](https://github.com/prettier/eslint-config-prettier/compare/v8.5.0...v8.8.0)

---
updated-dependencies:
- dependency-name: eslint-config-prettier
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-07-24 10:37:44 +00:00
dependabot[bot]
261f1370a3
chore(deps-dev): bump @commitlint/cli from 17.1.2 to 17.6.7 (#20)
Bumps [@commitlint/cli](https://github.com/conventional-changelog/commitlint/tree/HEAD/@commitlint/cli) from 17.1.2 to 17.6.7.
- [Release notes](https://github.com/conventional-changelog/commitlint/releases)
- [Changelog](https://github.com/conventional-changelog/commitlint/blob/master/@commitlint/cli/CHANGELOG.md)
- [Commits](https://github.com/conventional-changelog/commitlint/commits/v17.6.7/@commitlint/cli)

---
updated-dependencies:
- dependency-name: "@commitlint/cli"
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-07-24 10:36:39 +00:00
dependabot[bot]
c94acf3ed3
chore(deps): bump clsx from 1.1.1 to 2.0.0 in /website (#17)
Bumps [clsx](https://github.com/lukeed/clsx) from 1.1.1 to 2.0.0.
- [Release notes](https://github.com/lukeed/clsx/releases)
- [Commits](https://github.com/lukeed/clsx/compare/v1.1.1...v2.0.0)

---
updated-dependencies:
- dependency-name: clsx
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-07-24 10:35:24 +00:00
dependabot[bot]
f6005d0e48
chore(deps-dev): bump @tsconfig/docusaurus in /website (#7)
Bumps [@tsconfig/docusaurus](https://github.com/tsconfig/bases/tree/HEAD/bases) from 1.0.6 to 2.0.0.
- [Commits](https://github.com/tsconfig/bases/commits/HEAD/bases)

---
updated-dependencies:
- dependency-name: "@tsconfig/docusaurus"
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-07-24 13:34:09 +03:00
dependabot[bot]
4ea8ba1cc6
chore(deps-dev): bump prettier from 2.8.0 to 3.0.0 (#6)
Bumps [prettier](https://github.com/prettier/prettier) from 2.8.0 to 3.0.0.
- [Release notes](https://github.com/prettier/prettier/releases)
- [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md)
- [Commits](https://github.com/prettier/prettier/compare/2.8.0...3.0.0)

---
updated-dependencies:
- dependency-name: prettier
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-07-24 13:32:01 +03:00
dependabot[bot]
0e2c7590da
chore(deps-dev): bump husky from 8.0.1 to 8.0.3 (#19)
Bumps [husky](https://github.com/typicode/husky) from 8.0.1 to 8.0.3.
- [Release notes](https://github.com/typicode/husky/releases)
- [Commits](https://github.com/typicode/husky/compare/v8.0.1...v8.0.3)

---
updated-dependencies:
- dependency-name: husky
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-07-24 13:30:34 +03:00
113250bcca
fix: upgrade typescript in peerDependencies (#15)
Co-authored-by: Mikhail Novikov <notiv.nt@gmail.com>
2023-07-24 09:20:40 +03:00
50e55b1bba
ci: update release-please.yml 2023-07-23 19:16:58 +03:00
github-actions[bot]
43344d308f
chore(main): release 2.7.0 (#3)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2023-07-23 19:04:27 +03:00
e605c97ac1
Update README.md 2023-07-23 16:39:41 +03:00
dependabot[bot]
afa165d717
chore(deps-dev): bump @typescript-eslint/parser from 5.45.0 to 5.62.0 (#11)
Bumps [@typescript-eslint/parser](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/parser) from 5.45.0 to 5.62.0.
- [Release notes](https://github.com/typescript-eslint/typescript-eslint/releases)
- [Changelog](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/parser/CHANGELOG.md)
- [Commits](https://github.com/typescript-eslint/typescript-eslint/commits/v5.62.0/packages/parser)

---
updated-dependencies:
- dependency-name: "@typescript-eslint/parser"
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-07-23 13:06:46 +00:00
dependabot[bot]
740c602ab6
chore(deps-dev): bump lint-staged from 13.0.4 to 13.2.3 (#10)
Bumps [lint-staged](https://github.com/okonet/lint-staged) from 13.0.4 to 13.2.3.
- [Release notes](https://github.com/okonet/lint-staged/releases)
- [Commits](https://github.com/okonet/lint-staged/compare/v13.0.4...v13.2.3)

---
updated-dependencies:
- dependency-name: lint-staged
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-07-23 16:06:44 +03:00
dependabot[bot]
11aff4f401
chore(deps-dev): bump eslint from 8.28.0 to 8.45.0 (#12)
Bumps [eslint](https://github.com/eslint/eslint) from 8.28.0 to 8.45.0.
- [Release notes](https://github.com/eslint/eslint/releases)
- [Changelog](https://github.com/eslint/eslint/blob/main/CHANGELOG.md)
- [Commits](https://github.com/eslint/eslint/compare/v8.28.0...v8.45.0)

---
updated-dependencies:
- dependency-name: eslint
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-07-23 16:05:26 +03:00
76011e10eb
docs: rename package from nestjs-telegraf to @maks1ms/nestjs-telegraf (#4) 2023-07-23 15:50:06 +03:00
dca35a0c7f
feat: add GlobalUpdate decorator (#2) 2023-07-23 15:38:05 +03:00
7f43458c55
chore: Update and rename LICENSE.md to LICENSE 2023-07-23 15:35:18 +03:00
f265176d71
ci: Rename release-please to release-please.yml 2023-07-23 15:30:42 +03:00
25bd4bb4ba
docs: add versioned docs (#1) 2023-07-23 15:28:11 +03:00
7c8d746863
ci: Add release-please 2023-07-23 15:25:36 +03:00
67225ae8ea Update package-lock.json 2023-07-23 11:07:20 +00:00
f9047c59d1 Add devcontainer 2023-07-23 11:05:38 +00:00
33 changed files with 2323 additions and 6391 deletions

View File

@ -0,0 +1,6 @@
{
"name": "nestjs-telegraf Dev Container",
"image": "mcr.microsoft.com/devcontainers/typescript-node:1-18"
}

View File

@ -1,8 +1,8 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
// project: 'tsconfig.json',
// sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [

20
.github/workflows/release-please.yml vendored Normal file
View File

@ -0,0 +1,20 @@
on:
push:
branches:
- main
permissions:
contents: write
pull-requests: write
name: release-please
jobs:
release-please:
runs-on: ubuntu-latest
steps:
- uses: google-github-actions/release-please-action@v3
with:
release-type: node
package-name: release-please-action
token: ${{ secrets.PAT }}

15
CHANGELOG.md Normal file
View File

@ -0,0 +1,15 @@
# Changelog
## [2.7.1](https://github.com/Maks1mS/nestjs-telegraf/compare/v2.7.0...v2.7.1) (2023-07-24)
### Bug Fixes
* upgrade typescript in peerDependencies ([#15](https://github.com/Maks1mS/nestjs-telegraf/issues/15)) ([113250b](https://github.com/Maks1mS/nestjs-telegraf/commit/113250bccaec8673294ad80e825d322d840a9b88))
## [2.7.0](https://github.com/Maks1mS/nestjs-telegraf/compare/v2.6.4...v2.7.0) (2023-07-23)
### Features
* add GlobalUpdate decorator ([#2](https://github.com/Maks1mS/nestjs-telegraf/issues/2)) ([dca35a0](https://github.com/Maks1mS/nestjs-telegraf/commit/dca35a0c7f5dda6cb1ce2768d244d68274c544f3))

View File

@ -1,6 +1,7 @@
MIT License
Copyright (c) 2019 - present Hypeer (hello@hypeer.company)
Copyright (c) 2023 Maks1mS
Copyright (c) 2019 Hypeer (hello@hypeer.company)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -1,4 +1,4 @@
# NestJS Telegraf ![npm](https://img.shields.io/npm/dm/nestjs-telegraf) ![GitHub last commit](https://img.shields.io/github/last-commit/bukhalo/nestjs-telegraf) ![NPM](https://img.shields.io/npm/l/nestjs-telegraf)
# NestJS Telegraf ![npm](https://img.shields.io/npm/dm/@maks1ms/nestjs-telegraf) ![GitHub last commit](https://img.shields.io/github/last-commit/maks1ms/nestjs-telegraf) ![NPM](https://img.shields.io/npm/l/@maks1ms/nestjs-telegraf)
<img align="right" width="95" height="148" title="NestJS logotype"
src="https://nestjs.com/img/logo-small.svg">
@ -17,18 +17,11 @@ This package uses the best of the NodeJS world under the hood. [Telegraf](https:
- Ability to run multiple bots simultaneously.
- Full support of NestJS guards, interceptors, filters and pipes!
**User stories**
- [Новогодняя история одного телеграм-бота на NestJS](https://habr.com/ru/company/tinkoff/blog/596287/) by [Tinkoff Bank](https://github.com/Tinkoff)
- [The story of the creation of the personal telegram bot (40 articles!)](https://dev.to/endykaufman/i-decided-to-try-to-keep-a-twitter-history-of-rewriting-one-of-the-projects-im-starting--1e6p) by [@EndyKaufman](https://github.com/EndyKaufman)
**Chat**
* [Telegram](https://t.me/nestjs_telegraf)
## Installation
```bash
$ npm i nestjs-telegraf telegraf
$ npm i @maks1ms/nestjs-telegraf telegraf
```
## Documentation
Check out the [documentation site](https://nestjs-telegraf.hypeer.company).
Check out the [documentation site](https://nestjs-telegraf-maks1ms.vercel.app).

View File

@ -0,0 +1,9 @@
import { SetMetadata } from '@nestjs/common';
import { GLOBAL_UPDATE_METADATA } from '../../telegraf.constants';
/**
* `@GlobalUpdate` decorator, it's like `@Update` decorator,
* but processed before the scenes
*/
export const GlobalUpdate = (): ClassDecorator =>
SetMetadata(GLOBAL_UPDATE_METADATA, true);

View File

@ -1,4 +1,5 @@
export * from './update.decorator';
export * from './global-update.decorator';
export * from './scene.decorator';
export * from './wizard.decorator';
export * from './inject-bot.decorator';

View File

@ -48,8 +48,6 @@ export class ListenersExplorerService
this.bot = this.moduleRef.get<Telegraf<any>>(this.botName, {
strict: false,
});
this.bot.use(this.stage.middleware());
this.explore();
}
@ -59,10 +57,23 @@ export class ListenersExplorerService
this.telegrafOptions.include || [],
);
this.registerGlobalUpdates(modules);
this.bot.use(this.stage.middleware());
this.registerUpdates(modules);
this.registerScenes(modules);
}
private registerGlobalUpdates(modules: Module[]): void {
const globalUpdates = this.flatMap<InstanceWrapper>(modules, (instance) =>
this.filterGlobalUpdates(instance),
);
globalUpdates.forEach((wrapper) =>
this.registerListeners(this.bot, wrapper),
);
}
private registerUpdates(modules: Module[]): void {
const updates = this.flatMap<InstanceWrapper>(modules, (instance) =>
this.filterUpdates(instance),
@ -92,6 +103,20 @@ export class ListenersExplorerService
});
}
private filterGlobalUpdates(
wrapper: InstanceWrapper,
): InstanceWrapper<unknown> {
const { instance } = wrapper;
if (!instance) return undefined;
const isGlobalUpdate = this.metadataAccessor.isGlobalUpdate(
wrapper.metatype,
);
if (!isGlobalUpdate) return undefined;
return wrapper;
}
private filterUpdates(wrapper: InstanceWrapper): InstanceWrapper<unknown> {
const { instance } = wrapper;
if (!instance) return undefined;

View File

@ -3,6 +3,7 @@ import { Reflector } from '@nestjs/core';
import {
SCENE_METADATA,
LISTENERS_METADATA,
GLOBAL_UPDATE_METADATA,
UPDATE_METADATA,
WIZARD_STEP_METADATA,
} from '../telegraf.constants';
@ -16,6 +17,11 @@ import {
export class MetadataAccessorService {
constructor(private readonly reflector: Reflector) {}
isGlobalUpdate(target: Function): boolean {
if (!target) return false;
return !!this.reflector.get(GLOBAL_UPDATE_METADATA, target);
}
isUpdate(target: Function): boolean {
if (!target) return false;
return !!this.reflector.get(UPDATE_METADATA, target);

View File

@ -5,6 +5,7 @@ export const TELEGRAF_BOT_NAME = 'TELEGRAF_BOT_NAME';
export const DEFAULT_BOT_NAME = 'DEFAULT_BOT_NAME';
export const UPDATE_METADATA = 'UPDATE_METADATA';
export const GLOBAL_UPDATE_METADATA = 'GLOBAL_UPDATE_METADATA';
export const SCENE_METADATA = 'SCENE_METADATA';
export const LISTENERS_METADATA = 'LISTENERS_METADATA';
export const WIZARD_STEP_METADATA = 'WIZARD_STEP_METADATA';

1483
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@maks1ms/nestjs-telegraf",
"version": "2.6.4",
"version": "2.7.1",
"description": "Telegraf module for NestJS",
"keywords": [
"nest",
@ -54,19 +54,19 @@
"lodash": "^4.17.21"
},
"devDependencies": {
"@commitlint/cli": "17.1.2",
"@commitlint/config-angular": "17.1.0",
"@commitlint/cli": "17.6.7",
"@commitlint/config-angular": "17.6.7",
"@nestjs/common": "^9.0.11",
"@nestjs/core": "^9.0.11",
"@types/lodash": "4.14.191",
"@typescript-eslint/eslint-plugin": "5.44.0",
"@typescript-eslint/parser": "5.45.0",
"eslint": "8.28.0",
"eslint-config-prettier": "8.5.0",
"@typescript-eslint/parser": "5.62.0",
"eslint": "8.45.0",
"eslint-config-prettier": "8.8.0",
"eslint-plugin-prettier": "4.2.1",
"husky": "8.0.1",
"lint-staged": "13.0.4",
"prettier": "2.8.0",
"husky": "8.0.3",
"lint-staged": "13.2.3",
"prettier": "3.0.0",
"reflect-metadata": "0.1.13",
"telegraf": "4.12.2",
"typedoc": "0.23.11",
@ -77,6 +77,6 @@
"@nestjs/core": "^9.0.0 || ^10.0.0",
"reflect-metadata": "^0.1.13",
"telegraf": "^4.0.0",
"typescript": "^4.1.2"
"typescript": "^4.1.2 || ^5.0.2"
}
}

View File

@ -9,7 +9,7 @@ At times you may need to access the native `Telegraf` instance. You can inject t
```typescript {8} title="src/echo/echo.service.ts"
import { Injectable } from '@nestjs/common';
import { InjectBot } from 'nestjs-telegraf';
import { InjectBot } from '@maks1ms/nestjs-telegraf';
import { Telegraf } from 'telegraf';
import { TelegrafContext } from '../common/interfaces/telegraf-context.interface.ts';
@ -24,7 +24,7 @@ If you run [multiple bots](/extras/multiple-bots) in the same application, expli
```typescript {8} title="src/echo/echo.service.ts"
import { Injectable } from '@nestjs/common';
import { InjectBot } from 'nestjs-telegraf';
import { InjectBot } from '@maks1ms/nestjs-telegraf';
import { Telegraf } from 'telegraf';
import { TelegrafContext } from '../common/interfaces/telegraf-context.interface.ts';

View File

@ -9,7 +9,7 @@ In some cases, you may need to run multiple bots at the same time. This can also
```typescript
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TelegrafModule } from 'nestjs-telegraf';
import { TelegrafModule } from '@maks1ms/nestjs-telegraf';
@Module({
imports: [
@ -42,7 +42,7 @@ Please note that you shouldn't have multiple bots without a name, or with the sa
You can also inject the `Bot` for a given bot:
```typescript
import { Injectable } from '@nestjs/common';
import { InjectBot, Telegraf, Context } from 'nestjs-telegraf';
import { InjectBot, Telegraf, Context } from @maks1ms/nestjs-telegraf';
@Injectable()
export class EchoService {

View File

@ -15,7 +15,7 @@ If you want to configure a telegram bot webhook, you need to get a middleware vi
To access it, you must use the `app.get()` method, followed by the provider reference:
```typescript
import { getBotToken } from 'nestjs-telegraf';
import { getBotToken } from '@maks1ms/nestjs-telegraf';
// ...
const bot = app.get(getBotToken());

View File

@ -6,14 +6,14 @@ slug: /
---
```bash
$ npm i nestjs-telegraf telegraf
$ npm i @maks1ms/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';
import { TelegrafModule } from '@maks1ms/nestjs-telegraf';
@Module({
imports: [

View File

@ -17,7 +17,7 @@ import {
Help,
On,
Hears,
} from 'nestjs-telegraf';
} from '@maks1ms/nestjs-telegraf';
import { TelegrafContext } from './common/interfaces/telegraf-context.interface.ts';
@Update()

View File

@ -1,18 +1,28 @@
function getNextVersionName() {
return 'Canary';
}
const BASE_GH_URL = 'https://github.com/maks1ms/nestjs-telegraf';
module.exports = {
title: 'NestJS Telegraf',
tagline: 'Powerful Nest module for easy and fast creation Telegram bots',
url: 'https://nestjs-telegraf.hypeer.company',
url: 'https://nestjs-telegraf-maks1ms.vercel.app',
baseUrl: '/',
onBrokenLinks: 'throw',
favicon: 'img/favicon.ico',
organizationName: 'hypeertech',
organizationName: 'maks1ms',
projectName: 'nestjs-telegraf',
themeConfig: {
navbar: {
title: 'NestJS Telegraf',
items: [
{
href: 'https://github.com/hypeertech/nestjs-telegraf',
type: 'docsVersionDropdown',
position: 'right',
},
{
href: BASE_GH_URL,
label: 'GitHub',
position: 'right',
},
@ -35,11 +45,7 @@ module.exports = {
items: [
{
label: 'Discussions',
href: 'https://github.com/hypeertech/nestjs-telegraf/discussions',
},
{
label: 'Telegram',
href: 'https://t.me/nestjs_telegraf',
href: `${BASE_GH_URL}/discussions`,
},
],
},
@ -48,16 +54,17 @@ module.exports = {
items: [
{
label: 'Issues',
href: 'https://github.com/hypeertech/nestjs-telegraf/issues',
href: `${BASE_GH_URL}/issues`,
},
{
label: 'Examples',
to: 'https://github.com/hypeertech/nestjs-telegraf/tree/master/sample/',
}
to: `${BASE_GH_URL}/tree/master/sample/`,
},
],
},
],
copyright: `Copyright © 2019 - ${new Date().getFullYear()}, <a target="_blank" href="https://hypeer.company">Hypeer</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>.`,
copyright: `Copyright © 2019 - 2023 <a target="_blank" href="https://hypeer.company">Hypeer</a>, <a target="_blank" href="mailto:arthur.asimov.z0@gmail.com">Arthur Asimov</a> and <a target="_blank" href="https://github.com/hypeertech/nestjs-telegraf/graphs/contributors">Others</a>.<br>
Copyright 2023 - <a target="_blank" href="https://github.com/Maks1mS">Maks1mS</a> and <a target="_blank" href="${BASE_GH_URL}/graphs/contributors">Others</a>`,
},
},
presets: [
@ -67,10 +74,17 @@ module.exports = {
docs: {
sidebarPath: require.resolve('./sidebars.js'),
routeBasePath: '/',
editUrl:
'https://github.com/hypeertech/nestjs-telegraf/edit/master/website/',
editUrl: ({ docPath }) => {
const nextVersionDocsDirPath = 'docs';
return `${BASE_GH_URL}/edit/main/website/${nextVersionDocsDirPath}/${docPath}`;
},
showLastUpdateAuthor: true,
showLastUpdateTime: true,
versions: {
current: {
label: `${getNextVersionName()} 🚧`,
},
},
},
theme: {
customCss: require.resolve('./src/css/custom.css'),
@ -78,12 +92,4 @@ module.exports = {
},
],
],
plugins: [
[
require.resolve('docusaurus-gtm-plugin'),
{
id: 'GTM-PRP5KRP',
}
]
],
};

6636
website/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -11,10 +11,10 @@
"serve": "docusaurus serve"
},
"dependencies": {
"@docusaurus/core": "2.2.0",
"@docusaurus/preset-classic": "2.1.0",
"@docusaurus/core": "2.4.1",
"@docusaurus/preset-classic": "2.4.1",
"@mdx-js/react": "1.6.22",
"clsx": "1.1.1",
"clsx": "2.0.0",
"docusaurus-gtm-plugin": "^0.0.2",
"react": "17.0.2",
"react-dom": "17.0.2"
@ -32,8 +32,8 @@
]
},
"devDependencies": {
"@docusaurus/module-type-aliases": "2.2.0",
"@tsconfig/docusaurus": "1.0.6",
"@docusaurus/module-type-aliases": "2.4.1",
"@tsconfig/docusaurus": "2.0.0",
"@types/react": "17.0.39",
"@types/react-helmet": "6.1.5",
"@types/react-router-dom": "5.3.3",

View File

@ -1,4 +1,4 @@
{
"extends": "@tsconfig/docusaurus/tsconfig.json",
"include": ["src/"]
"exclude": ["src/sw.js"]
}

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 '@maks1ms/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 '@maks1ms/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 '@maks1ms/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 @maks1ms/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 '@maks1ms/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 @maks1ms/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 '@maks1ms/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 '@maks1ms/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,19 @@
{
"docs": {
"Getting Started": [
"installation",
"getting-updates",
"telegraf-methods",
"async-configuration"
],
"Extras": [
"extras/bot-injection",
"extras/middlewares",
"extras/multiple-bots",
"extras/standalone-applications"
],
"Migrating": [
"migrating/from-v1-to-v2"
]
}
}

3
website/versions.json Normal file
View File

@ -0,0 +1,3 @@
[
"2.6"
]