### Install twilio-python from source Source: https://github.com/twilio/twilio-python/blob/main/README.md Installs the twilio-python library by downloading the source code and running the setup script. This method is an alternative to using pip. ```shell python3 setup.py install ``` -------------------------------- ### Install twilio-python using pip Source: https://github.com/twilio/twilio-python/blob/main/README.md Installs the twilio-python library using pip, the Python package installer. Includes troubleshooting for Windows path length issues and instructions for installing pip if not already present. ```shell pip3 install twilio ``` ```shell curl https://bootstrap.pypa.io/get-pip.py | python ``` -------------------------------- ### Manage Call Resources Source: https://github.com/twilio/twilio-python/blob/main/README.md Provides examples for creating a new phone call and retrieving details about an existing call using its SID. ```python from twilio.rest import Client client = Client("ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "your_auth_token") # Create a call call = client.calls.create(to="9991231234", from_="9991231234", url="http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient") print(call.sid) # Get call details call = client.calls.get("CA42ed11f93dc08b952027ffbc406d0868") print(call.to) ``` -------------------------------- ### Generating TwiML Responses with Python Source: https://github.com/twilio/twilio-python/blob/main/README.md This example shows how to generate TwiML (Twilio Markup Language) for voice applications using the `twilio.twiml.VoiceResponse` class. It demonstrates creating a simple response that says a welcome message and then prints the resulting XML. ```python from twilio.twiml.voice_response import VoiceResponse r = VoiceResponse() r.say("Welcome to twilio!") print(str(r)) ``` ```xml Welcome to twilio! ``` -------------------------------- ### Test twilio-python installation by sending an SMS Source: https://github.com/twilio/twilio-python/blob/main/README.md A Python script to verify the twilio-python installation by sending an SMS message. It requires your Twilio Account SID, Auth Token, and phone numbers. The script initializes the Twilio client and creates a message. ```python from twilio.rest import Client # Your Account SID and Auth Token from console.twilio.com account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token) message = client.messages.create( to="+15558675309", from_="+15017250604", body="Hello from Python!") print(message.sid) ``` -------------------------------- ### Run Twilio-Python Tests Source: https://github.com/twilio/twilio-python/blob/main/CONTRIBUTING.md Installs test dependencies and runs the full twilio-python test suite to ensure code changes do not break existing functionality. ```shell make test-install test ``` -------------------------------- ### Environment Variables for Twilio Proxy Configuration Source: https://github.com/twilio/twilio-python/blob/main/advanced-examples/custom-http-client.md This example shows the structure of an `.env` file used to store sensitive information and configuration settings for the Twilio Python application. It includes Twilio Account SID, Auth Token, and proxy addresses for both HTTP and HTTPS protocols. ```env ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx AUTH_TOKEN= your_auth_token HTTPS_PROXY=https://127.0.0.1:8888 HTTP_PROXY=http://127.0.0.1:8888 ``` -------------------------------- ### Initialize Twilio Client Source: https://github.com/twilio/twilio-python/wiki/Python-Version-6.x-Upgrade-Guide Demonstrates the simplified import and initialization process for the Twilio 6.x client. ```python from twilio.rest import Client client = Client('ACxxx', 'AUTHTOKEN') ``` -------------------------------- ### Fetch Resource Instances Source: https://github.com/twilio/twilio-python/wiki/Python-Version-6.x-Upgrade-Guide Shows how to retrieve specific resource instances using the new context-to-instance fetch pattern. ```python call = client.api.v2010.account.calls('CA123xxx').fetch() workspace = client.taskrouter.workspaces('WSxxx') workspace.fetch() ``` -------------------------------- ### Authenticate Twilio Client Source: https://github.com/twilio/twilio-python/blob/main/README.md Demonstrates how to initialize the Twilio client using Account SID/Auth Token, API Key/Secret, or environment variables. ```python from twilio.rest import Client # Using Account SID and Auth Token client = Client("ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "your_auth_token") # Using API Key and Secret client = Client("XXXXXXXXXXXXXXXXX", "YYYYYYYYYYYYYYYYYY", "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") # Using environment variables client = Client() ``` -------------------------------- ### Disable connection pooling Source: https://github.com/twilio/twilio-python/blob/main/CHANGES.md Example of how to disable connection pooling when creating a Twilio client. ```python from twilio.rest import Client from twilio.http.http_client import TwilioHttpClient client = Client( username, password, http_client=TwilioHttpClient(pool_connections=False) ) ``` -------------------------------- ### Enabling Debug Logging for Twilio API Requests Source: https://github.com/twilio/twilio-python/blob/main/README.md These examples demonstrate how to enable debug logging for API requests and responses made by the Twilio client. You can log to the console or to a file by configuring the `logging` module and setting the logger level for `client.http_client.logger`. ```python import logging # Assuming account_sid and auth_token are defined # client = Client(account_sid, auth_token) # Log to console logging.basicConfig() client.http_client.logger.setLevel(logging.INFO) ``` ```python import logging # Assuming account_sid and auth_token are defined # client = Client(account_sid, auth_token) # Log to a file logging.basicConfig(filename='./log.txt') client.http_client.logger.setLevel(logging.INFO) ``` -------------------------------- ### Read and Stream Resources Source: https://github.com/twilio/twilio-python/wiki/Python-Version-6.x-Upgrade-Guide Demonstrates using read() to return an array of resources and stream() for memory-efficient iteration with automatic paging. ```python # Read into array messages = client.api.account.messages.read() # Stream with generator for message in client.api.account.messages.stream(limit=5): print(message.sid) # Stream with paging configuration for number in client.api.account.incoming_phone_numbers.stream(limit=3000, page_size=100): print(number.phone_number) ``` -------------------------------- ### Iterate Through Resource Collections Source: https://github.com/twilio/twilio-python/blob/main/README.md Explains how to use the list method to retrieve resources, including configuring limits and page sizes for efficient data fetching. ```python from twilio.rest import Client client = Client("ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "your_auth_token") # Iterate through messages for sms in client.messages.list(): print(sms.to) # Fetch with limits and page sizes client.messages.list(limit=20, page_size=20) client.messages.list(limit=20, page_size=10) client.messages.list(limit=20, page_size=100) ``` -------------------------------- ### Send SMS with Custom HTTP Client in Twilio Python Source: https://github.com/twilio/twilio-python/blob/main/advanced-examples/custom-http-client.md This example shows how to send an SMS message using a custom HTTP client with the Twilio Python Helper Library. It involves creating a custom request class and passing it to the Twilio Client constructor. This is useful for scenarios like proxy authentication. ```python import os from twilio.rest import Client from custom_client import MyRequestClass from dotenv import load_dotenv load_dotenv() # Custom HTTP Class my_request_client = MyRequestClass() client = Client(os.getenv("ACCOUNT_SID"), os.getenv("AUTH_TOKEN"), http_client=my_request_client) message = client.messages \ .create( to="+15558675310", body="Hey there!", from_="+15017122661" ) print('Message SID: {}'.format(message.sid)) ```