telegram-bot-node
Plugins-driven telegram bot on Node.js.
Requirements
- nodejs >= 0.10
- npm >= 1.2.0
Install
npm i --save telegram-bot-node
Quck guide
Setup bot
var path = ;var TelegramBot = Bot;var myBot = '<TOKEN>' name: 'myBeatifulBot' polling: true // Folder with plugins. plugins: path; // Now listen `message` event for polling.myBot;
Plugins
All plugins looks like this:
var plugin = // Type for match a plugin to a message. type: MESSAGE_TYPESCOMMAND // Weight of plugin. If we have 2 or more plugins matched on one type, // then will be call plugin with more weight. weight: 1 // Checking that a plugin is we need. // Returns boolean. True if passed and false if not. { return infodatacommand === 'weather'; } // Function-handler for messages, like: "/weather London". // It will be call if type of message and type of plugin the same. // And test function is passed. {};
Information object:
var info = data: {} // information from message analyzers message: {} // current telegram message user: {} || undefined // telegram user;
For use plugins you must set type of a plugin. Allowed types store in MESSAGE_TYPES
var.
var MESSAGE_TYPES = MESSAGE_TYPES;
Types:
- COMMAND
- LOCATION
- TEXT
- PHOTO
- AUDIO
- VIDEO
- ACTION
- CONTACT
- STICKER
- DOCUMENT
- ALWAYS
- ALL
Example: create weather plugin
Create weather plugin. You can use custom file name, for example ./lib/plugins/commands/weather.js
or simple ./lib/plugins/weather.js
:
var MESSAGE_TYPES = MESSAGE_TYPES;var request = ;// Promises lib, you can use bluebirs, vow or other.var vow = ;var WEATHER_URL = 'http://api.openweathermap.org/data/2.5/weather';var K = 27315; moduleexports = // Match only on commands. type: MESSAGE_TYPESCOMMAND weight: 1 { // Check that the command is `weather`. return infodatacommand === 'weather'; } { // Command `/weather London` has info.data.params = `London` var deferred = vowdefer; var city = infodataparams; request ; return deferred; };