Create A Simple Callback Receiver

Create a simple callback receiver for testing

This simple RESTful callback receiver accepts POST requests from ThingSpace, identifies the callback type, and displays the body via console.log(). It can be used as a development and test tool to quickly see real SMS callback requests from ThingSpace.

NOTE: The computer running this Node.js server must have an external URL. ThingSpace cannot send callback messages to a system that does not have a URL.

Requirements

You must have Node.js installed on the computer, and the Node Package Manager (npm) must be up to date.

This project uses the Express web application framework to provide the HTTP functions, and the body-parser package to process POST requests and JSON.

Instructions

1. Create a new directory to hold the project (feel free to use a different name), and then cd into the new directory:

> mkdir CallbackListener
> cd CallbackListener

2. Create a package.json file and copy in this content:

{
  "name": "sms-callback-listener",
  "version": "1.0.0",
  "description": "A simple Node.js app to receive SMS callbacks",
  "author": "Verizon",
  "license": "FSFUL"
}

3. Install the Express web application framework :

> npm install express --save
> npm install body-parser --save

4. Create the cblistener.js file and include this code:

var express = require("express");
var bodyParser = require("body-parser");
var app = express();
 
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
 
app.post("/", function(req, res) {
  var size = Object.keys(req.body).length;
  if(req.body.smsStatus) {
    console.log("\nSMS_DeliveryReport:");
  } else if(req.body.type) {
    //OptIn or OptOut
    if(req.body.type == "OptIn") {
        console.log("\nOpt In from %s",req.body.recipient);
    } else if(req.body.type == "OptOut") {
        console.log("\nOpt Out from %s",req.body.recipient);
    } else {
        console.log("\nUnrecognized OptIn or OptOut message");
    };
  } else if(size == 4) {
    //SMS_MobileOriginatedMessage
    console.log("\nMO message from %s",req.body.sender);
  } else {
    //unknown callback
    console.log("\nUnrecognized request")
  };
  console.log(req.body);
  return res.end();
  //return res.send(req.body);
});
 
var server = app.listen(3000, function () {
    console.log("Listening on port %s...", server.address().port);
});

You can use a different port number if 3000 doesn’t work in your situation.

5. Install dependencies for the Express and body parser packages.

> npm install

6. Start the callback listener:

> node cblistener.js

7. Register the callback listener with the POST /callbacks API request using your long code and the URL for the callback listener:

{
  "callbackType": "Default",
  "code": "your long code",
  "url": "http://your.domain.com:3000"
}

Now you can generate the different types of callback requests and view them on the console: * OptIn: Use a device to text the word “start” to your long code. * SMS_DeliveryReport: Use POST /sms to send a message to a device. Be sure to set the deliveryReport field to true in the request. * SMS_MobileOriginatedMessage: Send a text message from a device to your long code. * OptOut: Use a device to text the word “stop” to your long code.