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:
    Copy
    Copied
    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:

Copy
Copied
py -3 -m venv venv

Activate the environment using the following command:

Copy
Copied
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:
Copy
Copied
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.

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 Access Keys page of the Customer 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:
    Copy
    Copied
    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:
    Copy
    Copied
    npm install ngrok -g
  3. In the terminal or command prompt, enter:
    Copy
    Copied
    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 Customer 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 step.

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

We'd love to hear from you!
Rate this content:
Still have a question?
 
Ask the community.