mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-01-26 21:53:28 +03:00
Merge pull request #1 from bukhalo/some-work
chore(): publish 0.6.0 release
This commit is contained in:
commit
57baf28809
19
.gitignore
vendored
19
.gitignore
vendored
@ -1,2 +1,19 @@
|
|||||||
node_modules
|
# dependencies
|
||||||
|
/node_modules
|
||||||
|
|
||||||
|
# IDE
|
||||||
|
/.idea
|
||||||
|
/.awcache
|
||||||
|
/.vscode
|
||||||
|
|
||||||
|
# misc
|
||||||
|
npm-debug.log
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
# tests
|
||||||
|
/test
|
||||||
|
/coverage
|
||||||
|
/.nyc_output
|
||||||
|
|
||||||
|
# source
|
||||||
dist
|
dist
|
7
.npmignore
Normal file
7
.npmignore
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# source
|
||||||
|
lib
|
||||||
|
index.ts
|
||||||
|
package-lock.json
|
||||||
|
tslint.json
|
||||||
|
tsconfig.json
|
||||||
|
.prettierrc
|
4
.prettierrc
Normal file
4
.prettierrc
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"trailingComma": "none",
|
||||||
|
"singleQuote": true
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2019 Igor Kamyshev
|
Copyright (c) 2019 Bukhalo Aleksandr Aleksandrovich & Igor Kamyshev
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
171
README.md
171
README.md
@ -1,153 +1,48 @@
|
|||||||
# nest-telegram
|
<p align="center">
|
||||||
|
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="120" alt="Nest Logo" /></a>
|
||||||
|
</p>
|
||||||
|
|
||||||
Integrate [telegraf.js](https://telegraf.js.org/) to [NestJS](https://nestjs.com/) application.
|
[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
|
||||||
|
[circleci-url]: https://circleci.com/gh/nestjs/nest
|
||||||
|
|
||||||
> Warning! Package under development, please waiting for v1 release.
|
<p align="center">A progressive <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient and scalable server-side applications.</p>
|
||||||
|
<p align="center">
|
||||||
|
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
|
||||||
|
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
|
||||||
|
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/core.svg" alt="NPM Downloads" /></a>
|
||||||
|
<a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a>
|
||||||
|
<a href="https://coveralls.io/github/nestjs/nest?branch=master" target="_blank"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#9" alt="Coverage" /></a>
|
||||||
|
<a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
|
||||||
|
<a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
|
||||||
|
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
|
||||||
|
<a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg"/></a>
|
||||||
|
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a>
|
||||||
|
<a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow"></a>
|
||||||
|
</p>
|
||||||
|
|
||||||
## Instalation
|
## Description
|
||||||
|
|
||||||
`yarn add nest-telegram`
|
[Telegraf](https://github.com/telegraf/telegraf) module for [Nest](https://github.com/nestjs/nest).
|
||||||
|
|
||||||
## Setup
|
## Installation
|
||||||
|
|
||||||
### Add TelegramModule to your app
|
```bash
|
||||||
|
$ npm i nestjs-telegraf telegraf
|
||||||
```ts
|
|
||||||
import { TelegramModule, TelegramModuleOptionsFactory } from 'nest-telegram'
|
|
||||||
|
|
||||||
// In real app, please, don't store token in source code
|
|
||||||
class TelegramOptionsFactory implements TelegramModuleOptionsFactory {
|
|
||||||
createOptions(): TelegramModuleOptions {
|
|
||||||
return {
|
|
||||||
token: 'TelegramToken#1213',
|
|
||||||
sitePublicUrl: 'https://my-site.com',
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
imports: [
|
|
||||||
TelegramModule.fromFactory({,
|
|
||||||
useClass: TelegramOptionsFactory,
|
|
||||||
}),
|
|
||||||
UtilsModule,
|
|
||||||
],
|
|
||||||
})
|
|
||||||
export class MyModule implements NestModule {
|
|
||||||
constructor(
|
|
||||||
private readonly moduleRef: ModuleRef,
|
|
||||||
private readonly telegramBot: TelegramBot,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
onModuleInit() {
|
|
||||||
const isDev = process.env.NODE_ENV === 'development'
|
|
||||||
|
|
||||||
this.telegramBot.init(this.moduleRef)
|
|
||||||
|
|
||||||
if (isDev) {
|
|
||||||
// in dev mode, we can't use webhook
|
|
||||||
this.telegramBot.startPolling()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Add custom middleware to your app
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { TelegramBot } from 'nest-telegram'
|
|
||||||
import { NestFactory } from '@nestjs/core'
|
|
||||||
import { AppModule } from '@app/app.module'
|
|
||||||
|
|
||||||
async function bootstrap() {
|
|
||||||
const isDev = process.env.NODE_ENV === 'development'
|
|
||||||
|
|
||||||
const app = await NestFactory.create(AppModule)
|
|
||||||
|
|
||||||
const bot = app.get(TelegramBot)
|
|
||||||
|
|
||||||
if (!isDev) {
|
|
||||||
app.use(bot.getMiddleware('hook-path'))
|
|
||||||
}
|
|
||||||
|
|
||||||
await app.listen(3000)
|
|
||||||
}
|
|
||||||
bootstrap()
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
TBD
|
||||||
|
|
||||||
Now, you can decorate any method with `TelegramActionHandler`.
|
## Support
|
||||||
|
|
||||||
Example:
|
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
|
||||||
|
|
||||||
```ts
|
## People
|
||||||
import { Injectable } from '@nestjs/common'
|
|
||||||
import { Context, PipeContext, TelegramActionHandler } from 'nest-telegram'
|
|
||||||
|
|
||||||
@Injectable()
|
- Authors - [Aleksandr Bukhalo](https://bukhalo.com/) & [Igor Kamyshev](https://kamyshev.me/)
|
||||||
export class HelpActions {
|
- Maintainers - [Aleksandr Bukhalo](https://bukhalo.com/)
|
||||||
@TelegramActionHandler({ onStart: true })
|
- Website - [https://nestjs.com](https://nestjs.com/)
|
||||||
async start(ctx: Context) {
|
|
||||||
await ctx.reply('Hello!')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Available actions for decorator:
|
## License
|
||||||
|
|
||||||
+ `onStart` {boolean}, it triggers on `/start` command.
|
Nest is [MIT licensed](https://github.com/nestjs/nest/blob/master/LICENSE).
|
||||||
+ `command` {string}, it triggers on any command, e.g. — `@TelegramActionHandler({ command: '/help' })`.
|
|
||||||
+ `message` {string|RegExp}, it triggers on text message matching RegExp or string.
|
|
||||||
|
|
||||||
Also, you can write Transformators for context (like Pipes in NestJS). Example:
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { Injectable } from '@nestjs/common'
|
|
||||||
import { ContextTransformer, Context } from 'nest-telegram'
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
class CurrentSender implements ContextTransformer<TokenPayloadModel> {
|
|
||||||
async transform(ctx: Context) {
|
|
||||||
const user = // get user from DB
|
|
||||||
|
|
||||||
return {
|
|
||||||
login: user.login,
|
|
||||||
isManager: user.isManager,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class SomethingActions {
|
|
||||||
@TelegramActionHandler({ command: '/say' })
|
|
||||||
async say(
|
|
||||||
ctx: Context,
|
|
||||||
// apply this transformer like this
|
|
||||||
@PipeContext(CurrentSender) user: TokenPayloadModel,
|
|
||||||
) {
|
|
||||||
const { login } = user
|
|
||||||
|
|
||||||
// now you can use `login`
|
|
||||||
await ctx.reply(`Hello, ${login}`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Also, you can write `Catchers` for exceptions (like Filters in NestJS). Example:
|
|
||||||
|
|
||||||
```js
|
|
||||||
import { TelegramErrorHandler, TelegramCatch, Context } from 'nest-telegram'
|
|
||||||
|
|
||||||
@TelegramCatch(MyExecption)
|
|
||||||
export class MyCatcher
|
|
||||||
implements TelegramErrorHandler<MyExecption> {
|
|
||||||
public async catch(ctx: Context, exception: MyExecption) {
|
|
||||||
await ctx.reply(exception.message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Stay tuned, stable release is coming. 🤓
|
|
1
index.d.ts
vendored
Normal file
1
index.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './dist';
|
6
index.js
Normal file
6
index.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
"use strict";
|
||||||
|
function __export(m) {
|
||||||
|
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
||||||
|
}
|
||||||
|
exports.__esModule = true;
|
||||||
|
__export(require("./dist"));
|
@ -1,4 +0,0 @@
|
|||||||
import Telegraf from 'telegraf'
|
|
||||||
import { Context } from './Context'
|
|
||||||
|
|
||||||
export type Bot = Telegraf<Context>
|
|
@ -1,3 +0,0 @@
|
|||||||
import { ContextMessageUpdate } from 'telegraf'
|
|
||||||
|
|
||||||
export type Context = ContextMessageUpdate
|
|
@ -1,34 +0,0 @@
|
|||||||
import { Injectable, Inject } from '@nestjs/common'
|
|
||||||
const Telegram = require('telegraf/telegram')
|
|
||||||
|
|
||||||
import { TokenInjectionToken } from './TokenInjectionToken'
|
|
||||||
import { TelegramModuleOptionsFactory } from './TelegramModuleOptionsFactory'
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class TelegramClient {
|
|
||||||
private telegram: any
|
|
||||||
|
|
||||||
public constructor(
|
|
||||||
@Inject(TokenInjectionToken) factory: TelegramModuleOptionsFactory,
|
|
||||||
) {
|
|
||||||
const { token } = factory.createOptions()
|
|
||||||
|
|
||||||
this.telegram = new Telegram(token)
|
|
||||||
}
|
|
||||||
|
|
||||||
public async sendMessage(
|
|
||||||
chatId: string | number,
|
|
||||||
text: string,
|
|
||||||
): Promise<void> {
|
|
||||||
await this.telegram.sendMessage(chatId, text)
|
|
||||||
}
|
|
||||||
|
|
||||||
public async sendMarkdown(
|
|
||||||
chatId: string | number,
|
|
||||||
markdown: string,
|
|
||||||
): Promise<void> {
|
|
||||||
await this.telegram.sendMessage(chatId, markdown, {
|
|
||||||
parse_mode: 'Markdown',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,4 +0,0 @@
|
|||||||
export interface TelegramModuleOptions {
|
|
||||||
token: string
|
|
||||||
sitePublicUrl?: string
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
import { TelegramModuleOptions } from './TelegramModuleOptions'
|
|
||||||
|
|
||||||
export interface TelegramModuleOptionsFactory {
|
|
||||||
createOptions(): TelegramModuleOptions
|
|
||||||
}
|
|
3
lib/decorators/index.ts
Normal file
3
lib/decorators/index.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export * from './pipe-context.decorator'
|
||||||
|
export * from './telegram-action-handler.decorator'
|
||||||
|
export * from './telegram-catch.decorator'
|
@ -1,6 +1,6 @@
|
|||||||
import { Type } from '@nestjs/common'
|
import { Type } from '@nestjs/common'
|
||||||
import { ContextTransformer } from '../ContextTransformer'
|
import { ContextTransformer } from '../interfaces'
|
||||||
import { addHandlerToStore } from './TelegramActionHandler'
|
import { addHandlerToStore } from './'
|
||||||
|
|
||||||
export const PipeContext = <T>(transform: Type<ContextTransformer<T>>) => (
|
export const PipeContext = <T>(transform: Type<ContextTransformer<T>>) => (
|
||||||
target: Object,
|
target: Object,
|
@ -1,4 +1,4 @@
|
|||||||
import { HandleParameters } from '../HandleParameters'
|
import { HandleParameters } from '../interfaces'
|
||||||
|
|
||||||
type Decorator = (
|
type Decorator = (
|
||||||
params: HandleParameters,
|
params: HandleParameters,
|
@ -1,5 +1,5 @@
|
|||||||
import { Type } from '@nestjs/common'
|
import { Type } from '@nestjs/common'
|
||||||
import { TelegramErrorHandler } from '../interfaces/TelegramErrorHandler'
|
import { TelegramErrorHandler } from '../interfaces'
|
||||||
|
|
||||||
type Decorator = (error: any) => ClassDecorator
|
type Decorator = (error: any) => ClassDecorator
|
||||||
|
|
1
lib/exeptions/index.ts
Normal file
1
lib/exeptions/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './invalid-configuration.exeption'
|
20
lib/index.ts
20
lib/index.ts
@ -1,15 +1,5 @@
|
|||||||
export { TelegramModule } from './telegram.module'
|
export * from './telegraf.module'
|
||||||
export { TelegramBot } from './TelegramBot'
|
export * from './interfaces'
|
||||||
export { TelegramClient } from './TelegramClient'
|
export * from './decorators'
|
||||||
|
export * from './telegraf.service'
|
||||||
export { TelegramModuleOptionsFactory } from './TelegramModuleOptionsFactory'
|
export * from './telegraf-telegram.service'
|
||||||
export { TelegramModuleOptions } from './TelegramModuleOptions'
|
|
||||||
|
|
||||||
export { PipeContext } from './decorators/PipeContext'
|
|
||||||
export { TelegramActionHandler } from './decorators/TelegramActionHandler'
|
|
||||||
export { TelegramCatch } from './decorators/TelegramCatch'
|
|
||||||
|
|
||||||
export { TelegramErrorHandler } from './interfaces/TelegramErrorHandler'
|
|
||||||
|
|
||||||
export { ContextTransformer } from './ContextTransformer'
|
|
||||||
export { Context } from './Context'
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { ContextTransformer } from './ContextTransformer'
|
import { ContextTransformer } from './'
|
||||||
import { Type } from '@nestjs/common'
|
import { Type } from '@nestjs/common'
|
||||||
|
|
||||||
interface ArgumentTransformation {
|
interface ArgumentTransformation {
|
@ -1,4 +1,4 @@
|
|||||||
import { HandleParameters } from './HandleParameters'
|
import { HandleParameters } from './'
|
||||||
|
|
||||||
export interface Handler {
|
export interface Handler {
|
||||||
handle: (...args: any[]) => Promise<void>
|
handle: (...args: any[]) => Promise<void>
|
5
lib/interfaces/index.ts
Normal file
5
lib/interfaces/index.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
export * from './telegraf-options.interface'
|
||||||
|
export * from './handler.interface'
|
||||||
|
export * from './handle-parameters.interface'
|
||||||
|
export * from './telegram-error-handler.interface'
|
||||||
|
export * from './context-transformer.interface'
|
20
lib/interfaces/telegraf-options.interface.ts
Normal file
20
lib/interfaces/telegraf-options.interface.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import { ModuleMetadata, Type } from '@nestjs/common/interfaces'
|
||||||
|
|
||||||
|
export interface TelegrafModuleOptions {
|
||||||
|
token: string
|
||||||
|
sitePublicUrl?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TelegrafOptionsFactory {
|
||||||
|
createTelegrafOptions(): TelegrafModuleOptions
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TelegrafModuleAsyncOptions
|
||||||
|
extends Pick<ModuleMetadata, 'imports'> {
|
||||||
|
useExisting?: Type<TelegrafOptionsFactory>
|
||||||
|
useClass?: Type<TelegrafOptionsFactory>
|
||||||
|
useFactory?: (
|
||||||
|
...args: any[]
|
||||||
|
) => Promise<TelegrafModuleOptions> | TelegrafModuleOptions
|
||||||
|
inject?: any[]
|
||||||
|
}
|
13
lib/telegraf-telegram.service.ts
Normal file
13
lib/telegraf-telegram.service.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { Injectable, Inject } from '@nestjs/common';
|
||||||
|
const Telegram = require('telegraf/telegram');
|
||||||
|
import { Telegram as TelegramClient } from 'telegraf';
|
||||||
|
|
||||||
|
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants';
|
||||||
|
import { TelegrafModuleOptions } from './interfaces';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class TelegrafTelegramService extends TelegramClient {
|
||||||
|
constructor(@Inject(TELEGRAF_MODULE_OPTIONS) options: TelegrafModuleOptions) {
|
||||||
|
super(options.token, {});
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1,2 @@
|
|||||||
|
export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS'
|
||||||
export const TokenInjectionToken = Symbol('TokenInjectionToken')
|
export const TokenInjectionToken = Symbol('TokenInjectionToken')
|
63
lib/telegraf.module.ts
Normal file
63
lib/telegraf.module.ts
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
import { Module, DynamicModule, Provider } from '@nestjs/common';
|
||||||
|
import {
|
||||||
|
TelegrafModuleAsyncOptions,
|
||||||
|
TelegrafOptionsFactory
|
||||||
|
} from './interfaces';
|
||||||
|
import {
|
||||||
|
TELEGRAF_MODULE_OPTIONS,
|
||||||
|
TokenInjectionToken
|
||||||
|
} from './telegraf.constants';
|
||||||
|
import { TelegrafService, TelegrafTelegramService } from './';
|
||||||
|
|
||||||
|
@Module({})
|
||||||
|
export class TelegrafModule {
|
||||||
|
static fromFactory(options: TelegrafModuleAsyncOptions): DynamicModule {
|
||||||
|
return {
|
||||||
|
module: TelegrafModule,
|
||||||
|
imports: options.imports || [],
|
||||||
|
providers: [
|
||||||
|
...this.createAsyncProviders(options),
|
||||||
|
TelegrafService,
|
||||||
|
TelegrafTelegramService,
|
||||||
|
{
|
||||||
|
provide: TokenInjectionToken,
|
||||||
|
useClass: options.useClass
|
||||||
|
}
|
||||||
|
],
|
||||||
|
exports: [TelegrafService, TelegrafTelegramService]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static createAsyncProviders(
|
||||||
|
options: TelegrafModuleAsyncOptions
|
||||||
|
): Provider[] {
|
||||||
|
if (options.useExisting || options.useFactory) {
|
||||||
|
return [this.createAsyncOptionsProvider(options)];
|
||||||
|
}
|
||||||
|
return [
|
||||||
|
this.createAsyncOptionsProvider(options),
|
||||||
|
{
|
||||||
|
provide: options.useClass,
|
||||||
|
useClass: options.useClass
|
||||||
|
}
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private static createAsyncOptionsProvider(
|
||||||
|
options: TelegrafModuleAsyncOptions
|
||||||
|
): Provider {
|
||||||
|
if (options.useFactory) {
|
||||||
|
return {
|
||||||
|
provide: TELEGRAF_MODULE_OPTIONS,
|
||||||
|
useFactory: options.useFactory,
|
||||||
|
inject: options.inject || []
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
provide: TELEGRAF_MODULE_OPTIONS,
|
||||||
|
useFactory: async (optionsFactory: TelegrafOptionsFactory) =>
|
||||||
|
await optionsFactory.createTelegrafOptions(),
|
||||||
|
inject: [options.useExisting || options.useClass]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -1,29 +1,29 @@
|
|||||||
import { Injectable, Inject } from '@nestjs/common'
|
import { Injectable, Inject, Logger } from '@nestjs/common'
|
||||||
import { ModuleRef } from '@nestjs/core'
|
import { ModuleRef } from '@nestjs/core'
|
||||||
import Telegraf, { ContextMessageUpdate } from 'telegraf'
|
import Telegraf, { ContextMessageUpdate } from 'telegraf'
|
||||||
import { flatten, head } from 'lodash'
|
import { flatten, head } from 'lodash'
|
||||||
|
|
||||||
import { ContextTransformer } from './ContextTransformer'
|
import { TelegramCatch, TelegramActionHandler } from './decorators'
|
||||||
import { TelegramCatch } from './decorators/TelegramCatch'
|
import { TokenInjectionToken } from './telegraf.constants'
|
||||||
import { TelegramErrorHandler } from './interfaces/TelegramErrorHandler'
|
import {
|
||||||
import { Handler } from './Handler'
|
TelegrafOptionsFactory,
|
||||||
import { Bot } from './Bot'
|
TelegramErrorHandler,
|
||||||
import { TelegramActionHandler } from './decorators/TelegramActionHandler'
|
Handler,
|
||||||
import { TokenInjectionToken } from './TokenInjectionToken'
|
ContextTransformer,
|
||||||
import { TelegramModuleOptionsFactory } from './TelegramModuleOptionsFactory'
|
} from './interfaces'
|
||||||
import { InvalidConfigurationException } from './InvalidConfigurationException'
|
import { InvalidConfigurationException } from './exeptions'
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TelegramBot {
|
export class TelegrafService {
|
||||||
|
private readonly logger = new Logger(TelegrafService.name, true)
|
||||||
private readonly sitePublicUrl?: string
|
private readonly sitePublicUrl?: string
|
||||||
private readonly bot: Bot
|
private readonly bot: Telegraf<ContextMessageUpdate>
|
||||||
private ref: ModuleRef
|
private ref: ModuleRef
|
||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
@Inject(TokenInjectionToken) factory: TelegramModuleOptionsFactory,
|
@Inject(TokenInjectionToken) options: TelegrafOptionsFactory,
|
||||||
) {
|
) {
|
||||||
const { token, sitePublicUrl } = factory.createOptions()
|
const { token, sitePublicUrl } = options.createTelegrafOptions()
|
||||||
|
|
||||||
this.sitePublicUrl = sitePublicUrl
|
this.sitePublicUrl = sitePublicUrl
|
||||||
this.bot = new Telegraf(token)
|
this.bot = new Telegraf(token)
|
||||||
}
|
}
|
||||||
@ -54,7 +54,7 @@ export class TelegramBot {
|
|||||||
|
|
||||||
this.bot.telegram
|
this.bot.telegram
|
||||||
.setWebhook(url)
|
.setWebhook(url)
|
||||||
.then(() => console.log(`Webhook set success @ ${url}`))
|
.then(() => this.logger.log(`Webhook set success @ ${url}`))
|
||||||
|
|
||||||
return this.bot.webhookCallback(`/${path}`)
|
return this.bot.webhookCallback(`/${path}`)
|
||||||
}
|
}
|
@ -1,39 +0,0 @@
|
|||||||
import {
|
|
||||||
MiddlewareConsumer,
|
|
||||||
Module,
|
|
||||||
NestModule,
|
|
||||||
DynamicModule,
|
|
||||||
} from '@nestjs/common'
|
|
||||||
import { ModuleMetadata, Type } from '@nestjs/common/interfaces'
|
|
||||||
|
|
||||||
import { TelegramBot } from './TelegramBot'
|
|
||||||
import { TelegramModuleOptionsFactory } from './TelegramModuleOptionsFactory'
|
|
||||||
import { TokenInjectionToken } from './TokenInjectionToken'
|
|
||||||
import { TelegramClient } from './TelegramClient'
|
|
||||||
|
|
||||||
interface TelegramFactory extends Pick<ModuleMetadata, 'imports'> {
|
|
||||||
useClass?: Type<TelegramModuleOptionsFactory>
|
|
||||||
inject?: any[]
|
|
||||||
}
|
|
||||||
|
|
||||||
@Module({})
|
|
||||||
export class TelegramModule implements NestModule {
|
|
||||||
public configure(consumer: MiddlewareConsumer) {
|
|
||||||
// pass
|
|
||||||
}
|
|
||||||
|
|
||||||
static fromFactory(factory: TelegramFactory): DynamicModule {
|
|
||||||
return {
|
|
||||||
module: TelegramModule,
|
|
||||||
providers: [
|
|
||||||
TelegramBot,
|
|
||||||
TelegramClient,
|
|
||||||
{
|
|
||||||
provide: TokenInjectionToken,
|
|
||||||
useClass: factory.useClass,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
exports: [TelegramBot, TelegramClient],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
1811
package-lock.json
generated
Normal file
1811
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
85
package.json
85
package.json
@ -1,38 +1,67 @@
|
|||||||
{
|
{
|
||||||
"name": "nest-telegram",
|
"name": "nestjs-telegraf",
|
||||||
"version": "0.5.1",
|
"version": "0.6.0",
|
||||||
"main": "dist/index.js",
|
"description": "Telegraf module for Nest framework",
|
||||||
"types": "dist/index.d.ts",
|
"keywords": [
|
||||||
"repository": "git@github.com:igorkamyshev/nest-telegram.git",
|
"nest",
|
||||||
"author": "Igor Kamyshev <igor@kamyshev.me>",
|
"nestjs",
|
||||||
|
"nodejs",
|
||||||
|
"typescript",
|
||||||
|
"telegraf",
|
||||||
|
"telegram",
|
||||||
|
"telegram bot",
|
||||||
|
"telegram bot api",
|
||||||
|
"bot",
|
||||||
|
"bot api",
|
||||||
|
"bot framework"
|
||||||
|
],
|
||||||
|
"homepage": "https://github.com/bukhalo/nestjs-telegraf#readme",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/bukhalo/nestjs-telegraf/issues",
|
||||||
|
"email": "aleksandr@bukhalo.com"
|
||||||
|
},
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"author": "Aleksandr Bukhalo <aleksandr@bukhalo.com>",
|
||||||
|
"contributors": [
|
||||||
|
"Aleksandr Bukhalo <aleksandr@bukhalo.com> (https://bukhalo.com/)",
|
||||||
|
"Igor Kamyshev <igor@kamyshev.me> (https://kamyshev.me/)"
|
||||||
|
],
|
||||||
|
"repository": "git@github.com:bukhalo/nestjs-telegraf.git",
|
||||||
|
"scripts": {
|
||||||
|
"build": "rm -rf dist && tsc -p tsconfig.json",
|
||||||
|
"precommit": "lint-staged",
|
||||||
|
"prepublish:npm": "npm run build",
|
||||||
|
"publish:npm": "npm publish --access public"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"lodash": "^4.17.13"
|
||||||
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@solid-soda/scripts": "^1.2.4",
|
"@nestjs/common": "^6.7.0",
|
||||||
"@team-griffin/install-self-peers": "^1.1.1",
|
"@nestjs/core": "^6.7.0",
|
||||||
"@types/lodash": "^4.14.121"
|
"@types/lodash": "^4.14.121",
|
||||||
|
"husky": "^4.0.7",
|
||||||
|
"lint-staged": "^9.5.0",
|
||||||
|
"prettier": "^1.19.1",
|
||||||
|
"reflect-metadata": "^0.1.13",
|
||||||
|
"telegraf": "^3.35.0",
|
||||||
|
"typescript": "^3.7.4"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@nestjs/common": "^6.7.0",
|
||||||
|
"@nestjs/core": "^6.7.0",
|
||||||
|
"reflect-metadata": "^0.1.13",
|
||||||
|
"telegraf": "^3.35.0"
|
||||||
},
|
},
|
||||||
"husky": {
|
"husky": {
|
||||||
"hooks": {
|
"hooks": {
|
||||||
"pre-commit": "yarn soda lint-staged",
|
"pre-commit": "lint-staged"
|
||||||
"commit-msg": "yarn soda commitlint"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"publishConfig": {
|
"lint-staged": {
|
||||||
"access": "public"
|
"*.ts": [
|
||||||
},
|
"prettier --write",
|
||||||
"scripts": {
|
"git add"
|
||||||
"build": "rimraf dist && tsc",
|
]
|
||||||
"prepare": "install-self-peers -- --ignore-scripts && yarn build",
|
|
||||||
"ci": "yarn soda lint",
|
|
||||||
"s": "yarn soda"
|
|
||||||
},
|
|
||||||
"peerDependencies": {
|
|
||||||
"@nestjs/common": "^5.7.3",
|
|
||||||
"@nestjs/core": "^5.7.3",
|
|
||||||
"reflect-metadata": "^0.1.13"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"lodash": "^4.17.13",
|
|
||||||
"telegraf": "^3.27.1"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,18 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"lib": ["es2017"],
|
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"noImplicitAny": false,
|
"noImplicitAny": false,
|
||||||
"removeComments": true,
|
"removeComments": true,
|
||||||
"noLib": false,
|
"noLib": false,
|
||||||
"allowSyntheticDefaultImports": true,
|
|
||||||
"emitDecoratorMetadata": true,
|
"emitDecoratorMetadata": true,
|
||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
"target": "es6",
|
"target": "es6",
|
||||||
"sourceMap": true,
|
"sourceMap": false,
|
||||||
"outDir": "./dist",
|
"outDir": "./dist",
|
||||||
"baseUrl": "./lib"
|
"rootDir": "./lib",
|
||||||
|
"skipLibCheck": true
|
||||||
},
|
},
|
||||||
"include": ["lib/**/*"],
|
"include": ["lib/**/*", "../index.ts"],
|
||||||
"exclude": ["node_modules"]
|
"exclude": ["node_modules", "**/*.spec.ts"]
|
||||||
}
|
}
|
||||||
|
29
tslint.json
Normal file
29
tslint.json
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"defaultSeverity": "error",
|
||||||
|
"extends": ["tslint:recommended"],
|
||||||
|
"jsRules": {},
|
||||||
|
"rules": {
|
||||||
|
"quotemark": [true, "single", "avoid-escape"],
|
||||||
|
"object-literal-sort-keys": false,
|
||||||
|
"ordered-imports": [
|
||||||
|
true,
|
||||||
|
{
|
||||||
|
"import-sources-order": "any",
|
||||||
|
"named-imports-order": "any"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"no-console": [false],
|
||||||
|
"interface-name": [true, "never-prefix"],
|
||||||
|
"eofline": false,
|
||||||
|
"linebreak-style": [false],
|
||||||
|
"indent": false,
|
||||||
|
"member-access": false,
|
||||||
|
"ban-types": false,
|
||||||
|
"max-classes-per-file": [false],
|
||||||
|
"member-ordering": [false],
|
||||||
|
"no-var-requires": false,
|
||||||
|
"one-line": [false],
|
||||||
|
"array-type": [false]
|
||||||
|
},
|
||||||
|
"rulesDirectory": []
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user