Twilio
SMS Provider
Introduction
Twilio is a leading communication platform that empowers businesses to connect with their users through various channels, including SMS. This document highlights the integration of CleverTap and Twilio, empowering businesses to elevate their SMS communication using Twilio's infrastructure.
Prerequisites for Integration
Before you begin with integration, ensure you have:
- A Twilio account with SMS permissions
- A CleverTap account with SMS setup enabled
Twilio Setup
This process involves the following three significant steps:
Find Twilio Details
CleverTap recommends keeping the Twilio account handy before configuring it on the CleverTap dashboard. To locate these details, navigate to the Console page from the Twilio dashboard:
- Account SID
- Auth Token
- My Twilio phone number
Add Twilio Details on CleverTap Dashboard
To add Twilio details on the CleverTap dashboard:
- From the CleverTap Dashboard, navigate to Settings > Channels > SMS.
- Click +Add Provider. The Add SMS provider page opens.
- Under Setup, enter the following details:
Field | Description |
---|---|
Nickname | Uniquely identifies the provider. |
Account SID | Enter your account identifier from the Twilio dashboard. This is a unique key to distinguish a specific Twilio parent account or subaccount. For more information, refer to Twilio Account SID. |
Auth token | Enter the Auth Token (Password) obtained from the Twilio dashboard. For more information, refer to Auth Token. |
Callback URL | Enter the URL where delivery or error callbacks must be posted after sending the SMS. By default, CleverTap's callback URL will be pre-populated. If you wish to send the callbacks to a custom URL instead, you can also set it here. For more information on setting a custom callback URL, refer to Custom callback URL. Note: This feature is currently in Public Beta. |
Restore | Click Restore to reinstate CleverTap's callback URL. This will remove any custom callback URLs. |
From Number | Enter phone numbers obtained under the Twilio console β Manage-numbers β Active number section. |
Service SID (Copilot) | Enter Service SID details obtained from the Twilio dashboard. The Copilot Service SID, a Twilio identifier, streamlines message management. It automates number selection and formatting, enhancing engagement by optimizing message delivery based on location and carrier requirements. |
Template ID | It is a unique template ID for each SMS template that you want to use. Selecting this option will make it mandatory to input the template ID when saving the provider. |
Mark as default | Select Mark this as default to make this SMS provider the default provider to send the SMS. |
- Click Save to save the details.
Send a Test SMS
To ensure that the integration is successful, send a test SMS as follows:
- Click the Send Test SMS hyperlink before creating SMS campaigns and journeys.
- Enter the following details:
- Country Code and Mobile Number: Enter the country code and mobile number to which you want to send the message.
- Message: This is a test message.
Twilio Trial Account
With a Twilio trial account, you can only send SMS campaigns only to numbers registered on the Twilio dashboard.
Encoding
The messages sent out from CleverTap are encoded in the UTF-8 charset.
Twilio Callbacks
Public Beta
Currently, this feature is in Public Beta.
The Twilio callback functionality enhances the tracking of SMS interactions originating from the CleverTap dashboard. Twilio's webhooks allow your Twilio phone number to receive incoming text messages. Within CleverTap's setup, the Callback URL is seamlessly generated and prefilled in the provider configuration under Settings > Channels > SMS > +Provider. Users can replace this URL with their own, which must be configured on our end to receive callbacks.
The callback mechanism between Twilio and CleverTap is designed to facilitate communication regarding SMS status updates. The Clevertap callback URL tracks for Delivered (SMS delivered to the user), Failed (SMS delivery error), and Replied (user response to SMS) events from Twilio.
Default Callback URL
In the default setup, Twilio directly sends the information to CleverTap's callback URL. The flow involves the following steps:
- Twilio generates the callback.
- Twilio sends the callback directly to CleverTap on the default URL.
- CleverTap processes the callback.
- Upon receiving callbacks from Twilio, the comprehensive stats are displayed on the CleverTap dashboard. For more information, refer to Stats.
Custom Callback URL
Twilio sends the callback to a custom URL, which may belong to your customer. The customer is then responsible for replaying the callback to CleverTap. The flow is as follows:
- Twilio generates the callback.
- Twilio sends the callback directly to the customer's custom URL.
- The customer's custom URL relays the callback to CleverTap.
- CleverTap processes the callback.
- Upon receiving callbacks from Twilio, the comprehensive stats are displayed on the CleverTap dashboard. For more information, refer to Stats.
Callback Data Passing
When the callback is sent through a custom URL, CleverTap's callback endpoint requires all fields from the request payload received from Twilio to be relayed back exactly as received.
Set Up Message Payload
In the Twilio callback integration with CleverTap, you can access dynamic parameters provided by Twilio in callback updates. This applies specifically when using a custom callback URL. Twilio provides meta details that should be forwarded to CleverTap as part of the integration.
- Key Name: meta
- Description: Denotes information that CleverTap uses for attributing delivery, failure, etc., to specific campaigns, profiles, and SMS providers is included in the callback update by CleverTap. CleverTap passes this parameter in the ongoing SMS to Twilio, and Twilio, in turn, passes the same parameter back to the callback URL.
- Sample Value: 919101699729.1200000000.1691686363.20230810.0.wzrk_default.-493000
Sample Request Payload
This section provides information about the sample request payload sent to SMS providers.
Delivered
The following is a sample request payload for Delivered from Twilio.
curl --location 'https://sk1.cb.wzrkt.com/sms/twilio?account=ABC-DEF-WWRZ&meta=9999999999.1200000000.1691686363.20230810.0.wzrk_default.-4930006' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'RawDlrDoneDate=2308102223' \
--data-urlencode 'SmsSid=SMXXXXXXXXXXXXX' \
--data-urlencode 'SmsStatus=delivered' \
--data-urlencode 'MessageStatus=delivered' \
--data-urlencode 'To=00000000000' \
--data-urlencode 'MessagingServiceSid=MGXXXXXXXXXXXX' \
--data-urlencode 'MessageSid=SMXXXXXXXXXXXX' \
--data-urlencode 'AccountSid=AC0000000000' \
--data-urlencode 'From=0000000000' \
--data-urlencode 'ApiVersion=2010-04-0'
Replied
The following is a sample request payload for Replied from Twilio.
curl --location 'https://sk1.cb.wzrkt.com/sms/twilio?account=ABC-DEF-WWRZ&meta=000000000.1200000000.1691686363.20230810.0.wzrk_default.-4930006' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'ToCountry=US' \
--dat a-urlencode 'ToState=MA' \
--data-urlencode 'SmsSid=SMXXXXXXXXXXXXX' \
--data-urlencode 'FromState=CA' \
--data-urlencode 'FromCity=SAN+FRANCISCO' \
--data-urlencode 'FromCountry=US' \
--data-urlencode 'SmsStatus=received' \
--data-urlencode 'Body=Test+Reply+Message+2' \
--data-urlencode 'To=00000000000' \
--data-urlencode 'MessagingServiceSid=MGXXXXXXXXXXXX' \
--data-urlencode 'MessageSid=SMXXXXXXXXXXXX' \
--data-urlencode 'AccountSid=AC000000000000' \
--data-urlencode 'From=00000000000' \
--data-urlencode 'ApiVersion=2010-04-0'
The following table provides information about the key-value pair present in the above sample request:
Key Name | Description | Sample Value |
---|---|---|
RawDlrDoneDate | 2302172359 | |
SmsSid | SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | |
MessageStatus | delivered | |
SmsStatus | MessageStatus value. | delivered |
AccountSid | The 34-character ID of the Account the message is associated with. | ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
MessagingServiceSid | The 34-character ID of the Messaging Service associated with the message. | MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
From | The phone number or channel address that sent this message. | +14017122661 |
To | The phone number or channel address of the recipient. | +15558675310 |
NumMedia | The number of media items associated with your message | 0 |
FromCity | The city of the sender. | SAN FRANCISCO |
FromState | The state or province of the sender. | CA |
FromZip | The postal code of the called sender. | 94103 |
FromCountry | The country of the named sender. | US |
ToCity | The city of the recipient. | SAUSALITO |
ToState | The state or province of the recipient. | CA |
ToZip | The postal code of the recipient. | 94965 |
ToCountry | The country of the recipient. | US |
For more information, refer to Twilio's request parameter.
Incoming Payload Mapping to System Events
CleverTap maps the following incoming payloads with their respective system events. You can view these on the CleverTap dashboard.
Event | Sample Payload Structure | Mapping to CleverTap System Events |
---|---|---|
Notification Delivered | curl --location 'https://sk1.cb.wzrkt.com/sms/twilio?account=ABC-DEF-WWRZ&meta=9999999999.1200000000.1691686363.20230810.0.wzrk_default.-4930006' --header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode 'RawDlrDoneDate=2308102223' --data-urlencode 'SmsSid=SMXXXXXXXXXXXXX' --data-urlencode 'SmsStatus=delivered' --data-urlencode 'MessageStatus=delivered' --data-urlencode 'To=00000000000' --data-urlencode 'MessagingServiceSid=MGXXXXXXXXXXXX' --data-urlencode 'MessageSid=SMXXXXXXXXXXXX' --data-urlencode 'AccountSid=AC0000000000' --data-urlencode 'From=0000000000' --data-urlencode 'ApiVersion=2010-04-0' | Maps to Notification Delivered event. For more information, refer to Events. |
Notification Replied | curl --location 'https://sk1.cb.wzrkt.com/sms/twilio?account=ABC-DEF-WWRZ&meta=000000000.1200000000.1691686363.20230810.0.wzrk_default.-4930006' --header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode 'ToCountry=US' --dat a-urlencode 'ToState=MA' --data-urlencode 'SmsSid=SMXXXXXXXXXXXXX' --data-urlencode 'FromState=CA' --data-urlencode 'FromCity=SAN+FRANCISCO' --data-urlencode 'FromCountry=US' --data-urlencode 'SmsStatus=received' --data-urlencode 'Body=Test+Reply+Message+2' --data-urlencode 'To=00000000000' --data-urlencode 'MessagingServiceSid=MGXXXXXXXXXXXX' --data-urlencode 'MessageSid=SMXXXXXXXXXXXX' --data-urlencode 'AccountSid=AC000000000000' --data-urlencode 'From=00000000000' --data-urlencode 'ApiVersion=2010-04-0' | Value of Body in the payload is populated in the Notification Replied event. For more information, refer to Events. |
Error Codes
If there is an error in the SMS delivery, these error codes indicate the nature of an error encountered. Each error code corresponds to a specific error description. The error descriptions are displayed within the Errors tab in the campaign stats section in case of delivery-related failures. The following table provides information about the acceptable error codes:
Error Codes | Error Description | Details |
---|---|---|
30001 | Queue overflow | Indicates that the rate limit was exceeded for sending messages, causing the queue to overflow. To resolve this issue, please wait a while before sending your message again. |
30002 | Account suspended | Indicates that the account was temporarily suspended between sending the message and its delivery. Kindly get in touch with Twilio for further assistance. |
30003 | Unreachable destination handset | Indicates that the recipient's mobile device is either turned off or presently not reachable. |
30004 | Message blocked | Indicates that the recipient's number you are attempting to contact is blocked from receiving this message, possibly due to being blacklisted. |
30005 | Unknown destination handset | Indicates that the attempt to reach the specified number encounters an unfamiliar recipient, which may suggest an invalid or non-existent contact. |
30006 | Landline or unreachable carrier | Indicates that the destination number is unable to receive this message. This can arise when contacting a landline or encountering an unreachable carrier, particularly with shortcodes. |
30007 | Carrier violation | Indicates that the carrier has detected the message content as objectionable, prompting the activation of content or spam filtering to safeguard their subscribers. For a deeper understanding of carrier filtering, consult supplementary resources. |
30008 | Unknown error | This error does not conform to any predefined categories mentioned above. |
30009 | Missing segment | Indicates that one or more segments associated with your multipart inbound message were not received. |
30010 | Message price exceeds the max price | Indicates that the cost of the message surpasses the maximum price parameter set. |
21614 | Invalid Mobile Number | Indicates that the user's number is not valid. |
21610 | Unsubscribed recipient | Indicates that the individual you are attempting to message has chosen not to receive messages from your Twilio phone number, Channels sender, or Messaging Service. |
Sample Payload
The following is a sample error payload:
curl --location 'https://sk1.cb.wzrkt.com/sms/twilio?account=WWW-WWW-WWRZ&meta=000000000.1200000000.1691686363.20230810.0.wzrk_default.-4930006' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'ErrorCode=30005' \
--data-urlencode 'SmsSid=SMXXXXXXXXXXXXXXXXXXXXXXX' \
--data-urlencode 'SmsStatus=failed' \
--data-urlencode 'MessageStatus=failed' \
--data-urlencode 'To=000000000000' \
--data-urlencode 'MessagingServiceSid=MGXXXXXXXXXXXX' \
--data-urlencode 'MessageSid=SMXXXXXXXXXXXXXXX' \
--data-urlencode 'AccountSid=AAAAAAA000000' \
--data-urlencode 'From=0000000000' \
--data-urlencode 'ApiVersion=2010-04-0'
Stats
CleverTap displays comprehensive stats such as Errors, Delivered, Clicked, and CTRs upon receiving callbacks from Twilio. After setting up the provider on the CleverTap dashboard, you can view these statistics from the following pages on the CleverTap dashboard:
- Stats tab of the Campaigns. For more information, refer to SMS Stats.
Note
The stats for Clicked event is available only if you use CleverTap's Click Tracking feature.
- Stats tab under Provider setup. For more information, refer to the Provider Stats.
You can also analyze the Notification Delivered and Notification Replied events by navigating to the Analytics > Events page, as shown in the following image:
Updated 7 months ago