From f754ab4a28fe2142667c488ff1e2ecf3b928ac7d Mon Sep 17 00:00:00 2001 From: Jeremy McEntire Date: Tue, 13 Jun 2017 12:30:48 -0700 Subject: [PATCH 1/2] [DEVX-5006] Add input parameter to 'gather' and coerce language format. --- tests/unit/twiml/__init__.py | 18 +++++++++++++++++- twilio/twiml/__init__.py | 16 ++++++++++++++++ twilio/twiml/voice_response.py | 11 ++++++++++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/tests/unit/twiml/__init__.py b/tests/unit/twiml/__init__.py index 65873e0927..1bc5a944df 100644 --- a/tests/unit/twiml/__init__.py +++ b/tests/unit/twiml/__init__.py @@ -3,7 +3,7 @@ from nose.tools import raises from six import text_type -from twilio.twiml import TwiMLException, TwiML +from twilio.twiml import format_language, TwiMLException, TwiML class TwilioTest(unittest.TestCase): @@ -14,3 +14,19 @@ def strip(self, xml): def test_append_fail(self): t = TwiML() t.append('foobar') + + def test_format_language_none(self): + language = None + self.assertEqual(language, format_language(language)) + + def test_format_language_valid(self): + language = 'en-US' + self.assertEqual(language, format_language(language)) + + def test_format_language_coerced(self): + language = 'EN_us' + self.assertEqual('en-US', format_language(language)) + + @raises(TwiMLException) + def test_format_language_fail(self): + format_language('this is invalid') diff --git a/twilio/twiml/__init__.py b/twilio/twiml/__init__.py index 6c7c05a94c..92dfcdbcc2 100644 --- a/twilio/twiml/__init__.py +++ b/twilio/twiml/__init__.py @@ -1,3 +1,4 @@ +import re import xml.etree.ElementTree as ET @@ -9,6 +10,21 @@ def lower_camel(string): return result[0].lower() + result[1:] +def format_language(language): + """ + Attempt to format language parameter as 'ww-WW'. + + :param string language: language parameter + """ + if not language: + return language + + if not re.match('^\w\w[_-]\w\w$', language): + raise TwiMLException('Invalid value for language parameter.') + + return language[0:2].lower() + '-' + language[3:5].upper() + + class TwiMLException(Exception): pass diff --git a/twilio/twiml/voice_response.py b/twilio/twiml/voice_response.py index 59d2593aef..c6151c8121 100644 --- a/twilio/twiml/voice_response.py +++ b/twilio/twiml/voice_response.py @@ -1,7 +1,7 @@ import json from six import string_types -from twilio.twiml import TwiML +from twilio.twiml import TwiML, format_language class VoiceResponse(TwiML): @@ -111,6 +111,7 @@ def gather(self, hints=None, barge_in=None, acknowledge_sound_url=None, + input=None, **kwargs): """ Add a new element @@ -126,6 +127,7 @@ def gather(self, :param hints: speech recognition hints :param barge_in: stop playing media upon speech :param acknowledge_sound_url: url to hit when sound starts + :param input: type Twilio should accept "dtfm", "speech", "dtfm speech" :param kwargs: additional attributes :return: element """ @@ -135,6 +137,13 @@ def gather(self, timeout=timeout, finish_on_key=finish_on_key, num_digits=num_digits, + partial_result_callback=partial_result_callback, + partial_result_callback_method=partial_result_callback_method, + language=format_language(language), + hints=hints, + barge_in=barge_in, + acknowledge_sound_url=acknowledge_sound_url, + input=input, **kwargs )) From efa6385a70faf619012f74c5859a362b13027ccd Mon Sep 17 00:00:00 2001 From: Jeremy McEntire Date: Fri, 16 Jun 2017 11:18:46 -0700 Subject: [PATCH 2/2] [DEVX-5006] Increase constraints for language argument regex. --- twilio/twiml/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/twilio/twiml/__init__.py b/twilio/twiml/__init__.py index 92dfcdbcc2..8e17325c7f 100644 --- a/twilio/twiml/__init__.py +++ b/twilio/twiml/__init__.py @@ -19,7 +19,7 @@ def format_language(language): if not language: return language - if not re.match('^\w\w[_-]\w\w$', language): + if not re.match('^[a-zA-Z]{2}[_-][a-zA-Z]{2}$', language): raise TwiMLException('Invalid value for language parameter.') return language[0:2].lower() + '-' + language[3:5].upper()