telebotframework

0.11.2 • Public • Published

ABANDONED

If you wish to take over the project, or the name, contact me

telebotframework

A framework for easy creation of bots for Telegram The framework relies on ES2015 to work, so older versions of node are not supported.

How to create a bot

Creating a bot is very simple. The following code initializes a bot

var TelegramBot = require("telebotframework").TelegramBot;
 
var SomeBot = new TelegramBot("the bot's very secret token");

The bot is now ready to do anything you tell it to do. Let's first tell it to listen for updates automatically:

SomeBot.startLongpolling();

The bot will now automatically fetch whatever is sent to it, by internally using the getUpdates method of teleapiwrapper. You can access the messages by binding to events on the bot:

SomeBot.on("text", message => {
  console.log("I got some text!! The text I got was '" + message.text + "'");
});
SomeBot.on("command", message => {
  console.log("I got a command!! The command was '" + message.command + "' and the arguments to the command were '" + message.argstring + "'");
});
SomeBot.on("file", message => {
  console.log("I got a file!! Saving it to 'Somefile.file'");
  message.saveFile("Somefile.file");
});

There are many more events. See the docs for more.

Answering to messages is also simple:

SomeBot.on("text", message => {
  message.answer("I got your message!");
});

This will send the text "I got your message!" to the chat the message originated from. You want to reply to a message? Easy!

SomeBot.on("text", message => {
  message.reply("I replied to your message!");
});

These methods return promises, which you can hook onto with .then and .catch to get notified when the messages are delivered or failed to deliver.

More advanced use

The original message object received from getUpdates or setWebhook in the API is available on each message object received through the events. Look at the rawMessage property of the messages.

Your bot has a property API. This is an instance of teleapiwrappers BotAPI. If you need more advanced capabilities than the methods in this framework can provide, like sending a reply keyboard with your message, or sending a file, you can invoke the methods of teleapiwrapper directly through this property.

Also, if you prefer to use webhooks or some other method of getting the update instead of going through the bot's built-in longpolling, there is a method for you. Your bot has the method processUpdates, which takes an array of raw Update objects

The documentation

Everything in the framework is documented with JSDoc. The documentation is available in node_modules/telebotframework/docs/index.html. Use it well. They are also readable on https://doc.suppen.no/telebotframework

Changelog

  • 0.11.1: Updated the documentation
  • 0.11.0: Gave message objects an "id" property, and deprecated "sender" in favor of "from". "sender" still works
  • 0.10.3: Fixed a bug where a promise was not returned when a file was saved

Readme

Keywords

Package Sidebar

Install

npm i telebotframework

Weekly Downloads

1

Version

0.11.2

License

ISC

Last publish

Collaborators

  • suppen