theptrk
theptrk

Reputation: 840

Twilio/Django not receiving response SMS

I want to text a twilio number and start a series of questions for a user. If it is their first time texting, a new "Caller" should be created. If they have played before, I'd like to look up the "last_question", we asked them and ask them the appropriate question. My code below yields no SMS response and a Twilio error "HTTP retrieval failure."

In models.py I have

class Caller(models.Model):
    body = models.CharField(max_length=200)
    from_number = models.CharField(max_length=20)
    last_question = models.CharField(max_length=2, default="0")

    def __unicode__(self):
        return self.body

In views.py

def hello_there(request):
    body = request.REQUEST.get('Body', None)
    from_number = request.REQUEST.get('From', None)
    try:
        caller = Caller.objects.get(from_number = from_number)
    except Caller.DoesNotExist:
        caller = None
    if caller:
        if caller.last_question == "0":
            if body == "Password":
                message = "Welcome to the game. What is 3 + 4?"
                caller.last_question = "1"
            else:
                message = "What is the password?"
        else:
            message = "you broke me"
    else:
        new_caller = Caller(body=body, from_number=from_number, last_question="0")
        new_caller.save()
        message = "New user created"
    resp = twilio.twiml.Reponse()
    resp.sms(message)
    return HttpResponse(str(resp))

Upvotes: 1

Views: 455

Answers (1)

phalt
phalt

Reputation: 1244

Twilio employee here - the issue could be because you're not providing a csrf_exempt decorator around this view. Django will trigger a security error because it is receiving a HTTP POST request from twilio.com. Django will not accept any HTTP POST request without a csrf token unless you make it exempt.

Have you thought about using the django-twilio package for Django? It will make your life much easier when developing with twilio. This is what your view will look like with django-twilio:

from django_twilio.decorators import twilio_view

@twilio_view
def hello_there(request):
    body = request.REQUEST.get('Body', None)
    from_number = request.REQUEST.get('From', None)
    try:
        caller = Caller.objects.get(from_number=from_number)
    except Caller.DoesNotExist:
        caller = None
    if caller:
        if caller.last_question == "0":
            if body == "Password":
                message = "Welcome to the game. What is 3 + 4?"
                caller.last_question = "1"
            else:
                message = "What is the password?"
        else:
            message = "you broke me"
    else:
        new_caller = Caller(body=body, from_number=from_number, last_question="0")
        new_caller.save()
        message = "New user created"
    resp = twilio.twiml.Reponse()
    resp.sms(message)
return resp

The twilio_view decorator will provide csrf exemption as well as ensuring all your requests are genuine and from twilio.com.

Check out the installation instructions to get started.

Upvotes: 1

Related Questions