メニュー

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?

IVR: PythonおよびFlaskを使用した電話ツリー

ET Phone Home: IVR Python & Flask Example

This Python Flask sample application is modeled after a typical call center experience, but with more Reese's Pieces.

Stranded aliens can call a phone number and receive instructions on how to get out of earth safely, or call their home planet directly. In this tutorial, we'll show you the critical bits of code that make this work.

To run this sample app yourself, download the code and follow the instructions on GitHub. You can also look at this GitHub repository to see how we've structured our application's file structure.

Read how Livestream and others built Interactive Voice Response with Twilio. Find source code for many web frameworks on our IVR tutorial page.

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

音声通話への応答

電話ツリーを始めるには、着信を受けたときに HTTP リクエストをウェブアプリケーションに送信するように、いずれかの Twilio 番号を設定しておく必要があります。

Click on one of your numbers and configure the Voice URL to point to our app. In our code, the route will be /ivr/welcome.

IVRのWebhook構成

If you don't already have a server configured to use as your webhook, ngrok is an excellent tool for testing webhooks locally.

構成済みのTwilio番号で、Twilioのリクエストに応答する準備が整います。

TwilioにTwiMLを伴って応答する

TwiML で Twilio リクエストに応答する

これで、Twilio 番号は、音声通話の着信があると、このコントローラーメソッドに HTTP リクエストを送信するように設定されました。アプリは TwiML で応答し、メッセージに対する応答として何をすべきか Twilio に指示します。

In this case, we tell Twilio to Gather the input from the caller and then Play a welcome message.

You may have noted we're using an unknown method, `TwiML`. This method comes from a custom view helper that takes a TwiML Response and transforms it into a valid HTTP Response. Check out the implementation:

import flask

def twiml(resp):
    resp = flask.Response(str(resp))
    resp.headers['Content-Type'] = 'text/xml'
    return resp
        
        
        
        
        ivr_phone_tree_python/views.py

        To see the full layout of this app, you can check out its file structure in the GitHub repository.

        After playing the audio and retrieving the caller's input, Twilio will send this input to our application.

        See where to send the caller's input

        発信者の入力を送信する場所

        The gather's action parameter takes an absolute or relative URL as a value - in our case, this is the menu endpoint.

        When the caller finishes entering digits, Twilio will make a GET or POST request to this URL and include a Digits parameter with the number our caller chose.

        After making its request, Twilio will continue the current call using the TwiML received in your response. Note that any TwiML verbs occurring after a <Gather> are unreachable unless the caller does not enter any digits.

              
              
              
              
              ivr_phone_tree_python/views.py

              Now that we have told Twilio where to send the caller's input, we can look at how to process that input.

              Process the caller's selection

              メインメニュー: 発信者の選択を処理

              この経路は、発信者の入力の処理を扱います。

              If our caller chooses '1' for directions, we use the _give_instructions method to respond with TwiML that will Say directions to our caller's extraction point.

              If the caller chooses '2' to call their home planet, then we need to gather more input from them. We wrote another method to handle this, _list_planets, which we'll cover in the next step.

              If the caller enters anything else, we respond with a TwiML Redirect to the main menu.

                    
                    
                    
                    
                    ivr_phone_tree_python/views.py

                    Main menu and return instructions

                    ivr_phone_tree_python/views.py

                    If the caller chooses '2', we will take them to the Planet Directory to collect more input.

                    発信者から追加の入力を得る

                    惑星案内板: 発信者からさらに入力を収集する

                    If our callers choose to call their home planet, we will read them the planet directory. Our planet directory is similar to a typical "company directory" feature of most IVRs.

                    In our TwiML response, we again use a Gather verb to get our caller's input. This time, the action verb points to the planets route, which will map our response to what the caller chooses.

                    Let's look at that route next. The TwiML response we return for that route uses a Dial verb with the appropriate phone number to connect our caller to their home planet.

                          
                          
                          
                          
                          ivr_phone_tree_python/views.py

                          Again, we show some options to the caller and instruct Twilio to collect the caller's choice.

                          See how we use the caller's input to call another number

                          惑星電話番号ディレクトリー:発信者を別の番号に接続する

                          In this route, we grab the caller's digit selection from the HTTP request and store it in a variable called selected_option. We then use a Dial verb with the appropriate phone number to connect our caller to their home planet.

                          The current numbers are hardcoded, but you could update this code to read phone numbers from a database or a file.

                                
                                
                                
                                
                                ivr_phone_tree_python/views.py

                                That's it! We've just implemented an IVR phone tree that will help get ET back home.

                                関連トピック

                                次はどこでしょうか?

                                Twilio を使う Python/Flask 開発者であれば、他のチュートリアルも調べてみてください。

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

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

                                Authy を使った 二要素認証

                                Twilio および Twilio 駆動型 Authy OneTouch を使用して、二要素認証(2FA)をウェブアプリケーションで実装します。

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

                                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!

                                Kevin Whinnery Agustin Camino Andrew Baker Paul Kamp Kat King Samuel Mendes
                                Rate this page:

                                ヘルプが必要ですか?

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