Level up your Twilio API skills in TwilioQuest, an educational game for Mac, Windows, and Linux. Download Now


Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?


Have you ever wondered how to create an automated survey that can be answered by phone or SMS?

This tutorial will show how to do it using the Twilio API.


  1. エンドユーザーはアンケートの電話番号に発信、またはSMS送信を行います。
  2. Twilioに電話やSMSを着信すると、HTTPリクエストを開発者のアプリケーションに発行し、どのように応答するかの指示を問い合わせます。
  3. Your web application serves up TwiML instructions to Gather or Record the user input over the phone, or prompts for text input with Message.
  4. 各設問の後に、Twilioはエンドユーザーの入力を伴った別のリクエストを開発者のサーバーに送信します。 アプリケーションはこの入力をデータベースに保存します。
  5. Your application returns a TwiML response to Twilio with instructions to either ask the next question or complete the survey.

Instacart uses Twilio to power their customer service surveys and integrate that feedback into their customer database. Read more here.



For your convenience, this application's repository already includes one survey that can be loaded into the database. This survey will be loaded into the database when the app starts as long as the database is configured correctly.

You can modify the survey questions by editing the seeds.rb file located in the root of the repository and re-running the app. You can see more about seeding your database here.


        Automated Survey controller


        We want users to take our survey, so we need to implement a handler for SMS and calls. First, let's take a moment to understand the flow of a Twilio-powered survey as an interview loop.



        The user can enter an answer for your survey over the phone using either their phone's keypad or by speaking. After each interaction Twilio will make an HTTP request to your web application with either the string of keys the user pressed or a URL to a recording of their voice input.

        It's up to the application to process, store, and respond to the user's input.


        Configure your application to work with Twilio

        Twilio 番号の設定

        インタビュープロセスを始めるには、着信の通話やテキストを受けたときに HTTP リクエストを Web アプリケーションに送信するように、いずれかの Twilio 番号を設定しておく必要があります。

        Click on one of your numbers and configure your Voice and Message URLs to point to your server. In our code, the routes are /surveys/voice and /surveys/sms, respectively.


        If you don't already have a server configured to use as your webhook, ngrok is a great tool for testing webhooks locally.


              Voice and SMS endpoints for user survey


              Lets see how we'll handle these webhooks.

              Respond to the Twilio request


              After receiveing a call or SMS, Twilio will send a request to the URL specified in that phone number's configuration:

              • /surveys/voice 通話用。
              • /surveys/sms SMS用。

              Each of these endpoints will receive the request and will use Twilio::TwiML::Response to return a welcome message to the user. For callers, the constructed message will contain a Say verb. For requests coming from a text we'll respond with a Message which will respond with an SMS.

              Our response will also include a Redirect verb to redirect to our question enpoint in order to continue the survey flow.


                    Welcome message for voice surveys


                    Now that we know how to handle requests to our webhooks, let's learn how to keep track of the survey's state.

                    Build a question controller for calls


                    This controller is used for calls only. It produces the TwiML with the questions in our survey.

                    The sequence of questions is modeled through the action verb attribute.


                          Question controller for voice surveys


                          Let's see how to create the voice response.

                          Create a voice response


                          If there is no question (Question::NoQuestion), we'll respond with an exit message.

                          If the question is "numeric" in nature, then we use the <Gather> verb. However, if we expect voice input from the user, we use the <Record> verb. Both verbs take an action attribute and a method attribute.

                          Twilio will use both attributes to define our response's endpoint to use as callback. That endpoint will be responsible for receiving and storing the caller's answer.

                          During the Record verb creation, we also ask Twilio for a Transcription. Twilio will process the voice recording and extract useful text, making a request to our response's endpoint when the transcription is complete.


                                Create a voice response or exit the survey


                                Now let's see how to handle the response that comes in from our user.

                                Handle user responses


                                When the user finishes their response, Twilio sends us a request that tells us what happened and asks for further instructions on how to proceed.

                                この時点では、Answercreate メソッドを使用してTwilioのリクエスト・パラメーターからデータを復旧する必要があります。

                                Recovered parameters vary according to what we asked during the survey:

                                • From 発信者の電話番号が含まれます。
                                • RecodingUrl contains the URL for listening to a recorded message.
                                • Digits 数値で回答する質問に対して入力されたキーが含まれます。
                                • CallSid 通話に対する一意の識別子が含まれます。

                                Finally we redirect to our question controller, which will ask the next question in the interview loop. This is done via the Voice::CreateResponse.for method.


                                      Handle user responses


                                      We're almost done! Now let's find a way to visualize our survey results.



                                      For this route, we simply query the database using the Active Record Query Interface.

                                      We display a panel for every question from the survey, and inside every panel, we list the responses from different calls.

                                      You can access this page in the application's root route.


                                            Display survey results at the application index


                                            That's it! If you configured one of your Twilio numbers to work with the application built in this tutorial you should be able to take the survey and see the results under the root route of the application. We hope you found this sample application useful.



                                            If you're a Ruby/Rails developer working with Twilio, you might enjoy these other tutorials:

                                            アポイントメント リマインダー


                                            クリック トゥー コール



                                            このチュートリアルをご覧いただき、ありがとうございます。 ご意見やご感想などございましたら、ぜひお聞かせください。 Twitterからご連絡いただき、どんなものを構築されているかお聞かせください!

                                            Agustin Camino Jose Oliveros Orlando Hidalgo Andrew Baker Kat King David Prothero
                                            Rate this page:


                                            誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。