Twilio Studio and CommCare Integration via Power Automate

Twilio Studio enables the creation of messaging workflows, such as SMS surveys and Interactive Voice Response (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

  1. Create a CommCare application for the integration. The case management flow should include:

    1. 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.

    2. 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.

    3. 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.

    4. Save any other information as case properties.

  2. 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

  1. Use the user interface to set up a simple IVR (or any other) flow.

  1. Choose options, set up variables, and actions in the flow.

  2. Set up a REST API call widget in Twilio for triggering the flow.

  3. Create a Make HTTP Request action at the end to send Twilio data back to CommCare.

Using Power Automate

  1. Create a Power Automate workflow to connect CommCare and Twilio.

    1. First action: Choose the Scheduled trigger when an HTTP request is received and save the URL generated by Power Automate.

    2. 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:

      1. case_id: the Case ID of the client is needed so that data from Twilio can be mapped back to CommCare.

      2. phone_number: the phone number of the client is needed for Twilio to execute any flows.

    3. 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.

      1. The Condition action assumes a blank value in a parsed property named flow_date.

      2. If the condition is met (true), the Delay Until Flow Date action inserts the utcNow() function and triggers the next step.

      3. 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.

    4. Fourth action: Trigger Twilio REST API to POST data from CommCare for triggering the flow.

      1. 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).

      2. 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

  1. 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

  1. Set up another Power Automate workflow for data flow from Twilio to CommCare.

    1. 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.

    2. Parse incoming JSON data.

    3. Add a one minute delay action to ensure that the Twilio flow executes entirely before the next action is executed.

    4. Add a HTTP action to get Twilio call details - paste the Twilio Call execution URL.

      1. Use Basic Authentication and use the Twilio Account SID and password as credentials.

      2. 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
    5. Parse incoming JSON data to prepare for CommCare form submission.

    6. Add HTTP action to submit form back to CommCare to update the case with Twilio call details.

      1. Use raw XML of the follow-up form submission and replace case properties and form content as needed with the parsed JSON values.

      2. Ensure that the case id is mapped correctly in the body from the parsed JSON.

      3. 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

  1. 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.

  2. Verifying Data in ComCare

    1. Check if the data captured from Twilio is successfully saved in CommCare.

    2. Ensure the structured data is available in reports and case lists in CommCare.

  3. Recap the integration process involving ComCare, Twilio Studio, and Power Automate.

    1. Highlight the importance of Power Automate as an integration layer.

    2. Mention the availability of other integration methods and the visual ease of Power Automate.