telegraf-handlers

1.0.1 • Public • Published

telegraf-handlers

A simple Command and Event Handler Wrapper for telegraf.js library

Example

const { Telegraf } = require("telegraf");
const bot = new Telegraf("your-telegraf-token-here");
const cmdh = require("telegraf-handlers");

// Configure directories
cmdh.setEdirs(
  `${__dirname}\\commands`,
  `${__dirname}/commands/`,
  `${__dirname}\\events`,
  `${__dirname}/events/`
); // You have to use two separate paths for dir and importdir because of path issues

cmdh.handlerInit(bot, cmdh.HANDLERENUM.COMMAND); // Initialises all of the commands

cmdh.handlerInit(bot, cmdh.HANDLERENUM.EVENT); // Initialises all of the events

bot.launch();

Example Command

// /commands/testcmd.js

const { Command } = require("telegraf-handlers");

class TestCmd extends Command {
  constructor() {
    super();
    // used when invoking a command in telegram, /[name]
    this.name = "testcmd";
  }
  // this method gets executed when command is invoked
  execute(ctx, message, args) {
    ctx.reply("Test command");
  }
}

module.exports = TestCmd;

Example Event

// /events/testevent.js

const { Event } = require("telegraf-handlers");

class TestEvent extends Event {
  constructor() {
    super();
    // type of event that gets added to the listener
    this.type = "sticker";
  }
  execute(ctx) {
    ctx.reply("Test event");
  }
}

module.exports = TestEvent;

Readme

Keywords

none

Package Sidebar

Install

npm i telegraf-handlers

Weekly Downloads

1

Version

1.0.1

License

GPL-3.0

Unpacked Size

48.7 kB

Total Files

13

Last publish

Collaborators

  • man_arch