Twilio Studio enables the creation of messaging workflows, such as SMS surveys and IVR surveys. By integrating Twilio Studio with CommCare using Power Automate, you can trigger Twilio Studio flows directly from CommCare. Once the flow is executed in Twilio, the resulting data can be seamlessly copied back into CommCare, creating an efficient and automated process.
This guide provides detailed step-by-step instructions for integrating Twilio Studio with CommCare using Power Automate. The integration allows seamless data transfer and automation between the platforms.
Here’s a quick video showcasing the integration https://www.loom.com/share/5073066457464e60bdadbcec2b7a7d64
The following sections explain how the integration is setup.
Setting up CommCare Integration
Create a CommCare application for the integration. The case management flow should include:
Registering a client and capturing necessary information. While any other information can be saved, it is necessary to save the phone number of the client in the Twilio prescribed format with a + and country code (E.164 format), for example: +16175551212. The E.164 format is the international telephone numbering plan.
Save the date of the flow as a case property. Leave this blank for a flow that needs to be triggered immediately. Map this to a date time question if the flow is to be run in the future.
Create a separate followup form for all the call detail indicators to be received from Twilio - save these as hidden values and create case properties for them. Submit a dummy form to use the raw XML for submitting this data back to CommCare via Power Automate.
Save any other information as case properties.
Enable data forwarding for new cases or form submissions (based on need) and paste the POST URL from Power Automate in 1.a below.
Setting up Twilio Studio Flow
Use the user interface to set up a simple IVR (or any other) flow.
Choose options, set up variables, and actions in the flow.
Set up a REST API call widget in Twilio for triggering the flow.
Create a Make HTTP Request action at the end to send Twilio data back to CommCare.
Using Power Automate
Create a Power Automate workflow to connect CommCare and Twilio.
First action: Choose the Scheduled trigger when an HTTP request is received and save the URL generated by Power Automate.
Second action: Parse JSON data of HTTP output
This action will parse all the case properties received via the data forwarding request from CommCare.
For example, in this flow, the above case properties are being parsed in this step. Note that the following values are necessary to successfully run the integration:
case_id: the Case ID of the client is needed so that data from Twilio can be mapped back to CommCare.
phone_number: the phone number of the client is needed for Twilio to execute any flows.
Third action (optional): If there are follow-ups required, the use a Condition action to define a true/false condition with a Delay Until action.
The Condition action assumes a blank value in a parsed property named flow_date.
If the condition is met (true), the Delay Until Flow Date action inserts the utcNow() function and triggers the next step.
If the condition is not met (false), the Delay Until Flow Date action takes the date value of the flow_date property to trigger the action when that date and time is met.
Fourth action: Trigger Twilio REST API to POST data from CommCare for triggering the flow.
Make sure the body of the HTTP request has a To and From field (ideally mapped from the parsed JSON data or manually entered if it is a static value (like your Twilio number, for example).
Make sure that the case id is part of the body so that it can be tagged to the relevant Twilio flow and used for updating the case later. This can be done by adding a parameter to the body as seen below.
Automating the Flow
Trigger the Twilio workflow through CommCare using Data Forwarding.
Data will be forwarded to the Power Automate endpoint described above.
Once this is setup correctly, every time a new case is created for the selected case type, data will be forwarded to the Power Automate endpoint and the flow will be triggered as explained above.
The following section explains how data from the Twilio flow can be saved as case properties in CommCare once it has been executed successfully.
Flow from Twilio to CommCare
Set up another Power Automate workflow for data flow from Twilio to CommCare.
First action: Choose the Scheduled trigger when an HTTP request is received and save the URL generated by Power Automate. This will be copied to the Twilio flow HTTP action.
Parse incoming JSON data.
Add a one minute delay action to ensure that the Twilio flow executes entirely before the next action is executed.
Add a HTTP action to get Twilio call details - paste the Twilio Call execution URL.
Use Basic Authentication and use the Twilio Account SID and password as credentials.
The Account SID would be available from your Twilio account and Call SID will be parsed from the previous action.
https://api.twilio.com/2010-04-01/Accounts/{AccountSID}/Calls/@{body('Parse_JSON_of_Twilio_Studio_Data')?['callsid']}.json
Parse incoming JSON data to prepare for CommCare form submission.
Add HTTP action to submit form back to CommCare to update the case with Twilio call details.
Use raw XML of the follow-up form submission and replace case properties and form content as needed with the parsed JSON values.
Ensure that the case id is mapped correctly in the body from the parsed JSON.
Create ‘Authorization’ header ‘with ApiKey username:APIKey’ for authentication.
Once setup, this will automatically submit a form to CommCare and update the case with the relevant case properties of the Twilio call and also show in form exports.
Testing the Integration
Register a client and trigger the workflow in ComCare.
Receive an IVR call from Twilio and interact with the flow.
Check the successful execution of flows from Twilio to CommCare and vice versa.
Verifying Data in ComCare
Check if the data captured from Twilio is successfully saved in CommCare.
Ensure the structured data is available in reports and case lists in CommCare.
Recap the integration process involving ComCare, Twilio Studio, and Power Automate.
Highlight the importance of Power Automate as an integration layer.
Mention the availability of other integration methods and the visual ease of Power Automate.