0.3.5 • Published 3 years ago

ts-telegraf-decorators v0.3.5

Weekly downloads
94
License
-
Repository
github
Last release
3 years ago

ts-telegraf-decorators

ts-telegraf-decorators

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

Installation

$ npm install ts-telegraf-decorators 

Base Examples

Create controller

import {Start, Command, TFController} from 'ts-telegraf-decorators'

@TFController()
export class ControllerTest {

    @Start()
    start(@TFContext() ctx){
        ctx.reply('Hello start')
    }
    
    @Command('ping')
    ping(@TFContext() ctx){
        ctx.reply('pong')
    }
}

Create app.ts

import {buildBot} from "ts-telegraf-decorators";
//import {ControllerTest} from "./controllers/ControllerTest";


buildBot({
    token: process.env.BOT_TOKEN,
    // bot: bot                 bot instance
    // session: session()       custom session
    controllers: [__dirname+'/controllers/**.js'],
    // or controllers: [ControllerTest],
}).startPolling()

If Use Container

Create service

import {Service} from "typedi";

@Service()
export class TestService {

    async getBotName(): Promise<string>{
        return 'My Bot'
    }
}

Use Container

import 'reflect-metadata'
import {buildBot} from "ts-telegraf-decorators";
//import {ControllerTest} from "./controllers/ControllerTest";
import {Container} from "typedi";

buildBot({
    token: process.env.BOT_TOKEN,
    container: Container,
    // bot: bot                 bot instance
    // session: session()       custom session
    controllers: [__dirname+'/controllers/**.js'],
    // or controllers: [ControllerTest],
}).startPolling()

Create controller

@TFController()
export class ControllerTest {

    @Inject()
    service: TestService
    
    @Start()
    start(@TFContext() ctx){
        ctx.reply('Hello start')
    }
    
    @Command('ping')
    async ping(@TFContext() ctx){
        ctx.reply('pong '+ await this.service.getBotName())
    }
}

Create Wizard

@TFWizard('steps')
export class WizardController {


    @TFWizardStep(1)
    hello(@TFContext() ctx) {
        console.log('step 1');
        return ctx.wizard.next();
    }

    @TFWizardStep(2)
    hello2(@TFContext() ctx) {
        console.log('step 2');
        return ctx.wizard.next();
    }

    @TFWizardStep(3)
    @Hears('hello')
    hello3(@TFContext() ctx) {
        console.log('step 3');
        return ctx.wizard.next();
    }

    @TFWizardStep(3)
    @Command('test')
    hello4(@TFContext() ctx) {
        console.log('step 3');
        return ctx.wizard.next();
    }

    @TFWizardStep(3)
    @Command('hello')
    hello5(@TFContext() ctx) {
        console.log('step 3')
        return ctx.scene.leave();
    }

    @Command('exit')
    exit(@TFContext() ctx) {
        return ctx.scene.leave();
    }

    @Leave()
    leave(@TFContext() ctx) {
        console.log('Leave');
    }
}

Create Custom Inject Parameters

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

Use Parameters

@TFController()
class AnyController {
      @Help()
      async enter(@TFContext()ctx, @CurrentUser() user) {
          ctx.reply('My name is ' + await this.service.getBotName())
      }
}
0.3.5

3 years ago

0.3.4

3 years ago

0.3.2

3 years ago

0.3.3

3 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.6

4 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago