This guide shows how to set up a development environment in five minutes to trigger API requests related to our Messaging API. It also shows how to send and receive messages using tunneling software to expose the local dev server to the public internet.
To get started, install Python, the Flask web framework, and Plivo’s Python SDK.
Linux and macOS users should already have Python installed. Windows users can download and install it. Follow instructions to install Flask.
To install the Plivo Python SDK, first create a project directory using the command mkdir mypythonapp
, then change to the directory and install the SDK using pip:
pip install plivo
Alternatively, you can download the source code from our GitHub repo and run
python setup.py install
We recommend that you use virtualenv to manage and segregate your Python environments, instead of using sudo
with your commands and overwriting dependencies.
Now you can create a file in the project directory and execute code to trigger any Plivo API. Here’s some example code that sends an SMS message. Create a file called SendSMS.py and paste into it this code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from flask import Flask, Response
from plivo import plivo
app = Flask(__name__)
@app.route('/send_sms/', methods=['GET', 'POST'])
def outbound_sms():
client = plivo.RestClient('<auth_id>','<auth_token>')
response = client.messages.create(
src='<sender_id>',
dst='<destination_number>',
text='Hello, from Flask!')
return Response(response.to_string())
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from flask import Flask, Response
from plivo import plivo
app = Flask(__name__)
@app.route('/send_mms/', methods=['GET', 'POST'])
def outbound_mms():
client = plivo.RestClient('<auth_id>','<auth_token>')
response = client.messages.create(
src='<sender_id>',
dst='<destination_number>',
media_ids=['801c2056-33ab-499c-80ef-58b574a462a2'],
text='Hello, MMS from Flask!',
media_urls=['https://media.giphy.com/media/26gscSULUcfKU7dHq/source.gif'],
type_='mms')
return Response(response.to_string())
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
Save the file and run it.
python SendSMS.py
You can follow the same approach to trigger other API requests. Refer to our detailed API reference to see all the API requests available on the Messaging API platform.
Now that we’ve sent a message, let’s set up a Flask server to handle incoming messages.
Plivo supports receiving SMS text messages in several countries (see complete SMS API coverage). When someone sends a text message to a Plivo phone number, you can receive it on your server by setting a Message URL in your Plivo application. Plivo sends the message, along with other parameters, to your Message URL.
Go to the directory where you want to write the code for your server and set up a virtual environment so that the packages you install won’t interfere with the system ones.
Use the command virtualenv env_name
to create a directory with the name env_name
along with the required binaries inside it. Activate the virtual environment with the command
source env_name/bin/activate
You should now see that your CLI is in the env_name
environment. The next time you run a pip install some_package
command it will install the software in this environment and not overwrite the system versions.
When you’re done with your development environment and want to return to the normal environment, deactivate it using the command deactivate
.
Use this code snippet to start a local server.
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 Received'
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from flask import Flask, request
app = Flask(__name__)
@app.route('/receive_mms/', methods=['GET', 'POST'])
def inbound_mms():
from_number = request.values.get('From')
to_number = request.values.get('To')
text = request.values.get('Text')
media_url = request.values.get('Media0')
print('Message received - From: %s, To: %s, Text: %s, Media: %s' %(from_number, to_number, text, media_url))
return 'Message Received'
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
python receive_sms.py
You should see your basic server app in action on http://localhost:5000/receive_sms/.
To serve XML documents, your local server must connect with Plivo API services. For that, we recommend using ngrok, which exposes local servers running behind NATs and firewalls to the public internet over secure tunnels. Using ngrok, you can set webhooks that can talk to the Plivo server.
Install ngrok and run it on the command line, specifying the port that hosts the application on which you want to receive messages (5000 in this case):
./ngrok http 5000
This will start the ngrok server on your local server. Ngrok will display a forwarding link that you can use as a webhook to access your local server over the public network. You should be able to see your basic server app in action on https://<nrgok_URL>/inbound/.
Now people can send messages to your Plivo number.
You can follow the same approach to serve other XML documents to manage call flows. Refer to our detailed XML reference to check all the XML elements available on the Messaging API platform.