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.
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:
- 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
- Not having to worry about purchasing and managing traditional backend servers.
- 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.
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
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’.
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.
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.
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.
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
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.
STEPS TO BUILD THE SMS API IN DJANGO FRAMEWORK
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.
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
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.
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.
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.”
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:
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.
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.
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”
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.”
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
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:
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:
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.
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:
Next, we migrate with python manage.py migrate. Migrate is responsible for applying and reapplying migrations.
Next, we need to register our model in our admin:
Create an admin(a superuser) that will compose and send messages to the customer.
Let's run our server. Go to localhost:8000/admin
Then log in with our username and password. If you log in successfully, you see this:
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.
phone number: +2347033622204
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:
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.
Add the Twilio account SID and Auth token from your Twilio dashboard to the settings.py of your project.
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.
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.
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