Book Image

Twilio Best Practices

Book Image

Twilio Best Practices

Overview of this book

Table of Contents (15 chapters)
Twilio Best Practices
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Python


We can install the Twilio package for Python using pip, which is Python's standard package manager.

Note

If you don't have pip installed, follow the instructions at https://pip.pypa.io/en/latest/installing.html to install it.

To install the package, simply run the pip install twilio command.

In your code, you'll just need to import the library, and then you can instantiate the client (client.py) like this:

from twilio.rest import TwilioRestClient
import os
account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
client = TwilioRestClient(account_sid, auth_token)

Once you've set up your client, the syntax for sending an SMS is very similar to the one used in other examples, somewhere between the PHP and Ruby implementation, making use of keyword arguments (send_sms.py):

message = client.messages.create(to="+441290211999", from_="+441708300116", body="This is an SMS message.")

Here, our from_ parameter includes an underscore at the end because from...