The importance of SMS messaging to businesses can not be overemphasized. From sending a promotional message or an SMS alert about a change of policy, to sending one-time passwords to authorize transactions, there is so much you can do with SMS messaging.
This article teaches you how to leverage the
Twilio SMS API
to build a Bulk SMS application with
Django
. In this tutorial, you will build a Bulk SMS app that allows admin users to send messages to customers as SMS. With this app, you will see the power of the Twilio SMS API using the Django framework.
In this article, you will learn:
-
How to integrate the Twilio SMS API into a Django project
-
How to send SMS messages using the Twilio Programmable SMS API
-
How to use
Redis Queue
for queuing of long-running jobs.
You will need the following in order to fully follow along with this tutorial:
Django is a Python framework; as such, you need to have Python installed and use it as your development language. To confirm that you have Python installed, run the following command:
If Python is not installed, visit
Download Python
to install it based on your computer's operating system.
Make sure
pip
(Package Installer for
Python
) is also set up on your computer.
pip
is used for the installation of third-party dependencies. You can verify the installation by running the following command:
This project makes use of the free and open-source Django framework, a Python web framework designed to fulfill deadlines and adhere to stringent software specifications.
Building web apps is fast with Django; you can create a complete web application from the ground up within a few hours. Thanks to Django, you can avoid many of the stressors that come along with web development, and instead can focus on writing your business logic.
Before installing Django, create a
virtual environment
. A virtual environment is an isolated Python environment on your computer. This makes it more likely that, although being dependent on the same local machine, your current project's dependencies won't also include those of other projects.
Run the following command to create a new virtual environment:
A Django project is a website or application built using the Django framework. It consists of smaller submodules (apps) and configurations. A project can contain multiple apps, depending on the complexity of the website. A Django app is a self-contained set of models and views that can be plugged into a Django project. A project is an entire website, while an app is a small part of the project.
Create a new project called
SMSserviceproject
by running the following command:
Twilio is a platform as a service (PaaS) company that offers Cloud communication services. Software engineers can use their services to create applications that can make and receive phone calls as well as send and receive text messages. Their API allows software engineers to easily automate and schedule text messages to remind users on their platform of upcoming events.
Now that you have installed the Twilio library in the project, you will need to get your Twilio credentials.
Log in to the
Twilio Console
. On the dashboard, you will find your Account SID, Auth Token, and Twilio phone number. Take note of these values, as you will need them to configure your Django project:
TWILIO_ACCOUNT_SID = env("MY_ACCOUNT_SID")
TWILIO_AUTH_TOKEN = env("TWILIO_AUTH_TOKEN")
TWILIO_NUMBER = env("MY_TWILIO_NUMBER")
SMS_BROADCAST_TO_NUMBERS = [
"+23XXXXXXXXXXX",
"+23XXXXXXXXXXX", # use the format +1XXXXXXXXXX
from django.shortcuts import render
from twilio.rest import Client
from django.conf import settings
from django.http import HttpResponse
# Create your views here.
def broadcast_sms(request):
message_to_broadcast = ("How are you")
client = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
for recipient in settings.SMS_BROADCAST_TO_NUMBERS:
print(recipient)
if recipient:
client.messages.create(to=recipient,
from_=settings.TWILIO_NUMBER,
body=message_to_broadcast)
return HttpResponse("messages sent!" + message_to_broadcast, 200)
Here you define a function called broadcast_sms
to send the SMS. This function declares the message and uses the Twilio client to create a new message, passing in the recipient numbers, the message body, and the Twilio phone number.
Next, create a new file in the SMSapp directory called urls.py. In this file, paste the following code:
While the above code works and is fine for sending a small number of messages, but you'll hit rate limits as you increase the number of messages. To solve this problem, you need a task queue to queue sending the messages. Task queuing is necessary when dealing with long-running jobs that would dramatically reduce the performance of an HTTP response. This resource will help you to learn more about task queuing. In this tutorial, you will use Redis Queue for queuing your jobs.
Run the commands below to install Redis on your computer:
wget http://download.redis.io/releases/redis-7.0.9.tar.gz
tar xzf redis-7.0.9.tar.gz
cd redis-7.0.9
from django.shortcuts import render
from twilio.rest import Client
from django.conf import settings
from django.http import HttpResponse
from datetime import datetime, timedelta
from redis import Redis
from rq import Queue
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'SMSserviceproject.settings'
q = Queue(connection=Redis())
# Create your views here.
def send_sms(from_, to, body):
client = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
client.messages.create(from_=from_, to=to, body=body)
def broadcast_sms(request):
message_to_broadcast = ("How are you")
delta = 0
for recipient in settings.SMS_BROADCAST_TO_NUMBERS:
print(recipient)
if recipient:
q.enqueue_in(timedelta(seconds=delta), send_sms, settings.TWILIO_NUMBER, recipient, message_to_broadcast)
delta += 15
return HttpResponse("messages sent!" + message_to_broadcast, 200)
In the above code, you have updated the views.py file to include Redis Queue. Here, you created an instance of a queue to track the jobs to be done and invoked the enqueue_in
function to schedule a message to be sent out every 15 seconds.
To test the queuing capabilities, run the Redis server while in the redis-7.0.9 folder with the following command:
In this tutorial, you have been able to build a simple bulk SMS application with the power of the Twilio SMS API. You also used Redis Queue to queue the messages, avoiding the rate limits and 429 error responses for making too many concurrent requests to the Twilio API. The entire code base can be found in this GitHub repository. If you would like to continue developing this project, you can upgrade your account to be able to send SMS to any phone number, as a trial account works with only verified numbers.
Eme Anya Lekwa is a Software Engineer. He is passionate about how technology can be employed to ensure good governance and citizen political engagement. He is a mobile and web developer and a technical writer. He can be reached on LinkedIn.