メニュー

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?

JavaおよびSpringを使用した自動アンケート

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

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

どのような仕組みになっているのか概要を以下に示します。

  1. エンドユーザーは、調査電話番号に電話をかけるか SMS を送信します。
  2. Twilio は、その通話またはテキストを取得し、応答方法の指示を求めるためにアプリケーションへ HTTP リクエストを発行します。
  3. Web アプリケーションは、Twilio に対して(TwiML を使用)、電話を介したユーザー入力を Gather または Record するように指示するとともに、SMS を使用している場合は Message でテキスト入力を求めるように指示します。
  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.

        
        
        
        
        src/main/java/com/twilio/survey/SurveyJavaApplication.java

        Initialize the Java application

        src/main/java/com/twilio/survey/SurveyJavaApplication.java
        ここをクリックしてはじめましょう!

        調査の作成

        In order to perform automated surveys we first need to have some questions to ask. For your convenience, this application's repository already includes one survey that can be loaded into the database. If the database is configured correctly this survey will be loaded each time the app starts.

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

              
              
              
              
              src/main/java/com/twilio/survey/util/SurveyParser.java

              Seed database with survey questions

              src/main/java/com/twilio/survey/util/SurveyParser.java

              We want our users to have a way to take this survey, so we still 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 answer a question for your survey over the phone using either their phone's keypad or by speaking. After each interaction Twilio makes 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.

              SMSのアンケートについては、設問の送信されたTwilio番号に別のSMSを返信することでその設問に回答します。

              ユーザーの入力を処理、保存、応答するのは、アプリケーションの役割です。

              このフローを調べて、アプリケーションが実際にどのように機能するのかを見てみましょう。

              Configure your application to work with Twilio

              Twilio 番号の設定

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

              Survey Webhook Config

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

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

                    
                    
                    
                    
                    src/main/java/com/twilio/survey/controllers/SurveyController.java

                    Endpoints for voice and sms requests to your survey

                    src/main/java/com/twilio/survey/controllers/SurveyController.java

                    You've configured your webhooks in the Twilio Console. Let's learn how to handle requests to our Twilio endpoints.

                    Respond to the Twilio request

                    Twilio リクエストへの応答

                    Right after receiving a call or an SMS, Twilio sends a request to the URL specified in our phone number configuration (/survey/call for calls and /survey/sms for sms).

                    Each of these endpoints will receive the request and will use a TwiMLUtil to return a welcome message to the user. For voice call users, the message will contain a Say verb with the message, whereas if the user is interacting with our survey over SMS, the message will use a Message verb.

                    We will also include a Redirect verb pointing to the question's endpoint in order to continue the survey flow.

                          
                          
                          
                          
                          src/main/java/com/twilio/survey/controllers/SurveyController.java

                          Welcome a user and redirect to the question controller

                          src/main/java/com/twilio/survey/controllers/SurveyController.java

                          We've seen how to handle requests to our webhooks. Now let's respond to some messages.

                          Respond to incoming messages

                          質問コントローラー

                          This endpoint will check to see if our inbound request is an SMS or voice call, instantiating the proper class to build the correct TwiML response. Each type of question and interaction (Call or SMS) will produce different instructions on how to proceed. For instance, we can record voice or gather a key press during a call, but we can't do the same for text messages.

                          When the user is interacting with our survey 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 /survey/sms main endpoint. To solve that, we can use Twilio Cookies to keep track of what question is being answered at a given moment. This is done with the createSessionForQuestion method.

                                
                                
                                
                                
                                src/main/java/com/twilio/survey/controllers/QuestionController.java

                                Create and manage survey sessions

                                src/main/java/com/twilio/survey/controllers/QuestionController.java

                                Next, we'll see how to build TwiML to handle responses to our survey questions.

                                Twilioリクエストに返信する

                                TwiML 動詞の構築

                                If the question is "numeric" or "yes-no" in nature, we need to use the <Gather> verb. However, if we expect the user to record a free-form voice answer we need to 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 as a callback. This endpoint is 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 all useful text, making a request to our response endpoint when the transcription is complete.

                                      
                                      
                                      
                                      
                                      src/main/java/com/twilio/survey/util/TwiMLUtil.java

                                      Record and gather survey responses

                                      src/main/java/com/twilio/survey/util/TwiMLUtil.java

                                      We've seen how to generate questions with TwiML. Now, lets see how to handle the responses.

                                      Save user responses

                                      レスポンスの処理

                                      After the user has finished speaking and pressing keys, Twilio sends a request telling us what happened and asking for further instructions.

                                      At this point, we need to recover data from Twilio's request parameters (ResponseParser does this) and store them with our persistResponse method.

                                      Recovered parameters vary according to what we asked in our survey questions:

                                      • Body contains the text message from an answer sent over SMS.
                                      • Digits 数値で回答する質問に対して入力されたキーが含まれます。
                                      • RecodingUrl contains the URL for listening to a recorded message.
                                      • TranscriptionText contains the text of a voice recording.

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

                                            
                                            
                                            
                                            
                                            src/main/java/com/twilio/survey/controllers/ResponseController.java

                                            Process and store user survey responses

                                            src/main/java/com/twilio/survey/controllers/ResponseController.java

                                            Now, let's see how to visualize the results of our survey.

                                            調査結果を表示する

                                            調査結果の表示

                                            For this route we simply query the database using a JPA query and then display the information within a Mustache template. We display a panel for every question in the survey, and inside each panel we list the responses from different calls.

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

                                                  
                                                  
                                                  
                                                  
                                                  src/main/java/com/twilio/survey/controllers/DisplayController.java

                                                  Return survey results for template rendering

                                                  src/main/java/com/twilio/survey/controllers/DisplayController.java

                                                  これで終わりです!

                                                  If you have 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.

                                                  関連トピック

                                                  関連トピック

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

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

                                                  近く予定されているアポイントメントに先立ち、お客様へ接触するプロセスを自動化します。

                                                  クリック トゥー コール

                                                  クリックトゥコールはウェブトラフィックをボタンのクリック1つで、電話のトラフィックに変換します。

                                                  これは役に立ちましたか?

                                                  Thanks for checking this tutorial out! If you have any feedback to share with us, we'd love to hear it. Connect with us on Twitter and let us know what you build!

                                                  Mario Celi Jose Oliveros Orlando Hidalgo Andrew Baker Kat King Samuel Mendes
                                                  Rate this page:

                                                  ヘルプが必要ですか?

                                                  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.