DED9

Send SMS & MMS With Python

This tutorial will show you how to use Twilio’s Programmable SMS to send SMS and MMS using your Python application. The code samples in this tutorial use the Python Twilio helper libraries. Let’s get started!

Sign up for Twilio or sign in to your account

If you’re all set, have a Twilio account and a text-enabled Twilio phone number, and you to the code easily.

Before receiving phone calls and sendings, you must sign up for a Twilio account and purchase a Twilio phone number.

If you’re new to Twilio, you can sign up for a free trial account; to sign up, go to your console and get your account SID and verification password. You need these values for the code example below.

Get a phone number with SMS (and MMS) capabilities

You need a Twilio phone number with texting capabilities to send messages. If you don’t already have a Twilio phone number with texting capabilities, you’ll need to purchase one. After going to the number purchase page, check the SMS box and click on search:

If you can check the MMS box live in the US or Canada and also want to send MMS messages, you are viewing search results, and you can see feature icons in the list of available numbers:

Find the number you like and click buy to add it to your account.

If you’re using a demo account, you’ll need to verify your number through the console before you can test sending SMS to yourself.

Now that you have a Twilio phone number, you can start sending messages to mobile devices.

Sending SMS in Python via REST API

To send outgoing SMS from your Twilio account, you must make an HTTP POST to the Twilio message source.

The Twilio Python library helps you create a new instance of the message resource and specify the Tour message’s From and Body parameters. If you don’t already have the Python helper library installed, you can install it using pip:

Pip iPipall twiliTwilioou is not using pip, and you can find manual installation instructions here.

Now create a file named send_sms.py and enter the following code:

Replace the placeholder values for account_sid and auth_token with your own unique values. You can find these in your Twilio console.

By replacing the from_ number with the Twilio phone number you already purchased, you tell Twilio which phone number to use to send this message.

Next, identify yourself as the recipmessage recipient placing the destination number with your mobile phone number. Both theaters must use E.164 formatting (+ and a country code, for example, +16175551212).

twiliTwilio includes a body parameter that contains the content of the SMS we want to send.

Once you’ve updated the sample code, you can test it by running it from the command line:

python send_sms.py

You will receive the SMS in just a few moments.

Let’s take a moment to understand what happens behind the scenes when you send this request to Twilio.

Twilio’s answer

When Twilio receives your request to send an SMS via the REST API, it checks that you’ve entered a valid Twilio phone number in the from_formld. Twilio will then queue the SMS or return this HTTP error in response to your request.

Assuming your request did not result in any errors, Twilio’s HTTP response will contain the SID of the new message. This unique ID helps us refer to this message later: In thWeted that SID to the terminal.
Twi in the above codelio’s JSON response contains much data about your message. A sample answer might be:

{“sid”: “SMxxxxxxxxxxxxxxx”, “date_created”: “Thu, 09 Aug 2018 17:26:08 +0000”, “date_updated”: “Thu, 09 Aug 2018 17:26:08 +0000”, “date_sent” : nul, “account_sid”: “ACxxxxxxxxxxxxxxx”, “to”: “+15558675310”, “from”: “+15017122661”, “messaging_service_sid”: null, “body”: “This is the ship that made the Kessel Run in fourteen parsecs?”, “status”: “queued”, “num_segments”: “1”, “num_media”: “0”, “direction”: “outbound-api”, “api_version”: “2010-04-01” , “price”: null, “price_unit”: “USD”, “error_code”: null, “error_message”: null, “uri”: “/2010-04-01/Accounts/ACxxxxxxxxx/Messages/SMxxxxxxxxxxxx.json”, “subresource_uris”: { “media”: null } }

You can access any o properties from your Python code, just like when we printed message.sid.

Try adding a print statement like print(message.statu s). Save the file, then run the code with python send_sms.py one more time. You should see the status of your message, “Queued”, printed on your terminal.

Send SMS to multiple recipients in Python

If you want to send sms to multiple recipients in Python, you can create an array of recipients and iterate through each phone number in the array:

numbers_to_message = [‘+15558675310’, ‘+14158141829’, ‘+15017122661′] for number in numbers_to_message: client.messages.create( body=’Hello from my Twilio number!’, from_=’+15017122662′, to=number )

This creates a new message instance for each phone number in the list.

A note about the SMS rate limit in Python

As you send more messages through the API, Twilio queues them up for delivery at your specified rate limit. API requests for messages that exceed specified rates are queued and processed as capacity is available.

If your application tries to queue more than 4 hours of outgoing traffic (for example, queue more than 14,400 messages to Canada on a long code phone number), the API will start returning a 429 error.

How to (MMS) a message containing media in Python

To include media in your text message using Twilio, you’ll need to add the code we wrote above. This time we need to add the media_url parameter.

Create a file called send_mms.py and enter the following code:

Again, update the from_ and to parameters to use your Twilio phone number and your mobile phone number.

The new media_url parameter in this code tells Twilio where to go to get the media we want to insert. This must be a publicly accessible URL: Twilio cannot access URLs that are hidden or require authentication.

Just like when you send a simple text message, Twilio sends information about the message in response to your request. The JSON response will contain the unique SID and URI for your media source:

“subresource_uris”: {“media”: “/2010-04 01/Accounts/ACxxxxxxxxx/Messages/SMxxxxxxxxxxxxxxx/Media.json”}

When the Twilio REST API creates your new message resource, it stores the image at the specified media_url as the media resource. Once created, you can access this resource at any time via the API.

You can print this value from your Python code to see where the image is stored. Add the following line to the end of your send_mms.py file to see your newly rendered media URI:

Save the file and run it from the command line:

python send_mms.py

In less than a few moments you should receive a text message with a picture!

You have now successfully sent some messages with the Twilio Programmable SMS API and Python helper library.

 

Exit mobile version