Skip to content

boomerangjs/ts-telegraf-decorators

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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(ctx: any){
        ctx.reply('Hello start')
    }
    
    @Command('ping')
    ping(ctx: any){
        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

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

@TFController()
export class ControllerTest {

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

Releases

No releases published

Packages

 
 
 

Languages

  • TypeScript 100.0%