Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How track responses for an SMS to multiple recipients with the Twilio API?

Tags:

sms

twilio

survey

I would like to send a text message survey (eg. "How happy were you with X service? Reply 1 for satisified, Reply 2 for not satisfied") to multiple recipients. From the responses, I would like to create a report on the recipients that responded 1 vs the ones that responded 2. What is the best way to do this with the Twilio API? Does my app need to store the results from my incoming SMS message or does Twilio store these so I can query the results? If the former is the case and TwiML is involved, how do I parse the response and store the result? Thanks!

like image 602
Chirag Patel Avatar asked Feb 18 '23 10:02

Chirag Patel


1 Answers

** Disclaimer: Twilio evangelist here **

Chirag:

So it sounds like you have two requirements here:

  1. Use Twilio to send outbound text messages to different recipients
  2. Capture a users reply to that message

For the first requirement, you can start by going to Twilio.com and signing up for a new account. Its free to start and we give you a Twilio phone number you can use to start to build your app. Once you have the Twilio phone number, you can use the REST API to start sending outbound text messages from that Twilio phone number. We have a quickstart that shows you how to do this:

http://www.twilio.com/docs/quickstart/php/sms/sending-via-rest

Note that this link goes to the PHP sample, but you can use the drop down at the top of the page to pick from other stacks like .NET, Java, Python or Ruby.

Once you've sent an outbound message you need to capture the replies to that message (your second requirement). Twilio uses something called a webhook to notify you about incoming SMS messages.

A webhook is basically a URL exposed by your application and associated with your Twilio phone number. You can configure the URL associated with your phone number in the Twilio dashboard.

Each time we receive an incoming SMS message on your Twilio phone number, we will make an HTTP request to that URL. As part of that HTTP request we send along metadata about the inbound message like the phone number that the message was sent from and the Body of the message. The full list of parameters we send is here:

http://www.twilio.com/docs/api/twiml/sms/twilio_request

Now your app can pull those parameters out of the request and do whatever it wants with them. Since we already are sending the body of the message as we receive it, Tims suggestion of tracking the responses based on the From parameter and storing the message body in your own database is a good suggestion.

This quickstart shows receving an incoming text message, grabbing the From parameter and then responding by sending back from TwiML:

http://www.twilio.com/docs/quickstart/php/sms/replying-to-sms-messages

In your case, if you don't want to reply to the incoming message, just omit the TwiML response.

Hope the helps. Lets me know if you need more info.

Devin

like image 135
Devin Rader Avatar answered Apr 26 '23 04:04

Devin Rader