How to View Twilio SMS Notifications on Your Desktop with Python and FastAPI

January 25, 2023
Written by
Mia Adjei
Twilion
Reviewed by

How to View Twilio SMS Notifications on Your Desktop with Python and FastAPI

If you are building a project with Twilio SMS, there are times when it would be convenient to see the messages that come in on your Twilio number as desktop notifications. For example, if you are tracking a package or a food delivery, you may want to see these notifications appear without having to look away from your device.

Notification appears on MacOS

In this short tutorial, you will learn a quick way to use Python and FastAPI to create a tiny app that will display the messages you receive to your Twilio number as notifications on your desktop — in real time.

If you are looking for a quick and efficient way to manage your SMS notifications, then this tutorial is for you.

Let's get started!

Prerequisites

Set up a virtual environment and install dependencies

To start, open a new terminal window and navigate to where you would like to set up your project. Once you have done this, run the following commands to set up a new directory called desktop_notifications and change into the directory:

mkdir desktop_notifications
cd desktop_notifications

Next, you'll need to create and activate a Python virtual environment. There, you can install the dependencies for this project. If you are working on a macOS or UNIX machine, you can do this by running the following commands:

python3 -m venv venv
source venv/bin/activate

However, if you are working on Windows, run these commands instead:

python -m venv venv
venv\Scripts\activate

For this project, you will be using the following Python modules:

Run the following command in your terminal to install these modules in your virtual environment:

pip install fastapi uvicorn notify-py python-multipart

Now you're ready to start building your FastAPI application.

Build the FastAPI application

To build this tiny application, you'll only need to create one file: main.py. Create and open this new file now, at the root of your project. Then, add the following code to the file:

from fastapi import FastAPI, Form
from notifypy import Notify

app = FastAPI()

# When an SMS is received on your Twilio number, show a notification on your desktop.
@app.post('/notify')
async def notify(From: str = Form(...), Body: str = Form(...)):
    notification = Notify()
    notification.application_name = 'Message Received 🎉'
    notification.title = f'SMS from {From}'
    notification.message = Body
    return notification.send()

In the code above, you create a new web application with FastAPI, and then set up a /notify route that will receive POST requests to the /notify endpoint. When a POST request is made to the /notify endpoint, your application will call the notify() function, which creates a new notification using notify-py, sets that notification's name, title, and message, and then sends that notification to appear on your desktop. The phone number that the SMS was sent from (From) will appear in the notification's title, while the Body of the received SMS will become the body of the notification.

Run the server and start ngrok

To start your FastAPI application, run the following command from the terminal window within the activated virtual environment:

uvicorn main:app --reload

Once the application is running, you will see log statements like the following printed to the terminal, letting you know that the server is running on port 8000, and all is working as expected:

INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [66521] using StatReload
INFO:     Started server process [66523]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

Now that the server is running, it's time to run ngrok as well. In a separate terminal tab, start ngrok by running the following command:

ngrok http 8000

The log output from ngrok will be similar to the following:

ngrok                                                                                   (Ctrl+C to quit)

Session Status                online
Account                       [YOUR NAME] (Plan: Free)
Version                       3.1.1
Region                        United States (us)
Latency                       -
Web Interface                 http://127.0.0.1:4040
Forwarding                    https://XXXX-XXX-XX-XXX-XX.ngrok.io -> http://localhost:8000

Connections                   ttl     opn     rt1     rt5     p50     p90
                              4       0       0.00    0.00    5.49    5.71

Take note of the URL next to Forwarding, ending with .ngrok.io. You will need this to configure the webhook on your Twilio phone number.

Set up the webhook with ngrok

Navigate to the Phone Numbers section of the Twilio Console. There, select the phone number you would like to use, and then scroll down to the Messaging section.

Copy the forwarding URL from ngrok and paste it into the Webhook field under A MESSAGE COMES IN, adding /notify to the end of this URL. The full URL should look something like this: https://XXXX-XXX-XX-XXX-XX.ngrok.io/notify. Make sure the request method is set to HTTP POST.

Configure webhook in Twilio Console

Click the Save button to save your changes. Now, every time someone sends an SMS to your Twilio number, your notifications application will receive a request to the /notify endpoint and show you a desktop notification with the details about the SMS message you received.

Test it out

It's time to test the application. Try sending a message from your phone to your Twilio number. Maybe something like Your package has been delivered! 📦.

You will see the notification appear on your desktop!

Notification appears on MacOS

What's next for desktop notifications?

In this tutorial, you learned how to create a tiny application using Python and FastAPI, one that lets you see the SMS messages received to your Twilio number as desktop notifications.

As a next step, you could customize the notification further, perhaps changing its icon or making it play a sound when the notification appears. This project could also be a great jumping-off point for a fuller package-tracking app, or even for an application that sends SMS in addition to just reading them.

I can't wait to see what you build!

Mia Adjei is a Software Engineer for Technical Content at Twilio. They love to help developers build out new project ideas and discover aha moments. Mia can be reached at madjei [at] twilio.com.