Merge pull request #1 from bukhalo/some-work

chore(): publish 0.6.0 release
This commit is contained in:
Aleksandr Bukhalo 2020-01-12 15:32:32 +03:00 committed by GitHub
commit 57baf28809
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
36 changed files with 2106 additions and 4307 deletions

19
.gitignore vendored
View File

@ -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

7
.npmignore Normal file
View File

@ -0,0 +1,7 @@
# source
lib
index.ts
package-lock.json
tslint.json
tsconfig.json
.prettierrc

4
.prettierrc Normal file
View File

@ -0,0 +1,4 @@
{
"trailingComma": "none",
"singleQuote": true
}

View File

@ -1,6 +1,6 @@
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
of this software and associated documentation files (the "Software"), to deal

171
README.md
View File

@ -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
```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()
```bash
$ npm i nestjs-telegraf telegraf
```
## 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
import { Injectable } from '@nestjs/common'
import { Context, PipeContext, TelegramActionHandler } from 'nest-telegram'
## People
@Injectable()
export class HelpActions {
@TelegramActionHandler({ onStart: true })
async start(ctx: Context) {
await ctx.reply('Hello!')
}
}
```
- Authors - [Aleksandr Bukhalo](https://bukhalo.com/) & [Igor Kamyshev](https://kamyshev.me/)
- Maintainers - [Aleksandr Bukhalo](https://bukhalo.com/)
- Website - [https://nestjs.com](https://nestjs.com/)
Available actions for decorator:
## License
+ `onStart` {boolean}, it triggers on `/start` command.
+ `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. 🤓
Nest is [MIT licensed](https://github.com/nestjs/nest/blob/master/LICENSE).

1
index.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export * from './dist';

6
index.js Normal file
View 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
index.ts Normal file
View File

@ -0,0 +1 @@
export * from './dist';

View File

@ -1,4 +0,0 @@
import Telegraf from 'telegraf'
import { Context } from './Context'
export type Bot = Telegraf<Context>

View File

@ -1,3 +0,0 @@
import { ContextMessageUpdate } from 'telegraf'
export type Context = ContextMessageUpdate

View File

@ -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',
})
}
}

View File

@ -1,4 +0,0 @@
export interface TelegramModuleOptions {
token: string
sitePublicUrl?: string
}

View File

@ -1,5 +0,0 @@
import { TelegramModuleOptions } from './TelegramModuleOptions'
export interface TelegramModuleOptionsFactory {
createOptions(): TelegramModuleOptions
}

3
lib/decorators/index.ts Normal file
View File

@ -0,0 +1,3 @@
export * from './pipe-context.decorator'
export * from './telegram-action-handler.decorator'
export * from './telegram-catch.decorator'

View File

@ -1,6 +1,6 @@
import { Type } from '@nestjs/common'
import { ContextTransformer } from '../ContextTransformer'
import { addHandlerToStore } from './TelegramActionHandler'
import { ContextTransformer } from '../interfaces'
import { addHandlerToStore } from './'
export const PipeContext = <T>(transform: Type<ContextTransformer<T>>) => (
target: Object,

View File

@ -1,4 +1,4 @@
import { HandleParameters } from '../HandleParameters'
import { HandleParameters } from '../interfaces'
type Decorator = (
params: HandleParameters,

View File

@ -1,5 +1,5 @@
import { Type } from '@nestjs/common'
import { TelegramErrorHandler } from '../interfaces/TelegramErrorHandler'
import { TelegramErrorHandler } from '../interfaces'
type Decorator = (error: any) => ClassDecorator

1
lib/exeptions/index.ts Normal file
View File

@ -0,0 +1 @@
export * from './invalid-configuration.exeption'

View File

@ -1,15 +1,5 @@
export { TelegramModule } from './telegram.module'
export { TelegramBot } from './TelegramBot'
export { TelegramClient } from './TelegramClient'
export { TelegramModuleOptionsFactory } from './TelegramModuleOptionsFactory'
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'
export * from './telegraf.module'
export * from './interfaces'
export * from './decorators'
export * from './telegraf.service'
export * from './telegraf-telegram.service'

View File

@ -1,4 +1,4 @@
import { ContextTransformer } from './ContextTransformer'
import { ContextTransformer } from './'
import { Type } from '@nestjs/common'
interface ArgumentTransformation {

View File

@ -1,4 +1,4 @@
import { HandleParameters } from './HandleParameters'
import { HandleParameters } from './'
export interface Handler {
handle: (...args: any[]) => Promise<void>

5
lib/interfaces/index.ts Normal file
View 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'

View 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[]
}

View 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, {});
}
}

View File

@ -1 +1,2 @@
export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS'
export const TokenInjectionToken = Symbol('TokenInjectionToken')

63
lib/telegraf.module.ts Normal file
View 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]
};
}
}

View File

@ -1,29 +1,29 @@
import { Injectable, Inject } from '@nestjs/common'
import { Injectable, Inject, Logger } from '@nestjs/common'
import { ModuleRef } from '@nestjs/core'
import Telegraf, { ContextMessageUpdate } from 'telegraf'
import { flatten, head } from 'lodash'
import { ContextTransformer } from './ContextTransformer'
import { TelegramCatch } from './decorators/TelegramCatch'
import { TelegramErrorHandler } from './interfaces/TelegramErrorHandler'
import { Handler } from './Handler'
import { Bot } from './Bot'
import { TelegramActionHandler } from './decorators/TelegramActionHandler'
import { TokenInjectionToken } from './TokenInjectionToken'
import { TelegramModuleOptionsFactory } from './TelegramModuleOptionsFactory'
import { InvalidConfigurationException } from './InvalidConfigurationException'
import { TelegramCatch, TelegramActionHandler } from './decorators'
import { TokenInjectionToken } from './telegraf.constants'
import {
TelegrafOptionsFactory,
TelegramErrorHandler,
Handler,
ContextTransformer,
} from './interfaces'
import { InvalidConfigurationException } from './exeptions'
@Injectable()
export class TelegramBot {
export class TelegrafService {
private readonly logger = new Logger(TelegrafService.name, true)
private readonly sitePublicUrl?: string
private readonly bot: Bot
private readonly bot: Telegraf<ContextMessageUpdate>
private ref: ModuleRef
public constructor(
@Inject(TokenInjectionToken) factory: TelegramModuleOptionsFactory,
@Inject(TokenInjectionToken) options: TelegrafOptionsFactory,
) {
const { token, sitePublicUrl } = factory.createOptions()
const { token, sitePublicUrl } = options.createTelegrafOptions()
this.sitePublicUrl = sitePublicUrl
this.bot = new Telegraf(token)
}
@ -54,7 +54,7 @@ export class TelegramBot {
this.bot.telegram
.setWebhook(url)
.then(() => console.log(`Webhook set success @ ${url}`))
.then(() => this.logger.log(`Webhook set success @ ${url}`))
return this.bot.webhookCallback(`/${path}`)
}

View File

@ -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

File diff suppressed because it is too large Load Diff

View File

@ -1,38 +1,67 @@
{
"name": "nest-telegram",
"version": "0.5.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"repository": "git@github.com:igorkamyshev/nest-telegram.git",
"author": "Igor Kamyshev <igor@kamyshev.me>",
"name": "nestjs-telegraf",
"version": "0.6.0",
"description": "Telegraf module for Nest framework",
"keywords": [
"nest",
"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",
"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": {
"@solid-soda/scripts": "^1.2.4",
"@team-griffin/install-self-peers": "^1.1.1",
"@types/lodash": "^4.14.121"
"@nestjs/common": "^6.7.0",
"@nestjs/core": "^6.7.0",
"@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": {
"hooks": {
"pre-commit": "yarn soda lint-staged",
"commit-msg": "yarn soda commitlint"
"pre-commit": "lint-staged"
}
},
"publishConfig": {
"access": "public"
},
"scripts": {
"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"
"lint-staged": {
"*.ts": [
"prettier --write",
"git add"
]
}
}

View File

@ -1,19 +1,18 @@
{
"compilerOptions": {
"lib": ["es2017"],
"module": "commonjs",
"declaration": true,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"allowSyntheticDefaultImports": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"sourceMap": false,
"outDir": "./dist",
"baseUrl": "./lib"
"rootDir": "./lib",
"skipLibCheck": true
},
"include": ["lib/**/*"],
"exclude": ["node_modules"]
"include": ["lib/**/*", "../index.ts"],
"exclude": ["node_modules", "**/*.spec.ts"]
}

29
tslint.json Normal file
View 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": []
}

4007
yarn.lock

File diff suppressed because it is too large Load Diff