メニュー

Expand
ページを評価:

How to Record Phone Calls in Python

In this guide we’ll show you how to use Twilio Programmable Voice to record phone calls with your Python web application.

We’ll learn how to record both inbound and outbound calls. In both examples we’ll use Twilio's Python SDK.

Webアプリケーションを設定する

着信通話

Twilio makes answering a phone call as easy as responding to an HTTP request. When a phone number you have bought through Twilio receives an incoming call, Twilio will send an HTTP request to your web application asking for instructions on how to handle the call. Your server will respond with an XML document containing TwiML that instructs Twilio on what to do with the call. Those instructions can direct Twilio to read out a message, play an MP3 file, make a recording and much more.

通話への応答を開始するには、以下のことが必要になります:

  • Buy and configure a Twilio-powered phone number capable of making and receiving phone calls, and point it at your web application
  • Webアプリケーションを記述し、TwiMLを使って着信通話をどう処理するかをTwilioに指示します
  • 通話の着信があったときにTwilioがHTTPリクエストを発行できるように、Webアプリケーションをインターネットからアクセスできるようにします

電話番号の購入と設定

Twilioコンソール上で、世界中の電話番号を検索、購入できます。 音声通話の可能な番号は、地球上のおよそどこからでも通話を発信および着信することができます。

音声通話の利用可能な電話番号の検索

Once you purchase a number, you'll need to configure that number to send a request to your web application. This callback mechanism is called a webhook. This can be done in the number's configuration page.

着信電話番号のURLの設定

Webhookとは何か

Webhooks are user-defined HTTP callbacks. They are usually triggered by some event, such as receiving an SMS message or an incoming phone call. When that event occurs, Twilio makes an HTTP request (usually a POST or a GET) to the URL configured for the webhook.

To handle a webhook, you only need to build a small web application that can accept the HTTP requests. Almost all server-side programming languages offer some framework for you to do this. Examples across languages include ASP.NET MVC for C#, Servlets and Spark for Java, Express for Node.js, Django and Flask for Python, and Rails and Sinatra for Ruby. PHP has its own web app framework built in, although frameworks like Laravel, Symfony and Yii are also popular.

どのフレームワークや言語を選んだにせよ、WebhookはあらゆるTwilioアプリケーションに対して同様に機能します。 これらはTwilioに与えられたURIに対するリクエストを生成します。 アプリケーションはデータベースの読み書き、他のAPIとの統合、あるいは何らかの演算など、必要とされるあらゆる処理を実行し、それからTwilioに実行してほしい指示をTwiMLのレスポンスとしてTwilioに応答します。

TwiMLとは何か

TwiML is the Twilio Markup Language, which is just to say that it's an XML document with special tags defined by Twilio to help you build your SMS and voice applications. TwiML is easier shown than explained. Here's some TwiML you might use to respond to an incoming phone call:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Say>Thanks for calling!</Say>
</Response>

And here's some TwiML you might use to respond to an incoming SMS message:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>We got your message, thank you!</Message>
</Response>

すべてのTwiMLドキュメントはルートの<Response>要素と、その中に含まれるひとつ以上の動詞からなります。 動詞とは、電話をかけてきた人にあいさつする<Say>や、受信メッセージへの返信としてSMSを送信する<Message>といったような、Twilioにおこなってほしいアクションのことです。 TwiMLで実行できることがあまねく網羅されたリファレンスについては、TwiML APIリファレンスを参照してください。

着信通話を録音する

Now comes the fun part — writing Python that will handle an incoming HTTP request from Twilio!

In this example we’ll use the Flask web framework to respond to Twilio’s request. We’ll then use TwiML to tell Twilio how to handle the call.

        
        
        
        <Record> TwiML動詞を使って、発信者からのメッセージを録音します

        着信通話の一部の録音

        <Record> TwiML動詞を使って、発信者からのメッセージを録音します

        This code is generating the <Record> TwiML verb, check the documentation for additional options and configuration. You can find all recordings in your console. In addition, you can make use of callbacks to be notified when a recording is ready for use.

        発信通話を録音する

        Next up, we’ll record an outgoing call by writing a little Python. First, we’ll need to retrieve the credentials for our Twilio account.

        Twilioアカウントの認証情報を取得する

        First, you'll need to get your Twilio account credentials. They consist of your AccountSid and your Auth Token. They can be found on the home page of the console.

        Twilioアカウントの認証情報の取得

        通話の発信と録音

        Just pass an extra argument, record, to the client.calls.create method and Twilio will record the entire phone call.

              
              
              

              Once the call is complete, you can listen to your recordings in your Twilio Console or access them directly through Twilio’s REST API.

              You can also receive a Webhook request when your recording is ready by passing a RecordingStatusCallback URL with your client.calls.create command. Twilio will send a request to the URL you specified, and that request will include a link to the recording’s audio file: look for the RecordingUrl field.

              You can learn more about the RecordingStatusCallback parameter in the Making Calls reference docs.

              関連トピック

              We just learned to record inbound and outbound phone calls using Twilio Programmable voice and Python. If you’d like to learn more about how you can use Twilio and Python, check out these handy guides:

              Andrew Baker Rob Spectre David Prothero Ricky Robinett Kevin Whinnery Kat King Craig Dennis Tony Smith
              ページを評価:

              ヘルプが必要ですか?

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

                    
                    
                    

                    フィードバックくださりありがとうございます!

                    We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

                    Sending your feedback...
                    🎉 Thank you for your feedback!
                    Something went wrong. Please try again.

                    Thanks for your feedback!

                    Refer us and get $10 in 3 simple steps!

                    ステップ1

                    Get link

                    Get a free personal referral link here

                    ステップ2:

                    Give $10

                    Your user signs up and upgrade using link

                    ステップ3

                    Get $10

                    1,250 free SMSes
                    OR 1,000 free voice mins
                    OR 12,000 chats
                    OR more