Skip to content

Commit

Permalink
Say welcome using message with a middleware
Browse files Browse the repository at this point in the history
Created a middleware to tell if request is sms or not and used this
middleware on the welcome message while deciding between say and message
verbs;
  • Loading branch information
shyba committed Apr 5, 2016
1 parent 716dcfe commit f2fac93
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
8 changes: 8 additions & 0 deletions automated_survey/tests/surveys_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ def test_entry_point_redirection(self):

assert expected_url in response.url

def test_show_message_verb_on_sms(self):
response = self.client.get(reverse('survey',
kwargs={'survey_id': self.survey.id}),
{'MessageSid': '123'})

assert '<Message>' in response.content.decode('utf8')
assert '<Say>' not in response.content.decode('utf8')

def test_show_survey(self):
response = self.client.get(reverse('survey',
kwargs={'survey_id': self.survey.id}))
Expand Down
14 changes: 8 additions & 6 deletions automated_survey/views/surveys.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ def show_survey(request, survey_id):
}

first_question_url = reverse('question', kwargs=first_question_ids)
text_response = twiml.Response()

text_response.say(
'Hello and thank you for taking the %s survey' %
survey.title)
text_response.redirect(first_question_url, method='GET')
welcome = 'Hello and thank you for taking the %s survey' % survey.title
twiml_response = twiml.Response()
if request.is_sms:
twiml_response.message(welcome)
else:
twiml_response.say(welcome)
twiml_response.redirect(first_question_url, method='GET')

return HttpResponse(text_response, content_type='application/xml')
return HttpResponse(twiml_response, content_type='application/xml')


@require_POST
Expand Down
1 change: 1 addition & 0 deletions twilio_sample_project/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'automated_survey.middleware.SMSMiddleware',
)

ROOT_URLCONF = 'twilio_sample_project.urls'
Expand Down

0 comments on commit f2fac93

Please sign in to comment.