Building a Bulk SMS Service with Python, Flask and Twilio Notify
Time to read: 5 minutes
Very often, large companies use SMS to communicate or send broadcast messages to their users. This is because SMS is reliable and usually has more reach and engagement than most other forms of bulk messaging. In addition, it enables companies to communicate with users who do not have smartphones.
In this article, we are going to use the Twilio Notify service, along with Python and the Flask framework to build a Bulk SMS service.
User journey
The user creates a message, inputs the phone numbers of the recipients of the message, and then sends them an SMS as shown in the video GIF below.
Tutorial requirements
To follow this tutorial, you are expected to:
- Have sufficient understanding of Python and Flask.
- Have Python 3 installed on your machine
- Have a Twilio Account. If you are new to Twilio, create a free account now.
Setting up your Twilio account
To follow this tutorial, you need your Twilio Account SID, Auth Token and Twilio phone number. You can get the Account SID and Auth Token here:
Twilio Notify
Twilio Notify enables you to send notifications to any number of recipients with one API request. To use Twilio Notify, you must first create a Messaging Service. From the Twilio console go to the messaging service section under Programmable SMS and create a new messaging service. Give the messaging service any name of your choice. For the use case section, choose “Notifications, Outbound only”.
Next, select “Services” in the Notify section of the Twilio console. Click on the red plus button and choose a name for your Notify service.
Now select the messaging service your created earlier in the “Messaging Service SID” dropdown:
Press the “Save” button at the bottom to apply this change.
Take note of the “SERVICE SID” that appears below the friendly name, as we are going to use it later to send our SMS.
Buying a Twilio phone number
Now you need to purchase a Twilio phone number for your app here on your Twilio console. Select your country from the dropdown and search for available numbers. If you wish to get a number containing specific digits, you could enter them in the field provided as shown below.
After searching, you will be shown a list of available numbers from which you can make your purchase.
To configure your messaging service to use the phone number you have purchased, go to your Twilio SMS service section on the console and click on the messaging service you created earlier. You’d see a screen like the one below:
Click on “Add an Existing Number” and select the phone number you purchased.
Application setup
As mentioned earlier, we are going to be using the Flask framework for this project.
Creating the application directory and virtual environment
Make sure you have Python 3 installed on your computer. Run the following commands in your terminal to create the project directory and virtual environment for this project.
- Create project directory named twilio_bulk_sms
- Enter into the project directory
- Create a virtual environment for the project
- Activate the virtual environment
For macOS and Linux users:
For windows users:
The virtual environment helps create an isolated environment to run our project.
Structuring the project
Next, we are going to set up the project’s directory structure. In the application directory, run the command below to create a directory where we are going to put utility functions our application will need:
Installing project dependencies
Finally, let’s install all the dependencies we are going to use in this project.
- Flask: This library will be used to run our web server.
- Twilio Python Helper Library: This library will be used to help send sms.
- Flask Cors - This flask extension handles Cross Origin Resource Sharing (CORS), making cross-origin AJAX requests possible.
- Python-dotenv - Reads the key-value pair from your .env
file and adds them as environment variables.
You may run the command below to install the above dependencies:
Building the API
In this section, we are going to build an API to send SMS messages in bulk to the supplied phone numbers. First of all, we are going to write the function to send SMS messages to users. Create a file called sms.py
in the utils
directory and paste the code snippet below in it.
In the snippet above, we set the ACCOUNT_SID
, AUTH_TOKEN
and the NOTIFY_SERVICE_SID
from environment variables.
The send_bulk_sms
function accepts the list of users we want to send the SMS to as a list of dictionaries, each having at least a phone
key. As a second argument the function takes the message body we want to send.
We use Python’s lambda
function with map()
to convert the list of users to the Twilio binding format. Each phone number is turned into a dictionary in the format shown below:
The dictionary is then converted into a string because the to_binding
param accepts a list of strings.
Next, we are going to write utility functions for validating our request body and sending API responses. Create a file called request.py
in the utils
folder and paste the following snippet inside:
Create a file called response.py
in utils
folder and paste the code below in it. The code below helps format the responses that we’d be sending back to the client.
Finally, we need to create a app.py
file in the main directory with the code snippet below:
The snippet above creates an endpoint with the /message
URL that accepts the message
and phones
parameters. It then validates them using the utility functions created above, and finally sends the message via Twilio to the supplied numbers using the send_bulk_sms
utility function.
Testing the API
To run the application, make sure you set the TWILIO_ACCOUNT_SID
, TWILIO_AUTH_TOKEN
and TWILIO_NOTIFY_SERVICE_SID
in a .env
file at the root of the project:
Then execute the command below in your terminal:
You should see something like the image below displayed in your terminal:
For the sake of testing this API, I have gone ahead to build a demo client application in React JS. The code for this application can be found here on GitHub. You may follow the steps below to set it up locally:
- Install Node JS on your machine following the instructions here.
- Clone the repository by executing the command below in a new terminal:
- Next, enter the project directory:
- Install the project’s dependencies by executing the command below:
- I have added a proxy to the project’s
package.json
to help us send all unknown requests to our API which is hosted on port5000
. - Now start up the application using the command below:
- Visit the URL below in your browser to access the web client:
http://localhost:3000/
Conclusion
We have now come to the end of the tutorial. We have successfully built a bulk SMS service with Python, Flask and Twilio. Make sure to explore the Twilio Programmable SMS API to see more ways it can be applied to build amazing software.
You will find the source code for this tutorial here on GitHub.
See you in the next one!✌🏿
Philip Obosi is a Software Engineer based in Lagos, Nigeria who loves to build scalable web applications with JavaScript and Python. He is passionate about web performance, data visualization and the blockchain.
- Email: philip.c.obosi@gmail.com
- GitHub: worldclassdev
- Twitter: worldclassdev
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.