C#でプッシュボタン (DTMF信号) からのユーザー入力を収集する
In this guide, we'll show you how to gather user input during a phone call through the phone's keypad (using DTMF tones) in your ASP.NET application. By applying this technique, you can create interactive voice response (IVR) systems and other phone based interfaces for your users. The code snippets in this guide are written using modern C# language features and require the .NET Framework version 4.5 or higher. They also make use of the Twilio C# SDK. If you need help creating a new ASP.NET MVC project, check out our guide on the topic.
はじめましょう!
着信通話を受けるためにASP.NET MVCアプリケーションを設定する
This guide assumes you have already set up your web application to receive incoming phone calls. If you still need to complete this step, check out this guide. It should walk you through the process of buying a Twilio number and configuring your app to receive incoming calls from it.
<Gather> TwiML動詞でユーザー入力を収集する
The <Gather> TwiML verb allows us to collect input from the user during a phone call. Gathering user input through the keypad is a core mechanism of Interactive Voice Response (IVR) systems where users can press "1" to connect to one menu of options and press "2" to reach another. These prompts can be accompanied by voice prompts to the caller, using the TwiML <Say> and <Play> verbs. In this example, we will prompt the user to enter a number to connect to a certain department within our little IVR system.
If the user doesn't enter any input after a configurable timeout, Twilio will continue processing the TwiML in the document to determine what should happen next in the call. When the end of the document is reached, Twilio will hang up the call. In the above example, we use the <Redirect> verb to have Twilio request the same URL again, repeating the prompt for the user
If a user were to enter input with the example above, the user would hear the same prompt over and over again regardless of what button you pressed. By default, if the user does enter input in the <Gather>, Twilio will send another HTTP request to the current webhook URL with a POST parameter containing the Digits entered by the user. In the sample above, we weren't handling this input at all. Let's update that logic to also process user input if it is present.
ユーザー入力収集後に行われるアクション (action) を指定する
アプリケーション中にまったく別の呼び出し先を設けて、ユーザー入力を処理したいこともあるでしょう。 <Gather>動詞の"action"属性を使えばこれが可能です。 サンプルを書き換え、ユーザー入力の処理を受け持つ第2の呼び出し先を追加してみましょう。
action属性には、サーバーで処理を実行できる別ルートへの相対URLを指定します。 これで、単一のルートでの条件分岐のロジックの代わりに、action属性を使って通話ロジックを別のコードの流れで処理するようリダイレクトすることができます。
関連トピック
C#とASP.NETでコールセンター型のアプリケーションを構築されているなら、IVRシステムを実装した完全なサンプルアプリケーションのコードを1行ずつ追うのも有意義でしょう。
ヘルプが必要ですか?
誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。