Adding SMS messaging to your Django application with Twilio
Sms is a good, efficient way to send out time sensitive alerts and notifications to your users, I recently added SMS messaging for a website uptime monitoring feature I launched with 199fix.com and I want to show you how to do the same and hopefully help you avoid some of the pitfalls I went through during the process.
We will use django-twilio , a great django application that comes with most of the utilities you will need to deliver sms.
The easiest way to install django-twilio is via pip
$ pip install django-twilio
After django-twilio is installed you will need to add it to your Django application, go to your settings.py file add `django_twilio` to the INSTALLED_APPS tuple
INSTALLED_APPS = (
'django_twilio',
...
)
Once that is done you will need to sync the django-twilio models with your database, the commands to do this vary based on the Django version you are using
For Django 1.6 or lower
$ python manage.py syncdb
For Django 1.7 and above
$ python manage.py migrate django_twilio
Once you’ve got the django-twilio installed and your database models properly synced you will need to add your twilio api credentials which you will find here to your application.
Django will check for the api credentials in you environment variables and then in the Settings variables in settings.py, the easiest way to add these variables is to the settings.py file, just go ahead and add the code snippet below, replace the dummy data with your actual key values
TWILIO_ACCOUNT_SID = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
TWILIO_AUTH_TOKEN = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY'
Now that you’ve got all the hard stuff out of the way, it’s time for the fun part – sending the SMS.
The snippet below sends the message `Hi David!` to the Phonenumber `+15556667771` an sms directly a view
from twilio.rest import TwilioRestClient
from django.conf import settings
def awesome_method(request):
message = 'Hi David'
from_ = '+15556667777'
to = '+15556667771';
client = TwilioRestClient(
settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
response = client.messages.create(
body=message, to=to, from_=from_)
#do something with response
The response object returns the status of the api call, you can find more details on response codes here.
The code above is not DRY , duplicating the snippet above across your views will cause a maintenance nightmare, personally I prefer to create a utils.py file in my main app and a utility function .
from django.conf import settings
from twilio.rest import TwilioRestClient
from django.template.loader import render_to_string
def send_sms(to, message, content={}, template=None):
'''sms utility method'''
content.update({
'put any content here'
})
if not template:
'''If we have a template format the message'''
message = render_to_string(template, content)
message = message.encode('utf-8')
client = TwilioRestClient(
settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
response = client.messages.create(
body=message, to=to, from_='+15556667777')
return response
I can then re-write awesome_method to call the send_sms method, this is maintainable.
I can use this to send out automated messages, I also have the added advantage being able to format my messages using templates.
For example I send out sms alerts to 199fix users when your website goes down , lets say I have the following template store in templates/downtime_alert.html
{% if back_up %}
Hi {{name}, the website {{site_name}} [{{site_link}}] is back up at {{when}}.
{% else %}
Hi {{name}, the website {{site_name}} [{{site_link}}] is down at {{when}}.
{% endif %}
I will then use the send_sms method and the downtime_alert.html template to send out notifications
from app.utils import send_sms
def downtime_monitor():
content = {'when':timezone.now(),'name':user.name,'back_up':back_up}
message = ''
template = 'downtime_alert.html'
to = user.phonenumber
response = send_sms(to, message, content, template)
#do something with response
I’ve found that this is easy to manage, maintain and debug when things go wrong.
This is just the tip of the iceberg of what you can do with Twilio SMS or Twilio for that matter.
For more information on how to configure and troubleshoot django-twilio go here , if you want to find out more about the twilio api go here .