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

lambda-telegram-bot-handler

v0.2.0

Published

Library to build and run Telegram bots in AWS Lambda platform

Downloads

4

Readme

lambda-telegram-bot-handler

Library to build and run Telegram bots in AWS Lambda platform. It works using the webhook option of the Telegram bot API (https://core.telegram.org/bots/api#getting-updates), and the AWS API Gateway.

Usage

The library exports a method to easily handle Lambda invocations from Telegram acording to your specified filters.

var telegramHandler = require('lambda-telegram-bot-handler');

exports.handler = telegramHandler({
  onText: [
    {
      matches: /^\/pattern$/,
      handler: function (msg, cb) {
        // process and answer message here
        cb();
      }
    },
    {
      matches: /^\/pattern2 (.+)$/,
      handler: function (msg, cb, matches) {
        // process and answer message here
        // `matches` will have the result of "message".match(/^\/pattern2 (.+)$/)
        cb();
      }
    }
  ],
  onMessage: function (msg, cb) {
    // process and answer message here
    cb();
  }
});

exports.handler is the handler you specify in your Lambda function configuration. So it's the method that will be called on each Lambda invocation.

Only one message handler function will be called for each invocation, so onText handlers take precedence over onMessage, and among onText handlers, the first that matches the message text (in declaration order) will be called. In the example above, if a user sends the message /pattern, the first handler in the onText array will be called. If the user sends /pattern2 hello, the second handler will be called. And finally if the user sends /no_match, the onMessage handler will be called.

Reference

Lambda handler

telegramHandler(opts)

Returns a Lambda handler function: function (event, context) { ... }

opts is an object where you define the message handlers. It has the following properties:

  • onText (array of objects - optional): array of text message handlers. Handlers defined in this array will only process text messages (see https://core.telegram.org/bots/api#message) that match the defined regex. They are defined as an object with the following attributes:
    • matches (regex - mandatory): the regular expression that the message has to match
    • handler (function - mandatory): the actual message handler
  • onLocation (function - optional): location message handler.
  • onMessage (function - optional): fallback message handler. This function will be called for each message received that hasn't been handled by any other defined handlers.
  • onInlineQuery (function - optional): handler for inline queries (https://core.telegram.org/bots/api#inlinequery)
  • onChosenInlineResult (function - optional): handler for choosen inline query results (https://core.telegram.org/bots/api#choseninlineresult)

Message handler function

These are your defined functions to handle Telegram messages. They receive the following arguments:

  • msg: the actual telegram message
  • callback: function to be called when the handler has finished processing the message. NOTE: This callback ends the execution of the Lambda function, so make sure to always call it to avoid unnecessary extra running time.
  • matches: the result of executing regexp.exec on the message text. Will only be present for onText handlers as they run against a regexp.

Setup

To run a bot on AWS Lambda you just need 3 things:

  • A Lambda function with your bot's code (I use Grunt and grunt-aws-lambda to deploy code easily)
  • An API gateway POST endpoint attached to your Lambda function
  • A Telegram bot with its webhook configured to point to your API gateway endpoint (it'll look something like https://123456abc.execute-api.eu-west-1.amazonaws.com/prod/)

License

MIT