release 5.7.0 twilio inc. · pdf filechapter 1 installation install from pypi usingpip, a...

108
twilio-python Documentation Release 5.7.0 Twilio Inc. Apr 13, 2018

Upload: doanhanh

Post on 04-Feb-2018

272 views

Category:

Documents


1 download

TRANSCRIPT

  • twilio-python DocumentationRelease 5.7.0

    Twilio Inc.

    Apr 13, 2018

  • Contents

    1 Installation 1

    2 Getting Started 3

    3 User Guide 5

    4 Upgrade Plan 45

    5 API Reference 47

    6 Deploying to Google App Engine 87

    7 Frequently Asked Questions 89

    8 Changelog 91

    9 Support and Development 93

    Python Module Index 95

    i

  • ii

  • CHAPTER 1

    Installation

    Install from PyPi using pip, a package manager for Python.

    pip install twilio

    Dont have pip installed? Try installing it, by running this from the command line:

    curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python

    Or, install the library by downloading the source, installing setuptools, navigating in the Terminal to the folder con-taining the twilio-python library, and then running:

    python setup.py install

    1

    http://www.pip-installer.org/en/latest/https://github.com/twilio/twilio-python/zipball/masterhttp://pythonhosted.org/setuptools/

  • twilio-python Documentation, Release 5.7.0

    2 Chapter 1. Installation

  • CHAPTER 2

    Getting Started

    The /getting-started will get you up and running in a few quick minutes. This guide assumes you understand the coreconcepts of Twilio. If youve never used Twilio before, dont fret! Just read about how Twilio works and then jumpin!

    3

    http://www.twilio.com/api/

  • twilio-python Documentation, Release 5.7.0

    4 Chapter 2. Getting Started

  • CHAPTER 3

    User Guide

    Functionality is split over three different sub-packages within twilio-python. Below are in-depth guides to specificportions of the library.

    3.1 REST API

    Query the Twilio REST API to create phone calls, send messages and more!

    3.1.1 Accessing REST Resources

    To access Twilio REST resources, youll first need to instantiate a TwilioRestClient.

    Authentication

    The TwilioRestClient needs your Twilio credentials. While these can be passed in directly to the constructor,we suggest storing your credentials as environment variables. Why? Youll never have to worry about committingyour credentials and accidentally posting them somewhere public.

    The TwilioRestClient looks for TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN inside the currentenvironment.

    With those two values set, create a new TwilioRestClient.

    from twilio.rest import TwilioRestClient

    conn = TwilioRestClient()

    If youd rather not use environment variables, pass your account credentials directly to the the constructor.

    5

  • twilio-python Documentation, Release 5.7.0

    from twilio.rest import TwilioRestClient

    ACCOUNT_SID = "AXXXXXXXXXXXXXXXXX"AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY"client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)

    Proxies

    TwilioRestClient supports HTTP and SOCKS4/5 proxies. You can change the proxy configuration at any timewith the Connection class:

    from twilio.rest.resources import Connectionfrom twilio.rest.resources.connection import PROXY_TYPE_SOCKS5

    Connection.set_proxy_info('example.com',5000,proxy_type=PROXY_TYPE_SOCKS5,proxy_user='username',proxy_pass='password',

    )

    The TwilioRestClient will retrieve and use the current proxy information for each request.

    Listing Resources

    The TwilioRestClient gives you access to various list resources. ListResource.list, by default, returnsthe most recent 50 instance resources.

    from twilio.rest import TwilioRestClient

    # To find these visit https://www.twilio.com/user/accountACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX"AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY"

    client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)resources = client.calls.list()

    resource.ListResource.list() accepts paging arguments. The following will return page 3 with page sizeof 25.

    from twilio.rest import TwilioRestClient

    # To find these visit https://www.twilio.com/user/accountACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX"AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY"

    client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)resources = client.calls.list(page=3, page_size=25)

    6 Chapter 3. User Guide

  • twilio-python Documentation, Release 5.7.0

    Listing All Resources

    Sometimes youd like to retrieve all records from a list resource. Instead of manually paging over the resource, theresources.ListResource.iter method returns a generator. After exhausting the current page, the generatorwill request the next page of results.

    Warning: Accessing all your records can be slow. We suggest only doing so when you absolutely need all therecords.

    from twilio.rest import TwilioRestClient

    # To find these visit https://www.twilio.com/user/accountACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX"AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY"

    client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)for number in client.phone_numbers.iter():

    print number.friendly_name

    Get an Individual Resource

    To get an individual instance resource, use resources.ListResource.get(). Provide the sid of the resourceyoud like to get.

    from twilio.rest import TwilioRestClient

    # To find these visit https://www.twilio.com/user/accountACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX"AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY"

    client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)

    call = client.calls.get("CA123")print call.to

    3.1.2 Messages

    The Messages resource manages all interaction with Twilio SMS and MMS messages. For more information, seethe Message REST Resource documentation.

    Sending a Text Message

    The Message resource allows you to send an SMS message in a few lines of code.

    from twilio.rest import TwilioRestClient

    # To find these visit https://www.twilio.com/user/accountACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX"AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY"

    client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)

    3.1. REST API 7

    http://www.twilio.com/docs/api/rest/message

  • twilio-python Documentation, Release 5.7.0

    message = client.messages.create(body="Hello Monkey!", # Message body, if anyto="+12125551234",from_="+15105551234",

    )print message.sid

    If you want to send a message from a short code on Twilio, just set from_ to your short codes number.

    Sending a Picture Message

    To send a picture message (MMS), set media_url to the url of the picture you wish to send.

    Dont forget to check the availability of MMS in your area before using this functionality.

    from twilio.rest import TwilioRestClient

    # To find these visit https://www.twilio.com/user/accountACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX"AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY"

    client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)

    message = client.messages.create(body="Hello Monkey!", # Message body, if anyto="+12125551234",from_="+15105551234",media_url="http://example.com/image1.jpg"

    )

    You can send multiple pictures in the same message by setting media_url to a list of urls.

    message = client.messages.create(body="Hello Monkey!", # Message body, if anyto="+12125551234",from_="+15105551234",media_url=[ # List of media URLs, if any

    "http://example.com/image1.jpg","http://example.com/image2.jpg",

    ],)

    Retrieving Sent Messages

    from twilio.rest import TwilioRestClient

    # To find these visit https://www.twilio.com/user/accountACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX"AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY"

    client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)

    for message in client.messages.list():print message.body

    8 Chapter 3. User Guide

    http://www.twilio.com/api/sms/short-codeshttps://www.twilio.com/mms

  • twilio-python Documentation, Release 5.7.0

    Filtering Your Messages

    The list() methods supports filtering on to, from_, and date_sent. The following will only show messagesto +5466758723 on January 1st, 2011.

    from datetime import datefrom twilio.rest import TwilioRestClient

    # To find these visit https://www.twilio.com/user/accountACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX"AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY"

    client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)

    messages = client.messages.list(to="+5466758723",date_sent=date(2011,1,1),

    )

    for message in messages:print message.body

    Redacting or Deleting Message Records

    To protect your users privacy and/or comply with legal requirements, Twilio allows you to redact your Messagebodies or delete the records outright.

    from twilio.rest import TwilioRestClient

    # To find these visit https://www.twilio.com/user/accountACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX"AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY"

    client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)message_sid = "MM123"

    client.messages.redact(message_sid)message = client.messages.get(message_sid)print message.body # Will be an empty string

    client.messages.delete(message_sid) # Deletes record entirely, subsequent requestswill return 404

    3.1.3 Phone Calls

    The Calls resource manages all interaction with Twilio phone calls, including the creation and termination of phonecalls. For more information, see the Calls REST Resource documentation.

    Making a Phone Call

    The Calls resource allows you to make outgoing calls. Before a call can be successfully started, youll need a to setup a url endpoint which outputs valid TwiML. This can be done with the :class: twiml.Response class, get started here.

    3.1. REST API 9

    http://www.twilio.com/docs/api/rest/callhttp://www.twilio.com/docs/api/twiml/http://twilio-python.readthedocs.org/en/latest/usage/twiml.html#twiml-creation

  • twilio-python Documentation, Release 5.7.0

    from twilio.rest import TwilioRestClient

    # To find these visit https://www.twilio.com/user/accountACCOUNT_SID = "AC