Library for Particle devices to easily send SMS via a webhook to Twilio
- Github repository (includes full docs and examples)
- Full browsable API
- License: MIT (can be used in open or closed-source projects, including commercial projects, with no attribution required)
- Library: SmsWebhookRK
Using the library
Add the library to your project. Using Particle Workbench, from the Command Palette Particle: Install Library and add SmsWebhookRK. It’s also avaiable in the community libraries in the Web IDE.
Add the include statement to your main .ino or .cpp source file:
#include "SmsWebhookRK.h"
Add a call from setup():
void setup() {
SmsWebhook::instance().setup();
}
Add a call from loop():
void loop() {
SmsWebhook::instance().loop();
}
Note that both setup and loop calls are required! You should make the loop call on every loop; if there is nothing to do it returns quickly.
To send a SMS message you specify the recipient phone number and the message. Note that the phone number must be in +country code format, so in the United States it will always begin with +1
.
SmsMessage mesg;
mesg.withRecipient("+12125551212")
.withMessage("testing!");
SmsWebhook::instance().queueSms(msg);
This queues the SMS message to send. If the device is online and connected to the Particle cloud, it will be sent almost immediately. Otherwise, it will be queued to send later.
If an error occurs and the publish fails, the message will be retried later.
If you are only sending SMS to yourself from your own devices, you can leave the recipient phone number out and instead encode it in the webhook, that way you don’t need to code your phone number in your application code.
There is also an example using the CloudConfigRK library to store the SMS recipient’s phone number in the cloud. This makes it possible to set the recipient’s phone number using a Particle function, publish, the Device Notes field for the device in the Particle console, or even a Google Sheets spreadsheet.
Full documentation including how to set up Twilio programmable messaging and the webhook are in the Github README.