How to send SMS in Python using Plivo's Messaging API

Your company has settled on Plivo to handle its voice and messaging communications, and now it’s your job to start integrating Plivo into your company’s applications. Don’t worry — Plivo has an SDK to help you out. Let’s see how to send and receive messages through Plivo in a Python application.

Install the Plivo SDK

We’ll presume you already have Python installed. Installing the Plivo SDK is as simple as running

$ pip install plivo

If you prefer to install from source code, visit our Quickstart Guide for instructions.

Find your Auth ID and Auth Token

You have to have proper credentials before you can use the Plivo API. We provide an Auth ID and Auth Token in the Account section at the top of your Plivo Console.

credentials

Choose a phone number

You need an SMS-enabled Plivo phone number to send messages to the US and Canada due to the carrier regulations imposed by those countries. Check the Numbers screen of your Plivo console to see what numbers you have available and which of them support SMS capabilities. You can also buy numbers from this screen.

Buy New Number

The SMS regulations followed by the carriers vary from country to country. For messages to countries other than the US and Canada, you might want to register an alphanumeric sender ID for your messages. You can learn more about the use of alphanumeric sender ID and register one from your Plivo Console.

Send an SMS message

Now you’re ready to start. You can send an SMS message with just a few lines of code:

1
2
3
4
5
6
7
8
import plivo

client = plivo.RestClient("auth_id","auth_token")
message_created = client.messages.create(
    src='plivo_src_number',
    dst='the_destination_number',
    text='Hello, world!'
)

Replace the placeholders with actual values. Save the file as SendSMS.py. Run it using the command

$ python SendSMS.py

Note: If you’re using a Plivo trial account, you can send messages only to phone numbers that have been verified with Plivo. You can verify a phone number using the Sandbox Numbers page of the Console.

Receive an SMS message

Of course sending messages is only half of the equation. Plivo supports receiving SMS text messages in many countries (see our SMS API coverage page, and click on the countries you’re interested in). When someone sends an SMS message to a Plivo phone number, you can receive it on your server by using a Flask web app.

First, optionally, set up a virtual environment to keep these packages isolated from others on your system. Then create a file called receive_sms.py (or whatever name you like) with this code in it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from flask import Flask, request

app = Flask(__name__)

@app.route('/receive_sms/', methods=['GET', 'POST'])
def inbound_sms():

    from_number = request.values.get('From')
    to_number = request.values.get('To')
    text = request.values.get('Text')
    print('Message received - From: %s, To: %s, Text: %s' %(from_number, to_number, text))

    return 'Message Recevived'

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

Run it with the command

$ python receive_sms.py

You should then be able to see your basic server app in action on http://localhost:5000/receive_sms/.

That’s fine for testing, but it’s not much good if you can’t connect to the internet to receive incoming messages and handle callbacks. For that, we recommend using ngrok, which exposes local servers behind NATs and firewalls to the public internet over secure tunnels. Install it and run ngrok on the command line, specifying the port that hosts the application on which you want to receive messages:

./ngrok http [portnum]

Ngrok will display a forwarding link that you can use to access your local server using the public network.

ngrok

Now you can create an application to receive SMS messages (follow our Quickstart guide for details).

Conclusion

And that’s all there is to sending and receiving SMS messages using Plivo’s Python SDK. Don’t use Python? Don’t worry — we have SDKs for PHP, Java, Node.js, Ruby, .NET Core, .NET Framework, and Go. (Sorry, COBOL fans.)

Haven’t tried Plivo yet? Getting started is easy and only takes 5 minutes! Sign up today.

comments powered by Disqus