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

bot22api

v0.0.7

Published

Framework for Bot APIs

Readme

Framework for Bot APIs

built for the Telegram Bot API

npm npm npm GitHub stars

Documentation: https://sk22.github.io/botapijs

Telegram

For use with the Telegram Bot API, please use the extended package bot22api-telegram!

const bot22api = require('bot22api');
const { Bot, Handler, Requirement } = bot22api;
const { parsers, requires } = bot22api;

const bot = new Bot({
  apiUrl: 'https://somebotapi.io/?token=t84lGDUhW0SZq5cj/',
  parser: parsers.json
});

bot.register(new Handler('dbHandler', (done, { data }) => {
  console.log(data);
  done();
}));

bot.register(new Handler({
  name: 'textHandler',
  callable: function textHandler(done) {
    this.send('message', {
      user: this.data.user,
      text: `Hello ${this.data.user}!`
    });
    done();
  },
  requires: [
    requires.has('text'),
    new Requirement('helloChecker', (done, processor) => {
      if (processor.data.text === 'hello') done(); // no error
      else done(true); // error
    })
  ]
}));

bot.listen(3000, 'localhost');

installation

npm install bot22api

getting started

First of all require all needed dependencies.

const bot22api = require('bot22api');
const { Bot, Handler } = bot22api;

Bot /docs/

To create a new bot, call its constructor.

const bot = new Bot({
  apiUrl: 'https://somebotapi.io/?token=t84lGDUhW0SZq5cj/',
  route: /.*/,
  parser: bot22api.parsers.json,
  server: undefined, // will be created automatically
  log: null // will send no logs if null. npmlog by default
});

Now that you have a Bot object, you need to register so-called Handlers using the Bot's register method. Each Callable (Handler or Requirement) must have a unique name.

Bot#register /docs/

bot.register(handler);

Handler /docs/

Each time the bot receives a HTTP request, it'll try to execute all handlers that is has. A Handler can also have Requirements, that do almost the same as Handlers, but they check if the data can be used by the Handler. If one Requirement fails, the Handler won't execute.

bot.register(new Handler({
  name: 'loggerHandler',
  callable: (done, { data }) => {
    console.log(data);
    done();
  }
}));

Callable /docs/

All Callable-extending classes (Handler and Requirement) can also be constructed by passing the Callable name, the Callable function and optional Requirements.

This is what all Callables have in common. Mind that requires is optional!

Callable {
  name: string,
  callable: function,
  [requires]: Requirement[]
}

Callables themselves can not be used in any way. Do always construct either Handlers or Requirements.

By using a named function, you do not need to pass the name as a string, as the name is already given. See the documentation for further information about the construction of Callables.

Danger!

You can't simply filter out the send method from the processor and call it! Since send accesses the bot through the processor, it needs to get properties of this, which does not exist after you extracted it from the processor.

For workarounds, take a look at the documentation: Processor#send

Requirement /docs/

A Requirement constructs like any other Callable: Like a Handler. The only big difference to a Handler is when they are used.

Requirements perform checks or modifications on the data that the handler relies on. Requirements can also depend on other Requirements.

In any Callable, the requirements are represented in an Array named requires. For example, here's a Handler with some Requirements.

const { Requirement, requires } = bot22api;
new Handler({
  name: 'requirementShowcaseHandler',
  callable: (done) => {
    console.log('All Requirements have passed.'); done();
  },
  requires: [
    // checks if user.type === 'admin'
    requires.is('admin', 'user', 'type'),
    // using the (functionName, requires) constructor, see Callable doc
    new Requirement(function textNotEmpty(done, { data }) {
      if (typeof data.text === 'string' && data.text.length === 0) done();
      else done(true); // error -> done(err)
    }, [ requires.has('text') ])
  ]
});

This results in a Requirements tree like:

  • requirementShowcaseHandler
    • isUserTypeAdmin
      • hasUserType
    • textNotEmpty
    • hasText

Processor /docs/

When a HTTP request invokes a bot, the bot creates a Processor for all Handlers. As the Handlers and Requirement so get embedded into an environment that contains all current states, properties like the passed API URL and route can be retrieved from that object.

You can use the processor while inside a Callable function (Handler or Requirement)

new Handler({
  callable: (done, processor) => {
    console.log('Cool?', processor.handler.options.beCool);
    processor.send('message', `I received ${processor.data} ` +
      `on route ${processor.route}`);
  },
  options: { beCool: true }
});

Finalization

Once everything is set up, you need to tell the bot's server to start listening. To do that, simply call the Bot's listen method. If your server runs more than one Bot, call the Server's listen method instead.

bot.listen(3000, 'localhost');

Now to make HTTP requests, call your URL, which consists of your hostname, port, an optional route followed by ?url= and the set API URL. The data is submit in the POST request's body.

So just give the full URL to your bot API.

https://yourbot.rhcloud.com/yourRoute?url=https://somebotapi.io/?token=t84lGDUhW0SZq5cj/

license

MIT