rdegges/django-twilio — pull request #71 by phalt (2014-11-04 15:55:48+00:00)
Title: Decompose functionality

(closes #70)

A new ```decompose()``` function will strip out the Twilio-specific POST parameters from a Django HttpRequest object and present them back as a ```TwilioRequest``` object.

Each POST parameter will be an attribute on the new ```TwilioRequest``` class. This makes it much easier to discover the parameters sent to you from Twilio and access them without having to use the ```HttpRequest``` object. The ```decompose()``` function can also discover the type of Twilio request (''Message'' or ''Voice'') based on the parameters that are sent to you.

This means you could build a single view endpoint and route traffic based on the type of Twilio request you receive!

Here is an example:

from twilio import twiml
from django_twilio.decorators import twilio_view

# include decompose in your views.py
from django_twilio.requests import decompose

@twilio_view
def inbound_view(request):

response = twiml.response()

# Create a new TwilioRequest object
twilio_request = decompose(request)

# See the Twilio attributes on the class
twilio_request.to
# >>> '+44123456789'

# Discover the type of request
if twilio_request.type is 'message':
response.message('Thanks for the message!')
return response

# Handle different types of requests in a single view
if twilio_request.type is 'voice':
return voice_view(request)

return response