With Twilio, we can send bulk SMS notifications with a single API request via Twilio Notify. The API can be used not just for SMS, but for all the other channels supported by Twilio Notify like iOS and Android push notifications, web push via FCM and APNS, and Facebook Messenger. You can even mix multiple channels in a single request. In the steps below I will demonstrate how to set this up just for SMS.
Limitations of Twilio Notify API.
The total size of the request entity should not exceed 1MB. This is typically sufficient for 10,000 phone numbers.
Create a Messaging Service.
Assuming already you have a Twilio Phone number go to Programmable SMS → SMS → Messaging Service in your Twilio console. If you don't have a Twilio phone number you will need to purchase one before creating a new Messaging service.
There are different use cases for what you will use the messaging service for. In the example, I will go with Mixed.
Add your Twilio Phone Number to the Messaging Service.
To receive inbound messages with your Messaging Service, configure your Inbound Request URL.
Set up Notify Service Instance.
In the console navigate to Notify → Services and create a Notify Service.
Now choose your new Messaging Service for this Notify Service Instance and click save.
Create a custom Connection.
Create a custom connection with following values:
endpoint
: https://notify.twilio.com/v1/ServiceSID/Notifications
The ServiceSID is the service SID for the Notify Service Instance we created.- Set the Content Type to Other.
- The Connection requires 2 headers:
- Twilio API uses Basic authentication, you will use your Twilio account SID as the username and your auth token as the password for HTTP Basic authentication.
- The 2 header is the
Content-Type
with a value ofapplication/x-www-form-urlencoded
.
- In the Bulk Messaging Payload use the following Flang.
${def recipients = destinations.collect({ "&ToBinding=" + URLEncoder.encode(groovy.json.JsonOutput.toJson(["binding_type":"sms", "address": it])) }).join(); def message = 'Body='+URLEncoder.encode(text); message+recipients}
- If you are using the connection for single messaging SMS then you will need the following Flang for the Single Messaging Payload section
${
def message = 'Body='+URLEncoder.encode(text);def recipient='&ToBinding='+ URLEncoder.encode(groovy.json.JsonOutput.toJson(["binding_type":"sms", "address": destination])); message+recipient
}
0 Comments