telegram-command-handler
npm i telegram-command-handler
depends on node-telegram-bot-api
Features
- parse commands with/without bot nickname (like as /command@myBot a b ).
- adding description to the command.
- add own command parser or choose among the ready
- set the chatid and userid of which the command will be processed
Usage
1. create new command object-
currentBotObject command description;currentBotObject - instance of TelegramBotcommand - Object or Stringdescription - *
if command is Object - gets name: String command parser: Number of Default = 0 chatId: Number id of chat Default = - userId: Number id of user Defalut = -else gets String command name
List of parsers:
- Index: 0; Command format:
/command arg1 "arg2 ..."
; ReturnsArray [arg1,arg2] with field "=ERRORS" Array []
- index: 1; Command format:
/command name1=value name2="value ..."
; ReturnsObject {name1:value, name2:value, "=ERRORS":[]}
warnings:
- if you do not specify userId AND! chatId, handler will use more efficient function.
- "=ERRORS" array contains invalid fragments of args string.
- object
2.
add listener on command receiving - command.on("receive",function(msg, arguments){...})
msg - normal message object like as in .on("message")
Also listener can be added with command.onReceive(handler)
and removed with command.offReceive(handler)
3.
To add own parser - use Command.addParser(commandFinder(botName, commandName){}, argsParser(argsString){})
(this is static method)
4.
To get parser - use Command.getParser(index)
(this is static method)
-
commandFInder
- function with 2 String args - bot username and command name. Should return a regular expression that checks whether the command is typed and separates the string with parameters. Example of returncommandFInder("myBot","command")
when the current command is/command@myBot arg1 arg2
:[anything, " arg1 arg2"]
As you can see, element number 1 should always contain a string of parameters. -
argsParser
- function with 1 arg - the parameters string that was separated in commandFinder. Should return array, object etc with args.
5.
listening to the text begins asynchronously. Therefore, in order to simulate sending a command, for example, you should wait to end of execution command.initialization
!
Example
const BotAPI = ;const Command = ;const bot = optionstoken;//add longpolling, webhook etc //.... const help = bot "help"`*help*_Description_: get commands description. _Usage_: /help [command] _Examples_:|+- to get all commands print /help|+- to get about gettrack - /help gettrack` help;
Warnings
- this module uses ES6 futures!