How to Send SMS Text Messages Using Python

Written by plivo | Published 2022/06/23
Tech Story Tags: plivo | good-company | python | sms | programming | software-development | hackernoon-top-story | automation

TLDRYou can automate sending out SMS with any language. Python perennially tops the list of popular languages, so let’s look at how to send text messages using Python. The trick isn’t so much the language you use to send text messages, but rather the API you employ. Sure, theoretically you could write code from scratch that accepted text, formatted it for SMS, and forwarded it to a telecom provider. Fifteen years ago, maybe you would have done just that, but thanks to cloud service providers, nobody needs to do that anymore. Today we have dozens of cloud communications platforms that offer application programming interfaces (API) that anyone can use to add text messaging to applications quickly.via the TL;DR App

You can automate sending out SMS messages with any language.

Python perennially tops the list of popular languages, so let’s look at how to send text messages using Python.

The trick isn’t so much the language you use to send text messages, but rather the API you employ.

Sure, theoretically you could write code from scratch that accepted text, formatted it for SMS, and forwarded it to a telecom provider. Fifteen years ago, maybe you would have done just that, but thanks to cloud service providers, nobody needs to do that anymore.

Today we have dozens of cloud communications platforms that offer application programming interfaces (API) that anyone can use to add text messaging to applications quickly.

An SMS API lets developers integrate sending and receiving text messages with their existing applications. On the back end, it communicates with a communications platform that interfaces with the telecommunications networks and does the actual processing of messages.

To see how it works, we looked at Plivo’s SMS API. Plivo has the top rating for satisfaction among cloud communications platforms on the independent peer-to-peer reviews site G2. Plivo provides software development kits (SDK) in seven languages, including Python.

To get started, sign up for a free Plivo account and use the free credits provided to lease a phone number with which to send and receive messages.

Plivo’s documentation provides instructions on how to set up a Python development environment, including installing the Plivo Python SDK, setting up a Flask server, and installing ngrok, which exposes local servers running behind NATs and firewalls to the public internet over secure tunnels.

You don’t need Flask or ngrok to send SMS messages, but if you plan to receive them, you’ll need to use tools like these.

Create the Send SMS Application

Now you can dive right into coding. Create a file called send_sms.py and paste into it this code.

Replace the auth placeholders with the authentication credentials for your account, which you can find on the front page of the Plivo console.

Replace the phone number placeholders with actual phone numbers in E.164 format (for example, +12025551234). Pick your own mobile number as the destination so you can see the code in action. Then save the file.

import plivo
client = plivo.RestClient('<auth_id>','<auth_token>')
response = client.messages.create(
	src='<sender_number>',
	dst='<destination_number>',
	text='Hello, world!',)
print(response)

Test Your Application

Now run the program.

$ python send_sms.py

You should receive a “Hello, world!” message on your phone.

Of course this example is about as simple as they come.

You can imagine more complex scenarios — looking up a customer’s phone number and sending them an appointment reminder, for example, or sending someone a security code they can use for two-factor authentication.

However you use it, integrating SMS with your Python applications is simpler than you might have imagined. Text messaging opens up all kinds of opportunities for businesses. There’s no time like the present to use SMS to give your business a competitive advantage.



Written by plivo | Plivo — Enterprise-grade cloud communications stack for your business.
Published by HackerNoon on 2022/06/23