Conduct a Survey with Twilio Studio - Twilio
Register for SIGNAL by 8/31 for $250 off. Register now.

Conduct a Survey with Twilio Studio

User feedback is a critical part of doing business, and paper surveys can be messy and inefficient. Let’s look at how to build a Twilio Studio flow that hooks into your application via REST API and conducts a brief SMS survey.

 

Create Your Flow

We’ll start with a fresh Twilio Studio flow; log into your Twilio account and navigate to the Studio Dashboard, then tap the + icon to create a new flow. You can name your flow anything you like; we’re calling ours Simple Survey.

Simple Survey flow

You’ll notice that the canvas comes with a widget already in place -- that’s the Trigger widget, which kicks off our flow when the trigger we specify is fired. In this case, our trigger is going to be a REST API call. See the REST API section of the Studio User Guide to learn how to make these calls.

REST API Trigger

 


Build Survey Questions

We’ll start our survey by dragging a Send & Wait For Reply widget onto the canvas, and connecting it to the REST API trigger by connecting the red dot to the grey dot in the corner of the new widget. We can use this widget to send an SMS to the user with a survey question to reply to.

Survey question prompt

 

Split Based on Response

We’ll expect the user to reply with a number between 1 and 10. To check this, we’ll drag a Split Based On… widget onto the canvas. First, we’ll set the variable to test in the right sidebar. Find the Send & Wait For Reply widget in the dropdown (in this example, it’s called first_question), and select inbound.Body from the list to capture the reply to your prompt.Split based on Response

Next, we’ll need to declare the choices we’re looking for in the user’s responses -- numbers between 1 and 10. Tap the red New at the bottom of the Split widget to reveal the Transition On… dropdown menu. Select Condition Matches to create a new Transition.

New condition match



In the right sidebar, find the new Transition that you just created, and select Regex from the dropdown. We’re going to set this value to ^(?:[1-9]|0[1-9]|10)$ (a regular expression that evaluates to integers between 1 and 10). Save the new transition and it will appear on the widget.

Regex for 1-10


If the user enters something that the survey flow recognizes (a number between 1 and 10), we should prompt them with the next question. Drag another Send & Wait For Reply widget onto the canvas, and repeat the steps you just took to add a question and evaluate the response. Continue to repeat for each question in your survey, adjusting the Transition conditions each time based on the expected format of response.

Question sequence

 

No Matches Condition

But what if the user enters something other than a number between 1 and 10? We can account for those cases with the No Matches condition on our Split widgets. Ideally, we’ll guide the user towards a correctly-formatted response by gently suggesting the correct format. We can do this by dragging a Send Message widget onto the canvas and setting the Message Body to “I'm sorry, I didn't understand. Please enter a number from 1 - 10.” then connecting this new widget to the No Matches condition. We can then connect the Sent condition from our Send Message widget back to our original question to give the user a second chance to answer.

No Matches condition


Make an HTTP Request

Now that we have our survey questions in place and a simple handler for unexpected inputs, we’re ready to send the survey data back to our servers. Drag an HTTP Request widget onto the canvas and connect it to the last question’s Split widget. Set the Request URL to your endpoint (we’ll use http://example.com/survey, but yours will look different). Then, for each survey question, scroll down the right sidebar to HTTP Parameters and add a field for the question. The corresponding value will be the same variable you tested in the Split widget for that question -- wrapped in double curly braces and formatted something like {{widgets.first_question.inbound.Body}}. Studio supports the Liquid template language, which is a fancy way of saying “a way to help you load dynamic content throughout your flow.” We’re telling Studio that we’d like it to dynamically interpret the text between the two curly braces based on something -- in this case, the message body we receive in the user’s SMS response to our question. When we send our HTTP request, the parameters with our survey data will be included and we can track results.

Survey HTTP widget


The Finished Product

The final state of our canvas is that we have a Trigger widget that receives an incoming REST API request, then sets of Send & Wait For Reply, Split Based On…, and Send Message widgets to prompt the user with questions and evaluate their responses for format. Finally, an HTTP Request widget allows us to send the survey responses back to our servers.

Survey Flow

 

Time to test it out! You can make a request from your API to Studio and kick off this survey flow, then text responses to each question (try entering something other than a number between 1 and 10 to test the No Matches condition!). For endpoint details, parameters, and a sample curl request, see the REST API section of the Studio User Guide. Get ready to get instant SMS feedback from your users!

Jennifer Aprahamian
David Prothero
Brent Schooley

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.