Receiving an SMS

To receive an SMS, you need to:

Prerequisites

npm install express body-parser

Write the code

Add the following to receive-express.js:

const app = require('express')();
const bodyParser = require('body-parser');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

const handleInboundSms = (request, response) => {
  const params = Object.assign(request.query, request.body);
  console.log(params);
  response.status(204).send();
};
app
  .route('/webhooks/inbound-sms')
  .get(handleInboundSms)
  .post(handleInboundSms);

app.listen(process.env.PORT || 3000);

View full source

Run your code

Save this file to your machine and run it:

node receive-express.js

Configure the webhook endpoint in your Vonage Dashboard

So that Vonage knows how to access your webhook, you must configure it in your Vonage account.

In the code snippets, the webhook is located at /webhooks/inbound-sms. If you are using Ngrok, the webhook you need to configure in your Vonage Dashboard API Settings page is of the form https://demo.ngrok.io/webhooks/inbound-sms. Replace demo with the subdomain provided by Ngrok and enter your endpoint in the field labeled Webhook URL for Inbound Message:

Try it out

Now when you send your Vonage number an SMS you should see it logged in your console. The message object contains the following properties:

{
  "msisdn": "447700900001",
  "to": "447700900000",
  "messageId": "0A0000000123ABCD1",
  "text": "Hello world",
  "type": "text",
  "keyword": "Hello",
  "message-timestamp": "2020-01-01T12:00:00.000+00:00",
  "timestamp": "1578787200",
  "nonce": "aaaaaaaa-bbbb-cccc-dddd-0123456789ab",
  "concat": "true",
  "concat-ref": "1",
  "concat-total": "3",
  "concat-part": "2",
  "data": "abc123",
  "udh": "abc123"
}