项目作者: panarama360

项目描述 :
ts-telegraf-decorators
高级语言: TypeScript
项目地址: git://github.com/panarama360/ts-telegraf-decorators.git
创建时间: 2018-09-16T19:05:25Z
项目社区:https://github.com/panarama360/ts-telegraf-decorators

开源协议:

下载


ts-telegraf-decorators

ts-telegraf-decorators

This is a simple library that will allow you to use decorators and typescript. Based on telegraf

Installation

  1. $ npm install ts-telegraf-decorators

Base Examples

Create controller

  1. import {Start, Command, TFController} from 'ts-telegraf-decorators'
  2. @TFController()
  3. export class ControllerTest {
  4. @Start()
  5. start(@TFContext() ctx){
  6. ctx.reply('Hello start')
  7. }
  8. @Command('ping')
  9. ping(@TFContext() ctx){
  10. ctx.reply('pong')
  11. }
  12. }

Create app.ts

  1. import {buildBot} from "ts-telegraf-decorators";
  2. //import {ControllerTest} from "./controllers/ControllerTest";
  3. buildBot({
  4. token: process.env.BOT_TOKEN,
  5. // bot: bot bot instance
  6. // session: session() custom session
  7. controllers: [__dirname+'/controllers/**.js'],
  8. // or controllers: [ControllerTest],
  9. }).startPolling()

If Use Container

Create service

  1. import {Service} from "typedi";
  2. @Service()
  3. export class TestService {
  4. async getBotName(): Promise<string>{
  5. return 'My Bot'
  6. }
  7. }

Use Container

  1. import 'reflect-metadata'
  2. import {buildBot} from "ts-telegraf-decorators";
  3. //import {ControllerTest} from "./controllers/ControllerTest";
  4. import {Container} from "typedi";
  5. buildBot({
  6. token: process.env.BOT_TOKEN,
  7. container: Container,
  8. // bot: bot bot instance
  9. // session: session() custom session
  10. controllers: [__dirname+'/controllers/**.js'],
  11. // or controllers: [ControllerTest],
  12. }).startPolling()

Create controller

  1. @TFController()
  2. export class ControllerTest {
  3. @Inject()
  4. service: TestService
  5. @Start()
  6. start(@TFContext() ctx){
  7. ctx.reply('Hello start')
  8. }
  9. @Command('ping')
  10. async ping(@TFContext() ctx){
  11. ctx.reply('pong '+ await this.service.getBotName())
  12. }
  13. }
  14. `

Create Wizard

  1. @TFWizard('steps')
  2. export class WizardController {
  3. @TFWizardStep(1)
  4. hello(@TFContext() ctx) {
  5. console.log('step 1');
  6. return ctx.wizard.next();
  7. }
  8. @TFWizardStep(2)
  9. hello2(@TFContext() ctx) {
  10. console.log('step 2');
  11. return ctx.wizard.next();
  12. }
  13. @TFWizardStep(3)
  14. @Hears('hello')
  15. hello3(@TFContext() ctx) {
  16. console.log('step 3');
  17. return ctx.wizard.next();
  18. }
  19. @TFWizardStep(3)
  20. @Command('test')
  21. hello4(@TFContext() ctx) {
  22. console.log('step 3');
  23. return ctx.wizard.next();
  24. }
  25. @TFWizardStep(3)
  26. @Command('hello')
  27. hello5(@TFContext() ctx) {
  28. console.log('step 3')
  29. return ctx.scene.leave();
  30. }
  31. @Command('exit')
  32. exit(@TFContext() ctx) {
  33. return ctx.scene.leave();
  34. }
  35. @Leave()
  36. leave(@TFContext() ctx) {
  37. console.log('Leave');
  38. }
  39. }
  40. `

Create Custom Inject Parameters

  1. export const CurrentUser = createParamDecorator(ctx => {
  2. return (ctx as any).user;
  3. })

Use Parameters

  1. @TFController()
  2. class AnyController {
  3. @Help()
  4. async enter(@TFContext()ctx, @CurrentUser() user) {
  5. ctx.reply('My name is ' + await this.service.getBotName())
  6. }
  7. }