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 🙏

© 2024 – Pkg Stats / Ryan Hefner

telegram-bot-node

v0.2.6

Published

Telegram bot with plugins

Downloads

35

Readme

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 = require('path');
var TelegramBot = require('telegram-bot-node').Bot;
var myBot = new TelegramBot('<TOKEN>', {
  name: 'myBeatifulBot',
  polling: true,
  // Folder with plugins.
  plugins: path.resolve(__dirname, './lib/plugins/')
});

// Now listen `message` event for polling.
myBot.on('message', function (msg) {
  myBot.handle(msg);
});

Plugins

All plugins looks like this:

var plugin = {
  // Type for match a plugin to a message.
  type: MESSAGE_TYPES.COMMAND,
  // 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.
  test: function (info) {
    return info.data.command === '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.
  handler: function (info, bot) {}
};

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 = require('telegram-bot-node').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 = require('telegram-bot-node').MESSAGE_TYPES;
var request = require('superagent');
// Promises lib, you can use bluebirs, vow or other.
var vow = require('vow');
var WEATHER_URL = 'http://api.openweathermap.org/data/2.5/weather';
var K = 273.15;


module.exports = {
  // Match only on commands.
  type: MESSAGE_TYPES.COMMAND,
  weight: 1,
  test: function (info) {
    // Check that the command is `weather`.
    return info.data.command === 'weather';
  },
  handler: function (info, bot) {
    // Command `/weather London` has info.data.params = `London`
    var deferred = vow.defer();
    var city = info.data.params;
    request
      .get(WEATHER_URL)
      .query({q: city})
      .end(function (err, resp, body) {
        if (err || resp.statusCode !== 200) {
          var error = err || new Error('Status code: ' + resp.statusCode));
          console.error(error);
          return deferred.reject(error);
        }
        var result = body;
        var city = result.name;
        var temperature = Math.floor(result.main.temp - K);
        var description = result.weather[0].description;
        bot.sendMessage('Weather in ' + city + ': ' + temperature + '°C. ' + description)
          .then(function (resp) {
            deferred.resolve(resp);
          }, function (reason) {
            deferred.reject(reason);
          });
      });
      return deferred.promise();
  }
};