メニュー

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?

PythonおよびDjangoを使用した自動アンケート

Have you ever wondered how to build an automated survey for Voice or SMS for your Python Django web app? This tutorial shows how it's done using the Twilio Python SDK. We promise a quick build with our Python Helper Library and your Python skills - but handling the eventual results is up to you.

Here's How Our Survey Will Work:

  1. エンドユーザーは、調査電話番号に電話をかけるか SMS を送信します。
  2. Twilio receives the call or text and makes an HTTP request to your application asking for instructions on how to respond.
  3. Your web application instructs Twilio (using TwiML) to gather or record the user input over the phone, and prompt for text input with message if you are using SMS.
  4. 各設問に回答があったら、Twilioは開発者のサーバーにユーザーの入力を伴った別のリクエストを発行します。 ユーザーの入力は、アプリケーションのデータベースに保存されます。
  5. After storing the answer, your Django server will instruct Twilio to redirect the user to the next question or finish the survey.

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 configured. For your convenience, this sample 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 into the database when the app starts.

You can modify the questions from the survey by editing the bear_survey.json file located in the root of the repository and re-running the app's load_survey command:

$ python manage.py load_survey your_survey.json

        
        
        
        
        automated_survey/util.py

        We want users to take our survey, so we still need to implement the handler for SMS and for calls.

        First, let's see what the survey loop looks like!

        インタビューループ

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

        For SMS surveys the user will answer questions by simply replying with another SMS to the Twilio number that sent the question.

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

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

        Configure your application to use a Twilio number

        電話番号を構成する

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

        Click on one of your numbers and configure Voice and Message URLs that point to your server. In our code, the route is /automated-survey/first-survey/ for both Voice and Message. Ifyou don't already have a server set up to run your application, ngrok is a helpful tool for testing your webhooks locally.

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

              
              
              
              
              automated_survey/views/surveys.py

              Up next, let's see how Webhooks can help us with the survey flow.

              See how Twilio handles a call or SMS to your configured endpoint

              Respond to a Twilio Request with Python and Django

              Right after receiving a call or an SMS, Twilio will send a request to the URL specified on the number's configuration.

              The first endpoint will check if the user has already answered a question or not. If not, it means this is the first request, so we need to welcome the user and redirect them to the first question. This is done in show_survey which will use the Say verb for calls and the Message verb for SMS.

              The is_sms property comes from some simple middleware that checks if request has Voice or Message data.

              This endpoint will also include a Redirect to the question's endpoint so that the user can continue the survey.

                    
                    
                    
                    
                    automated_survey/views/surveys.py

                    Configure survey endpoints for your Twilio number

                    automated_survey/views/surveys.py

                    Lets see how we can keep track of the user's survey progress.

                    Track User Survey Progress

                    質問コントローラー

                    This endpoint will check if the survey is taken via SMS or a Call and build the next survey question as a TwiML response. Each type of question and interaction (Call/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 us 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 /automated-survey/first-survey/ endpoint. To solve that, we can use Twilio Cookies to keep track of which question is being answered at a given moment. This is done by setting an answering_question_id session key and leaving cookie management to Django.

                          
                          
                          
                          
                          automated_survey/views/questions.py

                          The Question Controller

                          automated_survey/views/questions.py

                          Now let's see how the response is built.

                          Build some TwiML for responses

                          TwiML 動詞の構築

                          If the question is of a "numeric" or "yes-no" nature then we use the <Gather> verb. However, if we expect the user to record a more robust answer, 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 be used as a callback. This endpoint will be responsible for receiving and storing the caller's answer.

                          During the Record verb creation, we can also ask Twilio for a Transcription. Twilio will process the recording and extraction of useful text, making a request to our response endpoint when this transcription is done.

                                
                                
                                
                                
                                automated_survey/views/questions.py

                                Handle voice and SMS survey questions

                                automated_survey/views/questions.py

                                Now let's learn how to handle our user's response.

                                Handle Submitted Responses

                                レスポンスの処理。

                                After the user has finished submitting their answers, 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 (_extract_request_body does this) and store that data using the save_response_from_request function.

                                Recovered parameters vary according to what we asked in our 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 recording transcription.
                                      
                                      
                                      
                                      
                                      automated_survey/views/question_responses.py

                                      アンケートのレスポンスを処理する

                                      automated_survey/views/question_responses.py

                                      Finally, we redirect to our Question controller, which will ask the next question in the loop. Our next_question_redirect function will handle this for us until the loop is finished.

                                      Display your results

                                      調査結果を表示する

                                      You probably don't want your survey results to just sit buried in your database. For this show_survey_results route we simply query the database for survey answers using the Django ORM and then display the information within a template. We display a panel for every question in the survey, and inside each panel we list the responses from the different calls or messages.

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

                                            
                                            
                                            
                                            
                                            automated_survey/views/surveys.py

                                            調査結果を表示する

                                            automated_survey/views/surveys.py

                                            Congratulations! Now you can send automated surveys to your users with the power of Twilio's API, the Twilio Python Helper Library, and Django.

                                            If you have configured one of your Twilio numbers with the application built in this tutorial you should be able to take the survey yourself and see the results under the root route of the application.

                                            関連トピック

                                            次はどこでしょうか?

                                            このサンプルアプリケーションがお役に立つことを願っています。Twilio を使う Python 開発者であれば、他のチュートリアルもお楽しみください。

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

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

                                            クリック トゥー コール

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

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

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

                                            Andrew Baker Jose Oliveros Orlando Hidalgo Agustin Camino Kat King Samuel Mendes Paul Kamp
                                            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.