npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

telegram-command-handler

v1.4.0

Published

handle commands like as /command arg1 arg2

Downloads

73

Readme

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-

new Command(currentBotObject, command, description);
currentBotObject - instance of TelegramBot
command - Object or String
description - *
if command is Object - gets
{
	name: String command name (without / )
	parser: Number of parser (the list is below). Default = 0
	chatId: Number id of chat. Default = -1 (off)
	userId: Number id of user. Defalut = -1 (off)
}
else gets String command name

List of parsers:

  • Index: 0; Command format: /command arg1 "arg2 ..."; Returns Array [arg1,arg2] with field "=ERRORS" Array []
  • index: 1; Command format: /command name1=value name2="value ..."; Returns Object {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 return commandFInder("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 = require('node-telegram-bot-api');
const Command = require('telegram-command-handler');
const bot = new BotAPI(options.token);//add longpolling, webhook etc

//....

const help = new Command(bot, "help",
`*help*
_Description_: get commands description. 
_Usage_: /help [command] 
_Examples_:
|+- to get all commands print /help
|+- to get about gettrack - /help gettrack`)

help.on("receive", function(msg, args){
	if(!args["=ERRORS"].length)
		bot.sendMessage(msg.chat.id, `args: ${args.join(' ')}`);
});

Warnings

  • this module uses ES6 futures!