Framework for Bot APIs
built for the Telegram Bot API
Documentation: https://sk22.github.io/botapijs
Telegram
For use with the Telegram Bot API, please use the extended package bot22api-telegram!
const bot22api = ;const Bot Handler Requirement = bot22api;const parsers requires = bot22api; const bot = apiUrl: 'https://somebotapi.io/?token=t84lGDUhW0SZq5cj/' parser: parsersjson; bot; bot; bot;
installation
npm install bot22api
getting started
First of all require all needed dependencies.
const bot22api = ;const Bot Handler = bot22api;
/docs/
BotTo create a new bot, call its constructor.
const bot = apiUrl: 'https://somebotapi.io/?token=t84lGDUhW0SZq5cj/' route: /.*/ parser: bot22apiparsersjson 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.
/docs/
Bot#registerbot;
/docs/
HandlerEach 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;
/docs/
CallableAll 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
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
/docs/
RequirementA 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;
name: 'requirementShowcaseHandler' { console; ; } requires: // checks if user.type === 'admin' requires // using the (functionName, requires) constructor, see Callable doc { if typeof datatext === 'string' && datatextlength === 0 ; else ; // error -> done(err) } requires ;
This results in a Requirements tree like:
- requirementShowcaseHandler
- isUserTypeAdmin
- hasUserType
- textNotEmpty
- hasText
- isUserTypeAdmin
/docs/
ProcessorWhen 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)
{ console; processor; } 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;
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/