Skip to content
Last updated

Receive an SMS Message with Python SDK

Note:

Before you can get started, you need the following already set up:

Learn how to handle incoming SMS messages in a Python application with the Sinch Python SDK.

Steps:
  1. Install the Python SDK
  2. Set up your environment
  3. Set up your Python application
  4. Configure your callback URL
  5. Test your application

Install the SDK

The easiest way to install the SDK is using pip:

  1. Open a command prompt or terminal to the local repository folder.
  2. Execute the following command:
    pip install sinch

Set up your environment

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

Install your dependencies

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

Set up your Python application

Create a new file named app.py and paste the provided "handle-incoming.py" code into the file.

handle-incoming.py
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

Modify your application

The code provided includes placeholder parameters. You'll need to update the parameters detailed in the following subsections with your values.

Initialize the client

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.

Note

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 your web server and set up a tunnel

  1. Start the server by executing the following command:

    flask run

    By default, your web server is started on port 5000.

  2. 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
  3. In the terminal or command prompt, enter:

    ngrok http 5000

    You will see a screen like the following. ngrok screenshot

  4. On the highlighed "Forwarding" line, copy the address ending in .ngrok.io.

Configure your Callback URL

Next, configure a Callback URL for your Sinch account.

  1. On your Sinch Build Dashboard-> Service APIs click on the service plan ID link.
  2. In “Callback URL” click Add Callback URL and paste in the HTTPS address referred to in the previous section.

Test the application

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.

Additional resources