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?


電話または SMS から回答可能な自動調査をどのように作成するのか知りたいと思ったことはありませんか?

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


  1. エンドユーザーは、調査の電話番号に電話をかけるか SMS を送信します。
  2. Twilio は、その通話またはテキストを取得し、応答方法の指示を求めるためにアプリケーションへ HTTP リクエストを発行します。
  3. Your web application instructs Twilio (using TwiML) to Gather or Record the user's voice input, and prompts text input with Message if you are using SMS.
  4. 各質問の後に、Twilio はユーザーの入力を使ってサーバーに別のリクエストを発行します。ユーザーの入力は、アプリケーションによってデータベースに保存されます。
  5. 回答を保存した後、サーバーは次の質問にユーザーを Redirect するか、または調査を終了するように Twilio に指示します。

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



        In order to perform an automated survey we first need to have some questions we want to ask a user. For your convenience, this sample application's repository already includes one survey that can be loaded into your database. You can take a look at the Readme File for detailed instructions.

        調査の質問は、レポジトリーのルートにある bear_survey.json ファイルを編集し、アプリを再度実行することで、変更できます。


              Load a survey into your application


              Before we dive in, let's take a moment to understand the flow of a Twilio-powered survey as an interview loop.



              The user can answer a question for your survey over the phone using either their phone's keypad or by voice. 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.





              Twilio 番号の設定

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

              いずれかの番号をクリックして、サーバーを指し示す Voice URL と Message URL を設定します。コードでは、経路はそれぞれ /voice/connect/sms/connect になっています。



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

                    Next, we'll see how to handle requests to our webhooks.


                    Twilio リクエストへの応答

                    Right after receiving a call or SMS, Twilio will send a request to the URL specified in that phone number's configuration (/voice/connect for calls and /sms/connect for sms).

                    Each of these endpoints will receive the request and return a welcome message to the user. When a user calls the Twilio number, our application will use the Say verb. For users that respond via SMS, the application will use a Message. It also includes a Redirect to the question's endpoint in order to continue the survey flow.




                          Next, we will see how to build a TwiML response that will play back questions and gather input from the user.

                          Ask survey questions via voice



                          Each type of question will produce different instructions on how to proceed. If the question is "numeric" or "yes-no" in nature then we use the <Gather> verb. However, if we expect the user to record an answer we use the <Record> verb. Both verbs take an action attribute and a method attribute.

                          Twilio uses both attributes to define the response endpoint that will be used as callback. This endpoint will be responsible for receiving and storing the caller's answer.

                          During the Record verb creation, we also ask for a Transcription. Twilio will process the voice recording and extract all useful text. When the transcription is done it will make a request to our response's endpoint.


                                Ask a survey question via voice call


                                Next, we will learn how to ask questions via SMS.

                                Ask survey questions via SMS


                                To ask questions over SMS we just need to use the <Message> TwiML verb. When users start a new survey, they will receive a welcome message as well as a message with the first survey question.

                                When the user is interacting over SMS we don't have something like an ongoing call session with a well defined state. It becomes harder to know if an SMS is answering question 2 or 20, since all requests will be sent to our main SMS endpoint at /sms/connect. To solve that, we can use Twilio Cookies to keep track of what question is being answered at the moment.


                                      Ask a survey question via SMS


                                      Up next, we will see how to store the state of the survey.

                                      Store survey answers


                                      After the user has finished inputting voice commands or pressing keys, Twilio sends us a request that tells us what happened and asks for further instructions.

                                      現時点では、Twilio のリクエストパラメーターからデータをリカバリーして、そのデータを保存しておく必要があります。

                                      Recovered parameters vary according to the questions:

                                      • Body contains the text message from an answer sent with SMS.
                                      • Digits 数値で回答する質問に対して入力されたキーが含まれます。
                                      • RecodingUrl contains the URL for listening to a recorded message.
                                      • TranscriptionText 録音のトランスクリプションの結果が含まれます。

                                      Finally we redirect to our question controller, which will ask the next question in the loop. This is done in the _redirectToQuestion() method.


                                            Store voice, SMS, or a transcription of survey answers



                                            Display your survey results


                                            You can access the survey results from the application's root route.

                                            The results will be shown grouped by survey. To hear recorded answers, you can click on a playback icon to listen to the user's response. Any transcribed voice answers will be shown when available.





                                                  If you have configured one of your Twilio numbers for 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.



                                                  Twilio を使う PHP 開発者であれば、他のチュートリアルもお楽しみください。

                                                  クリック トゥー コール



                                                  テキストメッセージを介した二要素認証を追加することで、Laravel に組み込まれているログイン機能のセキュリティを強化します。


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

                                                  Jose Oliveros Orlando Hidalgo Kat King Hector Ortega Andrew Baker Paul Kamp
                                                  Rate this page:


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