Texts, Texts, who wants my Texts?#
In a previous article , we nstalled and configured an SMS server (gateway): sms3tools. Let’s use Node-RED to send an SMS via the dashboard to a third party.
Objective:#
-
Create a dashboard with the following form:
- Phone (recipient)
- Message to send
- SUBMIT CANCEL
-
Create the flow; the answer to the following algorithm:
- Retrieve phone information + text to send
- Send json object to NODE-RED
{ "payload":"Ceci est le message", "topic":"33612345678", "_msgid":"6eb2943f.7ab59c" }- Send SMS to recipient
Prerequisites#
- A Node-RED platform
- smstools3 installed
- Have installed the node set node-red-dashboard
- Have installed the node set node-red-contrib-smstools
Implementation#
Difficulty apprehended#
When you’re new to Node-RED, manipulating a json object isn’t always straightforward. Here we’re faced with the following difficulty:
Object returned by the form:
{
"payload":
{
"topic":"33612345678",
"payload":"Ceci est le message"
},
"socketid":"Blz2Pm_yeZQp5YwOAAAB",
"_msgid":"3d93fb0b.857094"
}
An object with 3 properties, the first of which (payload) is itself an object with two properties: payload and topic.
Object expected by the node sms out:
{
"payload":"Ceci est le message",
"topic":"33612345678",
"_msgid":"3d93fb0b.857094"
}
Implemented solution#
After much deliberation, the “function” node seems the most appropriate for manipulating and transforming data, depending on the expected target (object).
Here’s the complete flow:
Explanations:
- The first form node creates the form on the dashboard.
- The second node change removes the property: socketid
- The function node extracts the values msg.payload.payload and msg.payload.topic, reassigning them to two distinct properties in the new message rewrite.
- The debug node is used to check the output result.
2to1 function code:
var payload=msg.payload.payload
var topic=msg.payload.topic
msg.payload=payload
msg.topic=topic
return msg;
Full flow code:
[{"id":"754604fc.a77ec4","type":"sms-out","z":"602114bc.b2e564","name":"@téléphone","topic":"","x":720,"y":220,"wires":[]},{"id":"ec87386c.d9a0e8","type":"ui_form","z":"602114bc.b2e564","name":"","label":"","group":"ae419132.f7df58","order":2,"width":0,"height":0,"options":[{"label":"Telephone","value":"topic","type":"text","required":true,"rows":null},{"label":"SMS","value":"payload","type":"text","required":true,"rows":null}],"formValue":{"topic":"","payload":""},"payload":"","submit":"submit","cancel":"cancel","topic":"","x":130,"y":160,"wires":[["f46f0597.38f7c8"]]},{"id":"70e9d2b7.7859c4","type":"debug","z":"602114bc.b2e564","name":"SMS - Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":720,"y":160,"wires":[]},{"id":"7f9646e7.83431","type":"function","z":"602114bc.b2e564","name":"2to1","func":"var payload=msg.payload.payload\nvar topic=msg.payload.topic\nmsg.payload=payload\nmsg.topic=topic\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":160,"wires":[["70e9d2b7.7859c4","754604fc.a77ec4"]]},{"id":"f46f0597.38f7c8","type":"change","z":"602114bc.b2e564","name":"","rules":[{"t":"delete","p":"socketid","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":160,"wires":[["7f9646e7.83431"]]},{"id":"ae419132.f7df58","type":"ui_group","z":"","name":"Form","tab":"4b84c940.7f1fe8","order":1,"disp":true,"width":"6","collapse":false},{"id":"4b84c940.7f1fe8","type":"ui_tab","z":"","name":"SMS","icon":"dashboard","disabled":false,"hidden":false}]
The dashboard obtained: