Before you can get started, you need the following already set up:
Set all SMS API configuration settings.
- Python and a familiarity with how to create a new app.
- PIP (package installer for Python) and a familiarity with how to install Python modules.
- Flask and a familiarity with how to set up a Flask environment and app.
- ngrok. You'll use ngrok to open a tunnel to your local server.
Learn how to handle incoming SMS messages in a Python application with the Sinch Python SDK.
The easiest way to install the SDK is using pip
:
- Open a command prompt or terminal to the local repository folder.
- Execute the following command:
pip install sinch
Now you can start setting up your environment.
Create a new folder where you want your app project and open a command prompt to that location. Create a new environment with the following command:
py -3 -m venv venv
Activate the environment using the following command:
venv/Scripts/activate
We will be using Flask to create a lightweight webserver that will listen for requests from the Sinch servers to handle incoming calls. Additionally, we'll be using the requests
module to make HTTP requests.
Use the following commands to install the Flask
and requests
modules:
pip install Flask
pip install requests
Create a new file named app.py
and paste the provided "handle-incoming.py" code into the file.
import os
import sinch
from flask import Flask, request
app = Flask(__name__)
sinch_client = sinch.SinchClient(
key_id="YOUR_KEY_ID",
key_secret="YOUR_KEY_SECRET",
project_id="YOUR_PROJECT_ID"
)
@app.route('/', methods=['POST'])
def result():
inbound_message = request.get_json()
print(inbound_message)
if all(key in inbound_message for key in ["body", "to", "from"]):
sinch_client.sms.batches.send(
body="Thank you for using the Sinch SDK. You sent: " + inbound_message["body"],
delivery_report="none",
to=[inbound_message["from"]],
from_=inbound_message["to"]
)
return "Inbound message received", 200
else:
return "Invalid data", 400
The code provided includes placeholder parameters. You'll need to update the parameters detailed in the following subsections with your values.
Before initializing a client using this SDK, you'll need three pieces of information:
- Your Project ID
- An access key ID
- An access key Secret
These values can be found on the <b>Access Keys</b> page of the Sinch Build Dashboard. You can also create new access key IDs and Secrets, if required.
If you have trouble accessing the above link, ensure that you have gained access to the Conversation API by accepting the corresponding terms and conditions.
Ensure that you save the file.
Start the server by executing the following command:
flask run
By default, your web server is started on port 5000.
Now you need to open a tunnel to the server you just set up. We are using ngrok for this. If you don't have ngrok installed already you can install it with the following command:
npm install ngrok -g
In the terminal or command prompt, enter:
ngrok http 5000
You will see a screen like the following.
On the highlighed "Forwarding" line, copy the address ending in
.ngrok.io
.
Next, configure a Callback URL for your Sinch account.
- On your Sinch Build Dashboard-> Service APIs click on the service plan ID link.
- In “Callback URL” click Add Callback URL and paste in the HTTPS address referred to in the previous section.
Now that your server is running and your webhook is configured, you can test the application.
From your messaging platform, send your SMS app a message. You will receive a message back in response on your mobile handset.
- Visit our API specification to test more endpoints.