Implementing Bulk SMS Application with Django, Twilio, and Fauna
Published: 2021-07-13 00:48:00

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] (https://pyauth.github.io/pyotp/) 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](https://fauna.com/) 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] (https://dashboard.fauna.com/accounts/register?utm_source=DevTo&utm_medium=referral&utm_campaign=WritewithFauna_SuggestiveApp_PAideloje)

In the Fauna dashboard, click on the “NEW DATABASE” button, provide a database name and click on the “SAVE” button.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1hvcs3uiufgi3ru4flgj.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lu9ho930dnilzrwmc7v2.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dfz3thnn3o5u8468qgio.PNG)
###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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t89u3q2zec7fogxa34ev.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6m72w8zz08ip9dy1eht2.PNG)
###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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/syjqzrnf7orzhrpgkz1c.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l1gsukry2pnlljx7y5ul.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hthjftmu0ikh933rf085.PNG)



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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fy9xvwo9ha3tok65vwkm.PNG)
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] (https://docs.fauna.com/fauna/current/drivers/python.html)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/onzw4d1it14qykid219q.PNG)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7y2040otrrgwb34i38f5.PNG)

###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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgidj6dqeitduf5tj4jd.PNG)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/750sla9lud46bhrn5dku.PNG)
###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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t33097krqh1qtz1c1cnw.PNG)
##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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xx24f0wg69mrc3foz9c2.PNG)
###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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/juzv9c8j2vwefdzk6kib.PNG)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h87mwpo2b78zdbaaf5bu.PNG)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pz1b944cij6cfg5vslm1.PNG)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9bftgftiejtlc13iekq8.PNG)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9aul07zomv8ys1rdzdsg.PNG)
Next, we will create a model in models.py to store the name and phone number of a customer.

smsapp/models.py
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tadlmc5w8t1bf7id12kn.PNG)
###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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yqxs1oq37h9h9bscbo8j.PNG)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vp9hcdw1htodex55dvk8.PNG)
Next, we migrate with python manage.py migrate. Migrate is responsible for applying and reapplying migrations. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vc96fxqbnzhz838k01kk.PNG) 
Next, we need to register our model in our admin:

smsapp/admin.py
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lcrqjr51m4qauzml0vic.PNG)
 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lcrqjr51m4qauzml0vic.PNG)
Then log in with our username and password. If you log in successfully, you see this:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ks2xrriikkrel2t0bj2s.PNG)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0sur4mi1nhgiri8hd8ly.PNG)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/27flsdc4klxlfjop7wli.PNG)
Add the Twilio account SID and Auth token from your Twilio dashboard to the settings.py of your project.

settings.py
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4xaq3560t2itb5ujygq7.PNG)
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 sent SMS to your customer on your platform. You can use the add customer button to add more customers and their phone numbers, and send them messages at a go. Note that, the phone numbers must be registered on Twilio if you are using a trial account. If you want to send unverified phone numbers, you will need to upgrade your Twilio account.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2fh78yhtiua8rh3k8nix.PNG)

##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](https://www.linkedin.com/in/peter-aideloje-64aab6116/)  [My twitter handle](https://twitter.com/PAideloje)