メニュー

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およびServletを使用した自動アンケート

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

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

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

  1. エンドユーザーは、調査電話番号に電話をかけるか SMS を送信します。
  2. Twilio は、その通話またはテキストを取得し、応答方法の指示を求めるためにアプリケーションへ HTTP リクエストを発行します。
  3. Your web application instructs Twilio (using TwiML) to Gather or Record user input over the phone, or prompt for text input with Message if the survey is taken via 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.

        
        
        
        
        src/main/java/com/twilio/automatedsurvey/servlets/QuestionServlet.java

        Return a specific survey question via a question servlet

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

        調査の作成

        In order to perform an automated survey, we first need to set up the survey questions. For your convenience, this application's repository already includes one survey. If the database is configured correctly every time that the app receives a request for a survey, it will create a new survey registration.

        You can modify the survey questions by editing the survey.json file located in the root of the repository and re-running the app.

              
              
              
              
              src/main/java/com/twilio/automatedsurvey/servlets/SurveyServlet.java

              Create a new survey within this servlet

              src/main/java/com/twilio/automatedsurvey/servlets/SurveyServlet.java

              We want users to take our survey, so we still need to implement the 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 your survey questions over the phone using either their phone's keypad or by voice input. 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.

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

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

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

              Configure your application to use a Twilio number

              Twilio 番号の設定

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

              Click on one of your numbers to configure your Voice and Message URLs to point to the /survey route using HTTP GET.

              自動アンケートのWebhookセットアップ

              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/automatedsurvey/servlets/SurveyServlet.java

                    Handle HTTP requests to your survey endpoint

                    src/main/java/com/twilio/automatedsurvey/servlets/SurveyServlet.java

                    We that we have configured our webhooks in the Twilio Console, we are ready to create our webhook endpoints.

                    Twilioリクエストに返信する

                    Twilio リクエストへの応答

                    Right after receiving a call or SMS, Twilio sends a request to the URL specified in our phone number configuration.

                    The endpoint will then process the request. If the incoming request is not a response to a previous question, the application will build and return a welcome message for the user. This welcome message is constructed by using a Say verb for a voice response or a Message verb if the user is taking the survey via SMS.

                    Our response also includes a Redirect for redirecting to the question's endpoint in order to continue with the survey flow.

                          
                          
                          
                          
                          src/main/java/com/twilio/automatedsurvey/servlets/SurveyServlet.java

                          Build a response for voice or SMS surveys

                          src/main/java/com/twilio/automatedsurvey/servlets/SurveyServlet.java

                          Thats how you generate TwiML responses in your webhooks. Now lets see how to store the state of the survey using a controller.

                          Build a question controller

                          質問コントローラー

                          This endpoint returns the Question specified in the request. If no specific question is specified in the request, it will return the first question in the survey.

                          各タイプの質問とやり取り(通話またはSMS)は処理の仕方において異なる命令を生成します。 たとえば、通話時には、音声を記録したり、キー入力を収集したりできますが、テキストメッセージの場合は同じ処理を行うことができません。

                          AbstractTwiMLQuestionFactory.getInstance solves that problem. It will return a different type of factory that knows how to build responses depending on wether the request originated via SMS or a voice call.

                          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 are sent to our /survey main endpoint.

                          To solve that, we'll store the id of the survey and question at hand in the HTTP session. We can reuse these for subsequent SMS requests.

                                
                                
                                
                                
                                src/main/java/com/twilio/automatedsurvey/servlets/QuestionServlet.java

                                Return a specific survey question via question servlet

                                src/main/java/com/twilio/automatedsurvey/servlets/QuestionServlet.java

                                レスポンスの構築方法を見てみましょう。

                                Build some TwiML for responses

                                TwiML 動詞の構築

                                If the survey question is numeric or boolean ("yes/no") in nature we will use the <Gather> verb to build our TwiML. However, if we expect the user to record a voice answer we want to use the <Record> verb. Both verbs take an action attribute and a method attribute.

                                Twilio uses both attributes to define our response's endpoint to use 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/automatedsurvey/servlets/TelephoneTwiMLQuestionFactory.java

                                      Build voice responses with TwiML

                                      src/main/java/com/twilio/automatedsurvey/servlets/TelephoneTwiMLQuestionFactory.java

                                      Now lets see what to do with the response.

                                      Handle submitted responses

                                      レスポンスの処理

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

                                      In Survey.answerCall and Survey.answerSMS we extract request parameters from a Map containing the information needed to answer the question.

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

                                      • Body SMS経由で送信された回答のテキストメッセージが含まれます。
                                      • Digits 数値で回答する質問に対して入力されたキーが含まれます。
                                      • RecordingUrl contains the URL for listening to a recorded message.
                                      • TranscriptionText contains the text of a voice recording transcription.

                                      After answering a question, the SurveyController redirects to our QuestionServlet, which will ask the next question in the loop.

                                            
                                            
                                            
                                            
                                            src/main/java/com/twilio/automatedsurvey/survey/Survey.java

                                            Handle SMS and voice responses

                                            src/main/java/com/twilio/automatedsurvey/survey/Survey.java

                                            次に、結果をどのように視覚化するかを見てみましょう。

                                            Display your results

                                            調査結果の表示

                                            For this route we simply query the database using a JPA query and then display the information within a JSP page. We display a list of surveys with their respective questions and answers.

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

                                                  
                                                  
                                                  
                                                  
                                                  src/main/java/com/twilio/automatedsurvey/servlets/IndexServlet.java

                                                  Display all survey results

                                                  src/main/java/com/twilio/automatedsurvey/servlets/IndexServlet.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 開発者であれば、他のチュートリアルもお楽しみください。

                                                  SMS・MMS 通知

                                                  もう 1 つのサーバー停止状態も見逃さないでください。サーバー停止状態が発生した場合に SMS 経由ですべての管理者に警告するサーバー通知システムを構築する方法を学びます。

                                                  クリック トゥー コール

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

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

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

                                                  Agustin Camino Jose Oliveros Orlando Hidalgo Andrew Baker Kat King Hector Ortega
                                                  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.