How to Send a Text Message with Twilio in Django



Python


In this article, we show how to send a text message with twilio in Django.

So sending text messages with a website is actually very useful and commonly done today.

It is probably used most often for security purposes.

Banks use the sending of text messages heavily. They send text messages to verify that you really are the person using the account by checking the phone number that you have on file.

Other websites that deal with people's money also use the sending of text messages often to make sure that the person signing in really is the account owner. They normally sending a random number to the cell phone and the user has to input this number. The numbers have to match in order for the user to be logged in, send money, etc.

So sending of text messages is a very good measure that can add security to your website.

In order to send a text message, you need a SMS gateway service. This is a service that allows a computer to send or receive SMS (Short Message Service) transmissions to or from a telecommunications network.

Twilio is one of the most popular SMS gateway services.

Twilio allows a computer to be able to send SMS messages.

You would not be able to send text messages directly with Python without a SMS gateway such as Twilio.

So in order to send a text message with the SMS gateway, Twilio, you need to sign up with Twilio and get an account. When you create an account, you will need 3 pieces of information in order to send a text message. You need a twilio account SID, a twilio auth token, and a twilio phone number that sends the text messages. You cannot just use your phone number as a phone number to send text messages. The phone number must be from twilio.

So now let's go through all the steps needed in order to set up text messaging in Django.

Installation

To install twilio on Django, then use the following line shown below.



This takes a few seconds to install twilio.

After this is done, then you need to add 'django-twilio' to the INSTALLED_APPS list in the settings.py file.

This is shown below.



Since you've now added, 'django-twilio', to the INSTALLED_APPS list in the settings.py file, then you must apply migrations.

This is done with the following line below.



Now the django-twilio app is installed sitewide across the project.

This completes the installation.

settings.py File

Now that everything is installed, we need to add a few lines of code into the settings.py file.

We need to add the twilio account SID, the twilio auth token, and the twilio phone number that twilio gave us that we will send messages from.

So it will look like what is shown below.



So since these are constants (unless of course you change your twilio account or get a new phone number to use), you want to keep these in the settings.py file.

Now since the settings are all in place, the last thing to do is write the code to actually send the text message.

views.py file

Now the last thing we have to do is write the code to send a text message.

This is shown below.



So in order to send message, we have to import Client from twilio.rest

Since we saved many settings in the settings.py file, we import settings from django.conf

We then write a simple message, "Hello testing twilio in Django"

So if you put this in a function, it will run and send a text message to the phone number, 1-111-1111.

And this is how you can send a text message with twilio in Django.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...