Once the Message API request is successful, your SMS will be put into a queue to be sent to its destination. By default, you will not receive an automatic notification regarding the delivery of your SMS. To get notified on the status of your message, include the
url parameter in your API request. You can also find your SMS delivery reports in the SMS Logs of your Plivo account. This will automatically output a notification when your SMS reaches its destination, or if it fails to deliver. Your delivery report will update to show either
"failed" for each individual recipient.
In this section, we will guide you to build an app to Receive Delivery Reports.
Plivo Phone Number(Optional): To send messages to the United States and Canada, you must have a Plivo phone number that supports SMS. Numbers can be purchased from the Numbers section of your Plivo Console and use the same as the source number/from number for the outbound SMS. This number will also help you receive incoming SMS as you must have a SMS-enabled Plivo phone number to do the same. Please note that you can also purchase numbers using the Numbers API.
Install Python and the Plivo Python Server SDK
You must set up and install Python and Plivo’s Python SDK to Receive Delivery Reports. Here’s how.
|macOS & Linux||To see if you already have Python installed, run the command python --version in the terminal|
|Windows||To install Python on Windows follow the instructions listed here.|
Install Plivo & Flask Python Package
Create a project directory, run the following command:
$ mkdir mypythonapp
Change the directory to our project directory in the command line:
$ cd mypythonapp
Install the SDK using pip
pip install plivo pip install flask
Alternatively, you can download the source code from this repo and run
python setup.py install
For features in beta, use the beta branch:
pip install plivo==4.2.0b1
Alternatively, you can download the source code from this repo(beta branch) 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.
Below is the code to handle delivery reports.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 from flask import Flask, request, make_response, Response app = Flask(__name__) @app.route('/delivery_report/', methods=['GET', 'POST']) def inbound_sms(): # Sender's phone number from_number = request.values.get('From') # Receiver's phone number - Plivo number to_number = request.values.get('To') # The text which was received text = request.values.get('Text') # Message UUID uuid = request.values.get('MessageUUID') # Print the message print('Message received - From: %s, To: %s, Text: %s, MessageUUID: %s' %(from_number, to_number, text, uuid)) return "Delivery status reported" if __name__ == '__main__': app.run(host='0.0.0.0', debug=True)
Exposing your local server to the internet
To receive Incoming Messages and to handle callbacks, your local server should be able to connect with Plivo API service, Ngrok is a tunneling software used to expose a web server running on your local machine to the internet. Using Ngrok you can set webhooks which can talk to Plivo server.
You can download and install ngrok from here. Follow the detailed configuration instructions to get started.
Run ngrok on the port which currently hosts your application. For example, if your port number is 80, run the following command:
./ngrok http <port_on_which_your_local_server_is_running>
This will give you a UI with links that look like
ngrok.io/* which you can use to access your local server using the public network.
Create an Application
- Create an Application by visiting the Application Page and click on
New Application. You can also use Plivo’s Application API.
- Give your application a name. Let’s call it
Receive SMS. Enter your server URL (e.g.
http://example.com/delivery_reports/) in the
Message URLfield and set the method as
POST. See our Application API docs to learn how to modify your application through our APIs.
- Click on
Create Applicationto save your application.
Assign a Plivo number to your app
- Navigate to the Numbers page and select the phone number you want to use for this app.
Receive SMS(name of the app) from the Plivo App dropdown list.
- Click on
Test and validate
Send an SMS to your Plivo number using a regular mobile phone. Plivo will send a request to your
Message URL with the parameters listed in the Messages Documentation.