メニュー

Expand
ページを評価:

Twilio Python ヘルパーライブラリー

The Twilio Python Helper Library makes it easy to interact with the Twilio API from your Python application. The most recent version of the library can be found on PyPi. The Twilio Python Helper Library supports Python applications written in Python 2.7 and above. If you are interested in migrating to the newer 6.x version of the Twilio Python Helper Library from the 5.x version, check out this guide.

ライブラリーをインストールする

ライブラリーのインストールは、Python用のパッケージマネージャーであるpipを使用してPyPiから行うのがもっとも簡単な方法です。 ターミナルから以下を実行するだけです。

pip install twilio

pip: command not foundが表示された場合はeasy_installを利用できます。ターミナルで下記を実行して下さい。

easy_install twilio

手動インストール

または、twilio-pythonソースコード (ZIP) をダウンロードして、下記を実行しライブラリーをインストールします:

python setup.py install

twilio-pythonライブラリが含まれるフォルダ中で実行します。

"Permission Denied" (権限がありません)

If the command line gives you a big long error message that says Permission Denied in the middle of it, try running the above commands with sudo (e.g. sudo pip install twilio).

インストールを確認する

Try sending yourself an SMS message. Save the following code sample to your computer with a text editor. Be sure to update the account_sid, auth_token, and from_ phone number with values from your Twilio account. The to phone number can be your own mobile phone.

        
        
        

        send_sms.pyという名前でファイルを保存します。 ターミナル上で、先ほど保存したファイルを含むディレクトリーにcdします。

        python send_sms.py

        お使いの電話きでテキストメッセージが受信されるはずです。

        It's okay to hardcode your credentials when testing locally, but you should use environment variables to keep them secret before committing any code or deploying to production. Check out How to Set Environment Variables for more information.

        Using this Library

        Authenticate Client

        
        from twilio.rest import Client
        
        account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        auth_token = "your_auth_token"
        client = Client(account_sid, auth_token)
        

        Create a New Record

        from twilio.rest import Client
        
        account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        auth_token = "your_auth_token"
        client = Client(account_sid, auth_token)
        
        call = client.calls.create(
            to="+14155551212",
            from_="+15017250604",
            url="http://demo.twilio.com/docs/voice.xml"
        )
        
        print(call.sid)
        

        Get Existing Record

        from twilio.rest import Client
        
        account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        auth_token = "your_auth_token"
        client = Client(account_sid, auth_token)
        
        call = client.calls.get("CA42ed11f93dc08b952027ffbc406d0868")
        print(call.to)
        

        Iterate Through Records

        from twilio.rest import Client
        
        account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        auth_token = "your_auth_token"
        client = Client(account_sid, auth_token)
        
        for sms in client.messages.list():
            print(sms.to)
        

        The library automatically handles paging for you. Collections, such as calls and messages, have list and stream methods that page under the hood. With both list and stream, you can specify the number of records you want to receive (limit) and the maximum size you want each page fetch to be (page_size). The library will then handle the task for you.

        list eagerly fetches all records and returns them as a list, whereas stream returns an iterator and lazily retrieves pages of records as you iterate over the collection. You can also page manually using the page method.

        For more information about these methods, view the auto-generated library docs.

        Handling exceptions

        Version 6.x of twilio-python exports an exception class to aid with handling exceptions that are specific to Twilio methods.

        To use it, import the TwilioRestException and catch it in a given try-except block.

        from twilio.base.exceptions import TwilioRestException
        
        try:
            # This could potentially throw an exception!
            message = client.messages.create(
                to="+15558675309", 
                from_="+15017250604",
                body="Hello there!")
        except TwilioRestException as e:
            # Implement your fallback code
            print(e)
        

        追加ドキュメント

        Pythonヘルパーライブラリーを使用できるようになったら、TwilioのREST APIドキュメントやあらゆるTwilioプロダクトのドキュメントで最新版のライブラリーを使用するサンプルコードを確認できるでしょう。 また、ここから自動生成された最新版SDKのライブラリードキュメントをご覧いただけます。

        バージョン5.xのヘルパーライブラリーにアクセスする

        廃止予定のお知らせ

        New functionality will only be added to the new library (Python Helper Library 6.x). The old library (5.x) is no longer supported: Twilio will not provide bug fixes and Support might ask you to upgrade before debugging issues. Learn how to migrate your existing application.

        The most recent version of the Python Helper Library is not API compatible with the previous 5.x version you may have used in previous Twilio applications. The older version will continue to work, and you will continue to find sample code for this version throughout our documentation. Should you need to install this version you can do so with the following command:

        pip install twilio==5.7.0

        お問い合わせ

        We'd love to hear your feedback on the Twilio Python Helper Library and help you past any issues you may encounter. Feel free to drop us a line, and we'll make sure to get you sorted!

        ページを評価:

        ヘルプが必要ですか?

        We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Community Forums or browsing the Twilio tag on Stack Overflow.

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

        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