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 instancevar 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/webhookwebhook; webhook; webhook;
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 appwebhook; // option 2 - mount the appvar app = ;app;app;
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 instancevar webhook = webhookSecret: WEBHOOK_SECRET { // mock mongo query dbmessages; };
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 dashboardvar STATUS_URL = '...'; // public facing webhook url // returns an express app instancevar webhook = // passed as express middleware // handles both incoming messages and status notifications { var secret = reqbodysecret || ''; var event = reqbodyevent; // message status notification if EVENT_INCOMING_MESSAGE !== event var split = secret; secret = split0; // add the server identifier on the message for convenience reqbody__id = split1; // assert secret is correct if WEBHOOK_SECRET === secret ; else // send Forbidden resstatus403; } { // mock dbmessages; }; // you can now get to the internal message idwebhook;
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 git@github.com: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 > telerivet-webhook@0.0.1 test /root/jksdua/telerivet-webhook> mocha --bail --timeout 10000 --reporter spec Timeout disabled
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