DEV Community

loading...
Cover image for Implementing Bulk SMS Application  with Django, Twilio, and Fauna

Implementing Bulk SMS Application with Django, Twilio, and Fauna

aidelojep
I am interested in people and technology.
・9 min read

This article focuses on implementing Bulk SMS API in a web application built with Python framework (Django). We used Twilio to generate the Bulk SMS, and Fauna as the database to save customer information. The value of bulk SMS in marketing campaigns and business cannot be overemphasized. Bulk SMS API’s can be a very reliable means for most companies to reach out to its large pool of customers about a new product, for a new campaign, or to keep them updated (major or minor changes made to) an already existing service/product.

This article will include steps on how to integrate Fauna with your SMS API system. You could also visit Fauna documentation to find out more about systems that are supported.

Brief Introduction to Serverless database

Serverless architecture refers to a system that affords users greater scalability, more flexibility, and quicker time to release, all at a reduced cost. Most developers prefer to use serverless architectures because of its broad range of advantages like:

  1. Freeing up developer resources and affords them the time to concentrate on projects that directly affects the business value as against spending that time on system maintenance
  2. Not having to worry about purchasing and managing traditional backend servers.
  3. It significantly results in a reduction in server cost, because you only pay as you go.

Fauna leads the serverless database as it is amongst the pioneering serverless databases and also the choice for many developers.

Setting Up Your Fauna Database

Create the Fauna Database

STEP 1: FAUNA SETUP

The first thing to do is create the database for our SMS API project in the Fauna dashboard. If you are new to Fauna and yet to create an account, you will be required to do so via the link attached here: Fauna Account Signup

In the Fauna dashboard, click on the “NEW DATABASE” button, provide a database name and click on the “SAVE” button.
Alt Text
Alt Text
Alt Text

Step 2: Collection indexes

To create a collection index, you would need to navigate the collections tab on the Fauna sidebar, then click on the NEW “COLLECTION” button. Next, enter a name for the collection and click on the “SAVE” button. You can decide to rename the collection to any name of your choice. In this article, we named our collection ‘user’.
Alt Text
Alt Text

Step 3: Creating a Fauna API key

To create a Fauna API key, you would have to navigate to security settings on the Fauna sidebar (located at the top left side of the screen). The Fauna API key helps to connect the database to our SMS API app.
Alt Text
Alt Text
Alt Text

The API keys generated from Fauna are secret keys. It is advisable to copy these keys safely and store them somewhere safe that you can easily retrieve back.

Step 4: Integrating Fauna into Django project

Here, we can now attempt to integrate our Python Library with Fauna from the pip. This can also be installed with a single line on the terminal.
Alt Text
After completing the installation of Django and Fauna, the next step is to run a sample code that is in Fauna Python driver documentations: Fauna python driver documentation

Alt Text
We can see from the code above how the Fauna driver connects with the database that generates its API keys.
Let us consider the steps to follow to build our SMS API application in the following subheading.

BUILDING OUR SMS API:

STEPS TO BUILD THE SMS API IN DJANGO FRAMEWORK

The Project (Bulk SMS APP)

This section will guide you to build a simple Bulk SMS sending web application using the Django web framework and implement the database with Fauna. This Bulk SMS app would allow admin users to add a customer and his phone number to the customers' list and select who to send already customized SMS.

Prerequisites

To fully understand this part of the tutorial, you are required to have the following in place:
Python 3.8 or newer and Django 3.0 or newer (if you don’t have one, don’t worry I will walk you through on how to install Python and Django)
Basic understanding of Fauna
Basic understanding of Django
A text editor e.g. PyCharm, Vscode

Install python

To check if python is successfully installed on your system, run the command “python --version” if python is installed, you will get a response as shown below with the version of python installed. If you don’t have python installed go to https://www.python.org/downloads/ select your operating system, and download.

Alt Text

Creating a virtual environment

Before we start, we need to create an isolated virtual environment to not ship our entire system file and dependencies during deployment.This means that the dependencies of other projects will not be included in the dependencies of our current project even though they are all on the same local machine. To do that, we are going to type the following command. Note the name of the virtual environment is “env” and it's just a variable name that can be named anything.
Alt Text
Next, activate the virtual environment by changing the directory to the virtual environment we just created with the name “env,” go into the Scripts folder and run the executable file “activate.”
Alt Text

INSTALL DJANGO

For this project, we are going to use Django, a python web framework built to meet deadlines, while satisfying the tough software requirements.
Django makes building web applications easy, it helps you build an entire Web application from scratch in a matter of hours. With Django you don’t have to struggle with the fuss of web development. It helps you focus on your business logic without needing to reinvent the wheel. It’s free and open source.
To install Django we are using a python package manager by simply running the command “pip install Django.” If Django is installed successfully, you will get the following output:
Alt Text

create a project (sms project)

Next, we will create our project using the command “Django-admin startproject “ command then the name of the project as shown below. Here the name of our project is "smsproject" and it’s just a variable name, you can name it whatever you like, but make sure it is descriptive.
Alt Text

Creating an app

Next, we create an app with the “python manage.py startapp [name of your app]” command. The name of the app for this tutorial is sms app. Again feel free to name it anything of your choice but should be self-descriptive. This helps other developers easily understand what you are working on. Make sure you change the directory to the sms project we created.
Alt Text
Now that we have successfully created our app, we need to add our app to the installed apps. Go to settings.py and add this: “smsapp.app.SmsappConfig”

smsproject/settings.py

Let us run our app to see if we have successfully installed and correctly set up our project. To run, you have to change the directory into your project and run the command “python manage.py runserver.”
Alt Text
As you can see, we have started our development server at http://127.0.01:8000/.

Open your browser and try to access this URL. If everything works fine, you will see the screen below
Alt Text
Next, we install the python phone number fields package to help us hold the phone numbers. The command is: “pip install django-phonenumber-field[phone numbers].” If the installation works, you will get the following output:
Alt Text
Having installed the Django phone number field, we need to add it to the list of our installed apps, and we do that in the settings.py as follows:

Smsproject/settings.py
Alt Text
Next, we will create a model in models.py to store the name and phone number of a customer.

smsapp/models.py
Alt Text

Migrations

In Django, we use migrations to move changes you make to your models (adding a field, deleting a model, etc.) into your database schema. We need to make migrations to our database.
Alt Text
Run the command “python manage.py makemigrations.” Makemigrations are used to create new migrations based on the changes made to the models in a project. The output is as shown below:
Alt Text
Next, we migrate with python manage.py migrate. Migrate is responsible for applying and reapplying migrations.
Alt Text
Next, we need to register our model in our admin:

smsapp/admin.py
Alt Text
Create an admin(a superuser) that will compose and send messages to the customer.
Let's run our server. Go to localhost:8000/admin
Alt Text
Then log in with our username and password. If you log in successfully, you see this:
Alt Text
Add a customer via the admin panel. Unlike the example below, enter a valid phone number so you can verify the message we will send.

name: Peter
phone number: +2347033622204

Twilio Integration

Twilio is a platform as a service company (PaaS) that provides Cloud communications services. With their services, software engineers can build applications that can make and receive calls and send and receive text messages. Their API makes it easy for software engineers to automate and schedule text messages to remind users on their platform of upcoming events etc.
The next thing is for us to install Twilio using the command “pip install Twilio”. If the installation works, you will get the output below:
Alt Text
Go to www.twilio.com to create a free Twilio account, get your SID and Auth_token, and then click on get a trial phone number, as shown on the screen below.
Alt Text
Add the Twilio account SID and Auth token from your Twilio dashboard to the settings.py of your project.

settings.py
Alt Text
For this project, I want to authorize only the admin to send SMS to customers on the platform, so I will be creating the function that will enable us to send SMS in the admin.py instead of the views.py. In the Django admin panel, we will select which customers we want and then choose "Send text campaign" from the dropdown menu. To send a text message, we instantiate a client object with our account ID and authorization token. Then, for each customer selected in the admin, we create a message by specifying the "to" phone number, using our trial number as the "from" number, and finally inserting our message as the body.

smsapp/admin.py

Next, we rerun our server with the command python manage.py run server. Then go to localhost:8000/admin and login into the admin dashboard. Send the customer’s name you want to send SMS to from the action dropdown select send text campaign then click go. Wao you just send SMS to customers on your platform. You can use the add customer button to add more customers and their phone numbers send them messages at a go. Note the phone numbers must be registered on Twilio if you are using a trial account just and we did in this tutorial. If you want to send unverified phone numbers, you will need to upgrade your Twilio account.
Alt Text

CONCLUSION

In this article, we built a Bulk SMS application with Django (Python framework) integrated in Fauna. Fauna serves as the storage point for customer’s information. Then we implemented the SMS API with Twilio to generate SMS to our customers.
If you have any questions, don't hesitate to contact me on my socials:

My LinkedIn handle My twitter handle

Discussion (0)