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?

C#およびASP.NET MVCを使用した自動アンケート

This ASP.NET MVC sample application demonstrates the use of the Twilio C# helper library and TwiML to deliver a survey to be completed via voice call.

In this tutorial, we'll walk you through the code necessary to power our automated survey. To run this sample app yourself, download the code and follow the instructions on GitHub.

        
        
        
        
        /AutomatedSurvey.Web/Models/Survey.cs

        Create a model to represent a survey in the database.

        /AutomatedSurvey.Web/Models/Survey.cs

        Building an automated survey can seem like a daunting project to take on. With the help of the Twilio C# helper library, we will do this in a few simple steps! Click the button below to get started.

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

        ここをクリックしてチュートリアルをはじめましょう

        Configuring the Twilio Phone Number

        To receive incoming calls, we need to setup our Twilio phone number.

        IVRのWebhook構成


        As you can see, the Twilio Console makes it easy to setup your webhooks for handling SMS messages and phone calls for your phone numbers. Setting up your Twilio phone number is a critical step for letting Twilio know where to forward messages and calls. If you don't already have a running server for this project, you can test your webhooks locally using ngrok.

        Up next, we will dynamically create a survey from a series of questions stored in a database.

        調査を作成する

        調査の作成

        質問は、Configuration.Seed メソッドを使って作成します。このアプローチの利点は、Update-Database を実行したときにデータが設定されることです。

              
              
              
              
              /AutomatedSurvey.Web/Migrations/Configuration.cs

              Seed survey questions in the DB.

              /AutomatedSurvey.Web/Migrations/Configuration.cs

              Now you know how to seed your database with the first survey. Next, we need to generate the TwiML for the initial response sent to the user.

              See how we respond to the initial request

              Responding to Twilio's Initial Request

              Whenever one of your Twilio phone numbers receives a call, Twilio will forward the request to the appropriate webhook.

              For this application Twilio should be configured to make a GET request to the application’s surveys/connectcall endpoint. Here the application finds the last created survey. Then, it uses the <Say> verb to speak the welcome message, and the <Redirect> verb to redirect to the first question in the survey.

                    
                    
                    
                    
                    /AutomatedSurvey.Web/Controllers/SurveysController.cs

                    Generate welcome message with TwiML and redirect users to the first survey question.

                    /AutomatedSurvey.Web/Controllers/SurveysController.cs

                    Now you've seen how to handle incoming calls and generate the TwiML necessary to redirect the user to the first question. In the next section, we will see how to build a voice response and gather user input at the same time.

                    Learn how to ask a question

                    Asking the Caller a Question

                    At this point, Twilio has made a request for the first question. We’re using the Twilio C# helper library to generate a TwiML response.

                    After using the verb <Say> to ask the user a question, we use either the verb <Gather> or the verb <Record> to collect an answer, depending on what type of question your survey will ask.

                          
                          
                          
                          
                          /AutomatedSurvey.Web/Domain/Response.cs

                          Build a TwiML response for the next question.

                          /AutomatedSurvey.Web/Domain/Response.cs

                          Next, we will explore how to gather diverse types of information such as numbers and voice recordings.

                          Handle different types of questions and answers

                          さまざまなタイプの質問をする

                          If we want a number or boolean (yes/no) response from the user, we use the <Gather> verb. For a free-form speech response, we use <Record> to collect an answer.

                          どちらの TwiML タグにも action 属性と method 属性があります。Twilio は両方の属性を使って、ユーザーの回答により、もう 1 つ HTTP リクエストをアプリケーションに発行します。

                          More specifically, the action attribute will be set to POST to an action method named answers/create which will be used to save the user's answer.

                                
                                
                                
                                
                                /AutomatedSurvey.Web/Domain/Response.cs

                                Generate the gather verb to correspond to the question type.

                                /AutomatedSurvey.Web/Domain/Response.cs

                                Now you can generate the TwiML response that will ask your question, as well as set the correct method to gather input that will best serve your analysis. However, we still need to store all of the information gathered in the various steps of the survey for use later on.

                                発信者のレスポンスを記録する

                                発信者のレスポンスを記録する

                                When the callers have finished entering their responses, Twilio will make a request to this controller with all the call parameters we will need. For this sample application, we will store either the RecordingUrl for free-form voice responses, or parse Digits for number and boolean responses. We'll also track the CallSid so we can track answers for a particular survey response.

                                      
                                      
                                      
                                      
                                      /AutomatedSurvey.Web/Controllers/AnswersController.cs

                                      Save the answer to the DB and redirect the user to the next question.

                                      /AutomatedSurvey.Web/Controllers/AnswersController.cs

                                      Most surveys include more than a single question so we must now redirect the user to the next question in this survey until the survey is complete. Once the survey is complete, you'll probably want an easy way to look at the survey results.

                                      Learn how to display survey results

                                      調査結果を表示する

                                      この経路では、Razor テンプレートを使って、データベースに問い合わせ、情報を表示するだけです。

                                      アプリケーションのこのページには、/surveys/results からアクセスできます。

                                            
                                            
                                            
                                            
                                            /AutomatedSurvey.Web/Controllers/SurveysController.cs

                                            As you saw, building an application using the Twilio C# helper library was very straightforward. We hope you found this sample application useful.

                                            関連トピック

                                            次はどこでしょうか?

                                            If you are a C# developer working with Twilio, you might want to check out other tutorials:

                                            Employee Directory with C# and ASP.NET MVC

                                            Twilio を使用して SMS メッセージを受け入れ、SQL データベースに対するクエリーに入れます。

                                            IVR: C#、ASP.NET MVCを使用したフォンツリー

                                            IVRs (interactive voice responses) are automated phone systems that can facilitate communication between callers and businesses.

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

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

                                            Agustin Camino Jose Oliveros Orlando Hidalgo Andrew Baker Kat King Hector Ortega
                                            Rate this page:

                                            ヘルプが必要ですか?

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