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

telerivet-webhook

v0.0.1

Published

Node.js webhook for the Telerivet SMS gateway service

Downloads

4

Readme

Telerivet Webhook

Node.js webhook for the Telerivet SMS gateway service

Installation

$ npm install telerivet-webhook

Usage

Get a webhook instance

var WEBHOOK_SECRET = '...';

// returns an express app instance
var webhook = require('telerivet-webhook')({
	webhookSecret: WEBHOOK_SECRET
});

Listen for events

Note: All events are prefixed with telerivet:: to avoid collisions with internal events.

Additional to the API, status of the message is also emitted as an event. So, messages will also emit sent, delivered, failed etc.

var EVENT_INCOMING_MESSAGE = 'telerivet::incoming_message';
var EVENT_MESSAGE_STATUS = 'telerivet::send_status';
var EVENT_MESSAGE_SENT = 'telerivet::sent';

// parameters and event names are exactly the same as the documentation at https://telerivet.com/api/webhook
webhook.on(EVENT_INCOMING_MESSAGE, function(message) {
	console.log(message.id);
	console.log(message.type);
});

webhook.on(EVENT_MESSAGE_STATUS, function(message) {
	console.log(message.status);
});

webhook.on(EVENT_MESSAGE_SENT, function(message) {
	console.log(message.id);
	console.log(message.type);
});

Run the app

webhook is an express app so you can either run the app directly or mount it in another express app or http server.

// option 1 - run the app
webhook.listen(3000);

// option 2 - mount the app
var app = express();
app.use('/telerivet/webhook', webhook);
app.listen(3000);

Sending auto replies

Optionally, you can set autoReply in the options hash to send autoreplies.

Note: You need to manually set the status_secret and status_url properties on the messages. Use the below code as a template. It is really important that you set status_secret in order to verify incoming requests.

var WEBHOOK_SECRET = '...';
var STATUS_URL = '...'; // public facing webhook url

// returns an express app instance
var webhook = require('telerivet-webhook')({
	webhookSecret: WEBHOOK_SECRET,
	autoReply: function(req, res) {
		// mock mongo query
		db.messages.find({}, function(err, messages) {
			res.json({
				messages: messages.map(function(message) {
					return {
						status_secret: WEBHOOK_SECRET,
						status_url: STATUS_URL,
						to_number: message.to,
						content: message.content
					};
				});
			});
		});
	}
});

The above example works quite well but it would be difficult to track the message for which the Telerivet server sends the status notification. The below example shows how to handle this situation by using a custom webhook secret verifier.

var EVENT_MESSAGE_DELIVERED = 'telerivet::delivered';
var EVENT_INCOMING_MESSAGE = 'telerivet::incoming_message';
var WEBHOOK_SECRET = '...'; // as setup in the telerivet dashboard
var STATUS_URL = '...'; // public facing webhook url

// returns an express app instance
var webhook = require('telerivet-webhook')({
	// passed as express middleware
		// handles both incoming messages and status notifications
	webhookSecret: function(req, res, next) {
		var secret = req.body.secret || '';
		var event = req.body.event;

		// message status notification
		if (EVENT_INCOMING_MESSAGE !== event) {
			var split = secret.split(':');
			secret = split[0];
			// add the server identifier on the message for convenience
			req.body.__id = split[1];
		}

		// assert secret is correct
		if (WEBHOOK_SECRET === secret) {
			next();
		} else {
			// send Forbidden
			res.status(403).end();
		}
	},
	autoReply: function(req, res) {
		// mock
		db.messages.find({}, function(err, messages) {
			res.json({
				messages: messages.map(function(message) {
					return {
						// append the message id to the secret
							// it is very important that we use the webhook secret as well as the message id here. if we only use the message id, an attacker simply needs to know a valid database identifier which may be weak (incremental - mysql, time based - mongo etc)
						status_secret: WEBHOOK_SECRET + ':' + message._id,
						status_url: STATUS_URL,
						to_number: message.to,
						content: message.content
					};
				});
			});
		});
	}
});

// you can now get to the internal message id
webhook.on(EVENT_MESSAGE_DELIVERED, function(message) {
	console.log(message.__id); // internal id
	console.log(message.id); // telerivet id
});

Testing

A test account has been created on the production Telerivet service. This can be used for testing this library.

Tests can be run with the following commands:

# clone the repo
$ git clone [email protected]:jksdua/telerivet-webhook.git
$ cd telerivet-webhook

# install dependencies
$ npm install

# run tests
$ npm test

Sample test output

root@localhost:~/jksdua/telerivet-webhook# npm test

> [email protected] test /root/jksdua/telerivet-webhook
> mocha --bail --timeout 10000 --reporter spec

Timeout disabled for incoming message tests


  #telerivet-webhook
    #general
      > should throw an error if webhook secret is missing
      > should throw an error if auto reply is not a function
    #incoming message
      #events
Please send a simulated incoming message from https://telerivet.com/p/fccbbed0/services. Click on "Test Services"
Received simulated message. Proceeding with test
        > should emit an event (10661ms)
      #auto reply
Please send a simulated incoming message from https://telerivet.com/p/fccbbed0/services. Click on "Test Services"
Received simulated message. Auto reply will be sent to a test number. Waiting for sent message notification
        > should send the reply (4959ms)
    #message status notification
      > should emit events - telerivet::failed (8170ms)
      > should emit events - telerivet::sent,telerivet::delivered (8021ms)
      > should emit events - telerivet::sent,telerivet::not_delivered (8015ms)
      > should emit events - telerivet::failed_queued,telerivet::sent (8018ms)


  8 passing (49s)

Note: Credentials for the service are located in test/config.json file. Login and modify the webhook service to point to your public facing server.

Changelog

v0.0.1 (23 Sep 2014)

  • Initial commit