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

メニュー

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

このアプリケーションについて

This Spark application uses the Twilio API to create an automated survey conducted over SMS or phone call. Callers will interact with this application over the phone, and you will be able to view the results on a dynamic dashboard.

GitHub でこのアプリケーションのソースコードを確認してください。

このチュートリアルでは、Twilio と対話し、アプリケーションを実行するためのコードに焦点を当てます。コードを実行する方法については、GitHub のプロジェクトの README をお読みください。

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

ここをクリックしてはじめましょう!

調査の作成

Before we gather responses, let's specify the questions we would like to ask.

それぞれの質問は、text (尋ねたい質問)と type (その質問をしたときに発信者に要求する入力の種類)の 2 つの属性を持つ JSON オブジェクトとして作成されます。

アプリでは、Google の Gson ライブラリーを使って、この JSON を Java オブジェクトに読み込みます。

        
        
        
        
        questions.json

        Our users will need to contact us in order to take our survey. Let's see how to accept a call or SMS.

        Accept a call with Twilio

        Accepting a Call

        Every time your Twilio number receives a call or an SMS, Twilio will send an HTTP request to the application asking what to do next. The application responds with TwiML that describes the action (<Say> a phrase, <Gather> input, <Message> an SMS, among others).

        Your Twilio number must be configured to make requests to the /interview route of this application. Go to your Manage Numbers page, and set up a new TwiML application using one of your Twilio numbers.

        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

              Handle an incoming call

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

              Now that we can receive a call, let's look at how to ask our user a question.

              Ask a question via SMS or voice call

              質問をする

              Once the application receives an HTTP request from Twilio, it uses a different factory based on whether an SMS or phone call generated the request. This factory helps us build the appropriate TwiML response.

              For SMS requests, the application will use the <Message> verb to answer all requests.

              For requests generated by phone calls, the TwiML will use the <Say> verb for the question's text, and <Gather> or <Record> to collect input based on expected user input.

              TwiML verbs have properties that allow you to change the way the verb works. For example, the <Record> verb always collects audio input, but using the transcribeCallback property, you can specify a route to which Twilio will send a transcript of the audio input. For more on TwiML verbs and their properties, check out the TwiML API documentation.

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

                    Build TwiML for survey questions

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

                    Let's see how to work with the transcription of a recorded response.

                    Transcribe voice messages

                    Using a Transcript of a Recorded Response

                    When Twilio finishes transcribing a free-form voice response, the application finds the corresponding survey and updates the response for that question to include the transcription. This makes it easier to skim the responses without listening to the full call.

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

                          Update survey question with the transcribed answer

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

                          Let's take a look at how to store the caller's response.

                          Store survey responses

                          Storing the Caller's Response

                          After each TwiML verb is executed and input is collected, Twilio sends that input to the application along with a request for more TwiML. The
                          /interview route in our controller handles this input and passes it to the model to update the survey. Using MongoDB's Morphia document-object mapper, we can update the question index, responses, and completeness status of the survey.

                                
                                
                                
                                
                                src/main/java/com/twilio/survey/models/SurveyService.java

                                Update the survey in the database

                                src/main/java/com/twilio/survey/models/SurveyService.java

                                Finally, let's take a look at how to display our survey results.

                                Render survey results

                                調査結果の表示

                                In order to view our results, the completed surveys are serialized to JSON which then populates the dashboard. The dashboard is served at the root (/) URL of the application. We use jQuery to call this route via Ajax and display the survey results in charts on the home page.

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

                                      Serialize survey questions and answers in a JSON object

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

                                      よくできました。このチュートリアル内で構築するアプリケーションに対してTwilio番号が構成されている場合、アンケートを受けて、アプリケーションのrootルート直下で結果を参照できます。 このサンプルアプリケーションがお役に立つことを願っています。

                                      関連トピック

                                      次はどこでしょうか?

                                      If you're hungry for more ways to work with Twilio APIs in Java, check out one of our other tutorials, like:

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

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

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

                                      Thanks for checking out this tutorial! 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 Agustin Camino Orlando Hidalgo Kat King Samuel Mendes Andrew Baker
                                      Rate this page:

                                      ヘルプが必要ですか?

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