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 🙏

© 2025 – Pkg Stats / Ryan Hefner

telegram-bot-commands-api

v1.0.2

Published

Commands API for Telegram Bot in node-telegram-bot-api

Readme

📦 Telegram Command API

Telegram Command API is a small wrapper for Node.js that makes handling commands and subcommands with node-telegram-bot-api much easier.


🚀 Installation

from npm:

npm i telegram-bot-commands-api

✨ Basic Example

const TelegramBot = require("node-telegram-bot-api");
const { Command, SubCommand, CommandAPI, StringArgument } = require("telegram-command-api");

const bot = new TelegramBot("YOUR-TOKEN", { polling: true });
const api = new CommandAPI(bot);

// /hello
const hello = new Command("/hello", true)
  .executes(({ msg, bot }) => {
    bot.sendMessage(msg.chat.id, "Hello!");
  });

api.register(hello);

console.log("Bot started!");

📖 Wiki

🔹 Command

Defines a main command (/start)

const startCommand = new Command("/start", true)
  .executes(({ msg, bot }) => {
    bot.sendMessage(msg.chat.id, `Hello ${msg.from.id}!`);
  });

🔹 SubCommand

Adds a subcommand to a main command.

const registerCommand = new Command("/register", true)
  .withSubCommand(
    new SubCommand("name")
      .withArguments(new StringArgument("name"))
      .executes(({ msg, args, bot }) => {
        bot.sendMessage(msg.chat.id, `Registered with name ${args[0]}!`);
      })
  )
  .withSubCommand(
    new SubCommand("hi")
      .executes(({ msg, bot }) => {
        bot.sendMessage(msg.chat.id, "You wrote /register hi");
      })
  );

🔹 Arguments

Currently supports:

  • StringArgument("name") → captures a single world argument.
  • NumberArgument("name") → captures a numeral argument.

Examples:

new SubCommand("say")
  .withArguments(new StringArgument("message"))
  .executes(({ msg, args, bot }) => {
    bot.sendMessage(msg.chat.id, `You said: ${args[0]}`);
  });
new SubCommand("say")
  .withArguments(new NumberArgument("number"))
  .executes(({ msg, args, bot }) => {
    bot.sendMessage(msg.chat.id, `You wrote number ${args[0]}`);
  });

🔹 Requires (permissions)

You can restrict the usage of a command/subcommand with .require(...). The callback must return true or false.

Example:

new SubCommand("vip")
  .require(async ({ msg, bot }) => {
    const member = await bot.getChatMember("-1001234567890", msg.from.id);
    return ["creator", "administrator", "member"].includes(member.status);
  })
  .executes(({ msg, bot }) => {
    bot.sendMessage(msg.chat.id, "You are in the VIP group ✅");
  });

🛠 API Reference

Command

  • .executes(handler) → runs when the command is triggered
  • .withSubCommand(sub) → adds a subcommand

SubCommand

  • .withArguments(arg) → adds an argument
  • .executes(handler) → execution handler
  • .require(fn) → async function that must return true/false

📌 Final Example

const bot = new TelegramBot("TOKEN", { polling: true });
const api = new CommandAPI(bot);

const start = new Command("/start", true)
  .executes(({ msg, bot }) => {
    bot.sendMessage(msg.chat.id, "Main /start command");
  })
  .withSubCommand(
    new SubCommand("hello")
      .executes(({ msg, bot }) => bot.sendMessage(msg.chat.id, "Hello!"))
  )
  .withSubCommand(
    new SubCommand("name")
      .withArguments(new StringArgument("name"))
      .executes(({ msg, args, bot }) => bot.sendMessage(msg.chat.id, `Hello ${args[0]}!`))
  );

api.register(start);