Skip to content

Instantly share code, notes, and snippets.

@juliacodez
Last active July 11, 2019 09:17
Show Gist options
  • Save juliacodez/6b97eb91f747b32fa602cb459f3a5b2e to your computer and use it in GitHub Desktop.
Save juliacodez/6b97eb91f747b32fa602cb459f3a5b2e to your computer and use it in GitHub Desktop.
SMS Forwarding with Nexmo

This flow forwards all incoming SMS messages from your Nexmo number to a predefined phone number. To achive this, first you'll need to be able to receive the incoming messages. Then, as a second step, you'll have to set up the outbound messges by using the sendsms node.

Prerequisites

Before getting started, you’ll need a few things:

  • The Nexmo Node-RED Module
  • A Nexmo account—create one for free if you haven't already
  • A way to expose your local server to the internet. A convenient way would be using ngrok or similar tunnelling services - skip this if you’re running Node-RED on a public webserver instead of your local machine.

For this flow, you'll also need a Nexmo number—and you can get one under Numbers > Buy numbers.

There are two tutorials that explain the concepts used more in depth:

Useful Links

[
{
"id": "35300a5a.d66096",
"type": "tab",
"label": "SMS Forwarding",
"disabled": false,
"info": ""
},
{
"id": "fc377faf.c5e37",
"type": "http in",
"z": "35300a5a.d66096",
"name": "",
"url": "/inbound-sms",
"method": "get",
"upload": false,
"swaggerDoc": "",
"x": 210,
"y": 300,
"wires": [
[
"48899171.a4a6b",
"3c6aa208.b7061e",
"fa71e1af.4de19"
]
]
},
{
"id": "3c6aa208.b7061e",
"type": "http response",
"z": "35300a5a.d66096",
"name": "",
"statusCode": "200",
"headers": {},
"x": 680,
"y": 300,
"wires": []
},
{
"id": "95a33eac.6763",
"type": "comment",
"z": "35300a5a.d66096",
"name": "❓About this path - Receive and Send SMS",
"info": "This flow forwards all incoming SMS messages from your Nexmo number to a predefined phone number.\nTo achive this, first you'll need to be able to receive the incoming messages. Then, as a second step, you'll have to set up the outbound messges by using the *`sendsms`* node.\n\n# Receiving SMS Messages\n\nIn order to receive SMS messages with Nexmo, you need to associate a webhook endpoint with a virtual number that you have rented from Nexmo. Inbound Messages to that number will then be sent to your webhook endpoint.\n\n## Defining a Webhook Endpoint\n\nFirst, set up this webhook endpoint in your Node-RED editor. Connect a `http` input node to a `http response` node, as well as to a `debug` node, so that you can view your inbound messages in the debug area. \nIn the `http` input node, select `GET` as a `Method` and fill in the `URL` field with something like `/inbound-sms`.\nThe `http response` node should have `200` set as `Status code`, but don't worry about it, this is the default value.\n\n## Exposing Your Local Server to the Internet\n\nNext you'll have to expose your local server to the internet, so that Nexmo can access it. If you’re running Node-RED on a public webserver instead of your local machine, you can skip this stage. \nOtherwise, a convenient way to do this is by using a tunneling service like [ngrok](https://ngrok.com).\n\n[Download](https://ngrok.com/download) and install **ngrok**, then run it in the terminal to start a tunnel on port `1880`.\n```bash\n$ ./ngrok http 1880\n```\n\n## Setting Up the Endpoint with Nexmo\n\nThe last step is letting the Nexmo SMS API know where it should forward the inbound messages.\n\nAssociate a webhook endpoint with one of your virtual numbers by going to [Your numbers](https://dashboard.nexmo.com/your-numbers), then clicking the settings icon next to the number you'd like to configure. \nNext, fill in the _Inbound Webhook URL_ field with `YOUR_URL/inbound-sms` and `Save changes`.\n\nNow, if you send a text message to your Nexmo number, you should see the message object appear in the debug sidebar.\nThe message payload will contain a couple of key values that should be noted:\n\n| KEY | DESCRIPTION |\n| --------------- | --- |\n| **`msisdn`** | The sender's number in E.164 format. For example 447401234567. |\n| **`to`** | Your Nexmo number that the SMS was sent to, in E.164 format. |\n| **`text`** | The content of the received SMS message.|\n| **`type`** | The type of the message body received (**`text`** key). Possible values are `text`, `unicode` and `binary`.| \n| **`keyword`** | The first word in the message body. This is typically used with short codes.| \n| **`message-timestamp`** | The time when Nexmo started to push this Delivery Receipt to your webhook endpoint.\n\nFind out more about these parameters in the [Nexmo API Reference for Inbound SMS](https://developer.nexmo.com/api/sms#inbound-sms).\n\n\n# Sending SMS messages\n\nTo respond to the inbound messages, you'll need to wire a *`sendsms`* node to the `http` input node.\nYou can set this node up by double clicking on it and filling in the parameters below. You'll find `API KEY` and `API SECRET` by clicking on the edit button next to `Nexmo Credentials`. Get these from your [Nexmo account](https://dashboard.nexmo.com/getting-started-guide).\n\nNext to the `TO`,`FROM` and `TEXT` parameters there is a `{}` sign, which means that [Mustache templating](https://mustache.github.io/) is supported for those fields. This comes in handy when passing certain values dynamically to the **`sendsms`** node.\n\n| KEY | DESCRIPTION |\n| --------------- | --- |\n| `API KEY` | Your Nexmo API key, shown in your [account overview](https://dashboard.nexmo.com/getting-started-guide). |\n| `API SECRET` | Your Nexmo API secret, shown in your [account overview](https://dashboard.nexmo.com/getting-started-guide). |\n| `TO` | The phone number you'd like the incoming SMS messages to be forwarded to, in E.164 format. For example 447401234567.\n| `FROM` | You can refer to the initial sender's number by using templating: **{{msg.payload.msisdn}}**, if this feature is [supported in your country](https://help.nexmo.com/hc/en-us/articles/115011781468). Otherwise, use one of your Nexmo numbers. \n| `TEXT` | The content of the outbound message. To forward the original message body, use **{{msg.body.text}}** in this field.\n\nTo have a bit more insight into what's happening when you send an SMS, the **`sendsms`**' output is wired into a **`debug`** node. Double click on it and set the `Output` to `complete msg object`.\n\nOnce you hit **Deploy**, your SMS forwarding service should be up and running! \n",
"x": 250,
"y": 160,
"wires": []
},
{
"id": "655c4f99.c5b39",
"type": "comment",
"z": "35300a5a.d66096",
"name": "📖 Check out the tutorials",
"info": "This flow is based on two concepts, [sending](https://www.nexmo.com/blog/2019/04/17/send-sms-messages-node-red-dr/) and [receiving](https://www.nexmo.com/blog/2019/04/24/receive-sms-messages-node-red-dr/) SMS messages.\nClick the links above to go to the respective tutorials.",
"x": 190,
"y": 80,
"wires": []
},
{
"id": "48899171.a4a6b",
"type": "debug",
"z": "35300a5a.d66096",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 590,
"y": 220,
"wires": []
},
{
"id": "fa71e1af.4de19",
"type": "sendsms",
"z": "35300a5a.d66096",
"creds": "47e82daf.b07814",
"to": "",
"fr": "",
"text": "From: {{msg.payload.msisdn}} \\r\\n Text: {{msg.payload.text}}",
"unicode": false,
"x": 530,
"y": 480,
"wires": [
[
"e70837a.02e53c8"
]
]
},
{
"id": "e70837a.02e53c8",
"type": "debug",
"z": "35300a5a.d66096",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"x": 750,
"y": 480,
"wires": []
},
{
"id": "eccfc743.22caa8",
"type": "http in",
"z": "35300a5a.d66096",
"name": "",
"url": "/receipt",
"method": "get",
"upload": false,
"swaggerDoc": "",
"x": 200,
"y": 660,
"wires": [
[
"2c5025c8.5da49a",
"992a092c.a97e78"
]
]
},
{
"id": "2c5025c8.5da49a",
"type": "http response",
"z": "35300a5a.d66096",
"name": "",
"statusCode": "",
"headers": {},
"x": 540,
"y": 660,
"wires": []
},
{
"id": "992a092c.a97e78",
"type": "debug",
"z": "35300a5a.d66096",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"x": 540,
"y": 740,
"wires": []
},
{
"id": "95381776.42f4d8",
"type": "comment",
"z": "35300a5a.d66096",
"name": "❓ About this path - Handle Delivery Receipts",
"info": "When the message gets delivered, the mobile phone carrier returns a [**Delivery Receipt**](https://developer.nexmo.com/messaging/sms/guides/delivery-receipts) to Nexmo. This will contain the delivery status.\n\n## Receiving a Delivery Receipt from a Mobile Carrier\n\nTo find out the `status` of your outbound message, you'll need to set up a webhook endpoint that Nexmo can forward the **Delivery Receipt** to.\n\n### Defining a Webhook Endpoint\n\nFirst, connect a `http` input node to a `http response` node, as well as to a `debug` node, so that you can view your delivery receipt in the debug area.\n\nIn the `http` input node, select `GET` as a `Method` and fill in the `URL` field with something like `/receipt`.\nThe `http response` node should have `200` set as `Status code`, but don't worry about it, this is the default value as well.\n\n### Setting Up the Endpoint with Nexmo\n\nThe last step is letting the Nexmo SMS API know where it should forward the delivery receipts. You can do so under your [API settings](https://dashboard.nexmo.com/settings) in the **Default SMS Setting** section.\n\nSet the default webhook URL for delivery receipts to `YOUR_URL/receipt`, then `Save changes`.\n\nNow go ahead and send another SMS!\n\nLearn more about delivery receipt status messages and error codes in the Nexmo [docs](https://developer.nexmo.com/messaging/sms/guides/delivery-receipts).\n",
"x": 250,
"y": 580,
"wires": []
},
{
"id": "47e82daf.b07814",
"type": "nexmobasic",
"z": ""
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment