How to receive SMS from Python flask
The simplest way to receive SMS from Python is to use the built in HTTP/Rest SMS api of Ozeki SMS Gateway. When you use this API, you will receive SMS messages by issuing a HTTP GET request to the SMS gateway. The HTTP Post response will contain the messages formatted in JSON format. The SMS gateway will send the SMS messages to the python console application, and it will return a HTTP 200 OK response to your request.
Python code to receive sms
The Python sms code sample below demonstrates how you can receive SMS using the http rest sms api of Ozeki SMS Gateway using the Python ozekilibsrest library. This library is provided to you free of charge, and you may use it and modify it in any of your projects.
ReceiveSms.py
from flask import Flask, render_template, request from ozekilibsrest import Configuration, MessageApi app = Flask(__name__) configuration = Configuration( username="http_user", password="qwe123", api_url="http://127.0.0.1:9509/api" ) api = MessageApi(configuration) logs = [] @app.route('/', methods=['GET', 'POST']) def index(): if request.method == 'POST': messages = api.download_incoming() message_count_string = messages.__str__() logs.append(message_count_string) for message in messages.messages: message_string = message.__str__() logs.append(message_string) return render_template('ReceiveSms.html', logs=logs) if __name__ == '__main__': app.run()
ReceiveSms.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Receive SMS with Ozeki SMS Gateway</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> </head> <body> <form action="/" method="POST" class="form-group" style="width: 40%; margin-top: 10vh; margin-left: 30%; display: flex; flex-direction: column; height: 10vh; justify-content: space-evenly;"> <button class="btn btn-primary" style="font-weight: 600;" type="submit" name="submit" value="download">Download</button> </form> <ul style="width: 40%; margin-left: 30%; height: 70vh; overflow-y: scroll;" class="list-group card"> <li class="list-group-item card"><b>Logs:</b></li> {% if logs %} {% for log in logs %} <li class="list-group-item">{{ log }}</li> {% endfor %} {% endif %} </ul> </body> </html>
How to use the Python sms example:
This Python sms example can be used in any Python application. To use it, you must install the ozekilibsrest package with the pip install ozekilibsrest command. After the package is installed, you must put the from ozekilibsrest import Configuration, Message, MessageApi directive into the header section of your Python source code. This will allow you to use the classes provided by the ozekilibsrest library. You can use the Message class to create the SMS. You can use the MessageApi class to send the SMS to the SMS gateway. The SMS gateway will forward your message to the mobile network either through a wireless connection or through the Internet.
Download ReceiveSms.py
The source code explained in this article can be downloaded and used and modified free of charge.
Download: ReceiveSms.py.zip (1.40Kb)
What is in the ReceiveSms.py folder?
The ReceiveSms.py folder contains the script of the python sms example which will show you how to receive SMS messages using Python.
How to install the ozekilibsrest library
In order to install the ozekilibsrest library, you have to open the command prompt and use the following command. This will install the ozekilibsrest library, and the dependencies it needs.
pip install ozekilibsrest
How to install the flask library
In order to install the flask library, you have to open the command prompt and use the following command. This will install the flask library, and the dependencies it needs.
pip install flask
How to receive sms from Python (Quick steps)
To receive sms from Python:
- Download and install Python
- Install the ozekilibsrest library using pip or codna
- Install the flask library using pip or codna
- Download the ReceiveSms.py.zip file
- Extract the .zip file from Downloads folder
- Open the ReceiveSms.py file in any text editor
- Launch Ozeki SMS Gateway
- Create a HTTP API user in Ozeki
- Run ReceiveSms.py code using the command prompt to send the test SMS messages
- Check the Sent box in Ozeki SMS Gateway
Install Ozeki SMS Gateway and create an HTTP API user
To be able to send SMS from Python flask, first you need to install Ozeki SMS Gateway. The SMS gateway can be installed on the same computer, where you develop your Python code. After installation, the next step is to connect Ozeki SMS Gateway to the mobile network. You can send a test sms from the Ozeki GUI to verify, that your mobile network connection works. The final step to prepare your environment is to create a HTTP sms api user. Create a user with a username of "http_user", and with a password of "qwe123" to make the example work without modification.
After the environment is setup, you can run your Python code.
HTTP API url to use send sms from Python flask
To send SMS from Python flask, your Python code will have to issue an HTTP request to the SMS gateway. The API url is shown below. Note that the IP address (127.0.0.1) should be replaced to the IP address of your SMS gateway. If Ozeki SMS Gateway is installed on the same computer where the Python flask sms application is running, this can be 127.0.0.1. If it is installed on a different computer, it should be the IP address of that computer.
http://127.0.0.1:9509/api?action=rest
HTTP authentication to use send sms from Python flask
To authenticate the Python flask sms client, you need to send the username and password in a base64 encoded string to the server in a HTTP request. The format used is: base64(username+":"+password). In Python you can use the following code to do this encoding:
def create_authorization_header(username, password): username_password = f'{ username }:{ password }' return f'Basic { b64encode(username_password.encode()).decode() }'
For example, if you encode the username 'http_user' and the password 'qwe123', you will get the following base64 encoded string: aHR0cF91c2VyOnF3ZTEyMw==.
HTTP request header to send SMS from Python flask
To send the SMS messages, you need to include the following lines as headers in the HTTP request. Note that we include a content type and an Authorization header.
Content-Type: application/json Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
HTTP request to send SMS from Python flask
To submit the SMS messages, your Python flask application will send an HTTP request similar to the one below. Note, that this request contains a HTTP header part, in the header we have defined the folder we want to download the messages from, and the action. We have also sent the Base64 encoded password and username to authenticate the request.
GET /api?action=receivemsg&folder=inbox HTTP/1.1 Host: 127.0.0.1:9509 User-Agent: python-requests/2.26.0 Accept-Encoding: gzip, deflate Accept: */* Connection: keep-alive Authorization: Basic aHR0cF91c2VyOnF3ZTEyMw==
HTTP response received by the Python flask sms example
Once the SMS gateway receives this request, it will generate a HTTP response. The HTTP response will contain a status code, to indicate whether the SMS submit request was successful or not. It will also return a JSON encoded structure to provide you useful details about the message's submission.
HTTP/1.1 200 OK Transfer-Encoding: chunked Content-Type: application/json; charset=utf8 Last-Modified: Fri, 17 Sep 2021 08:36:07 GMT Server: 10/10.3.124 Microsoft-HTTPAPI/2.0 User-Agent: OZEKI 10.3.124 (www.myozeki.com) Access-Control-Allow-Origin: * Date: Fri, 17 Sep 2021 07:08:11 GMT { "http_code": 200, "response_code": "SUCCESS", "response_msg": "", "data": { "folder": "inbox", "limit": "1000", "data": [ { "message_id": "31bc2fe0-c2a6-4ebe-afcd-6bcd9ea341fb", "from_connection": "admin@localhost", "from_address": "", "from_station": "%", "to_connection": "http_user@localhost", "to_address": "http_user", "to_station": "%", "text": "Hello world 1!", "create_date": "2021-09-17 09:07:14", "valid_until": "2021-09-24 09:07:14", "time_to_send": "2021-09-17 09:07:14", "submit_report_requested": true, "delivery_report_requested": false, "view_report_requested": false, "tags": [] }, { "message_id": "b7b8d592-e182-4e96-a375-b4a6aa73e483", "from_connection": "admin@localhost", "from_address": "", "from_station": "%", "to_connection": "http_user@localhost", "to_address": "http_user", "to_station": "%", "text": "Hello world 2!", "create_date": "2021-09-17 09:07:23", "valid_until": "2021-09-24 09:07:23", "time_to_send": "2021-09-17 09:07:23", "submit_report_requested": true, "delivery_report_requested": false, "view_report_requested": false, "tags": [] }, { "message_id": "b0308970-a3c9-45c4-bf97-14a914a66f57", "from_connection": "admin@localhost", "from_address": "", "from_station": "%", "to_connection": "http_user@localhost", "to_address": "http_user", "to_station": "%", "text": "Hello world 3!", "create_date": "2021-09-17 09:07:33", "valid_until": "2021-09-24 09:07:33", "time_to_send": "2021-09-17 09:07:33", "submit_report_requested": true, "delivery_report_requested": false, "view_report_requested": false, "tags": [] } ] } }
Python flask sms example: ReceiveSms.py
How to download the ReceiveSMS.py project (Video tutorial)
In the following video, you will learn about how you can use the ReceiveSms.py Python flask project. The video will start with the download page and will take you all the way to the opened code editor panel. You will learn how to download and open the code file. The video is only 54-seconds-long, but it has all the necessary information you need to successfully use the example code file.
The example code below is part of the ReceiveSms.py.zip folder.
How to use the project (Video tutorial)
In the following clip, you will see how you can launch the ReceiveSms.py Python code. The video will start with the opened code and will take you all the way to the events tab with the log of the sent message. You will see how to launch the code and what happened if the code was executed. The video is a little shorter than 1 minute, but it contains all the information that is necessary to run the project.
Conclusion
This article showed the steps of receiving SMS in one request from Python flask.
With this knowledge and the provided tools, you should be able to reach out to
several customers with one single code. Ozeki SMS Gateway has an important
part in this process because this program organizes the message delivery.
Ozeki SMS Gateway works with high quality and performance, allowing you to
send up to 1000 SMS per second.
Continue your learning in Ozeki's tutorial pages, where you can read about
topics like scheduling SMS and
deleting SMS in Python flask.
Now your only thing to do is to download Ozeki
SMS Gateway and start working!
More information
- Python flask send SMS with the HTTP rest API (code sample)
- Python flask send multiple SMS with the HTTP rest API (code sample)
- Python flask schedule SMS with the HTTP rest API (code sample)
- Python flask receive SMS with the HTTP rest API (code sample)
- Python flask delete SMS with the HTTP rest API (code sample)
- How to download the latest Python flask SMS library from Github