8000 Merge pull request #353 from twilio/DEVX-5006_update_gather_twiml · twilio/twilio-python@cea9fab · GitHub
[go: up one dir, main page]

Skip to content

Commit cea9fab

Browse files
authored
Merge pull request #353 from twilio/DEVX-5006_update_gather_twiml
[DEVX-5006] Add input parameter to 'gather' and coerce language format.
2 parents 3d32449 + efa6385 commit cea9fab

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

tests/unit/twiml/__init__.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from nose.tools import raises
44
from six import text_type
55

6-
from twilio.twiml import TwiMLException, TwiML
6+
from twilio.twiml import format_language, TwiMLException, TwiML
77

88

99
class TwilioTest(unittest.TestCase):
@@ -14,3 +14,19 @@ def strip(self, xml):
1414
def test_append_fail(self):
1515
t = TwiML()
1616
t.append('foobar')
17+
18+
def test_format_language_none(self):
19+
language = None
20+
self.assertEqual(language, format_language(language))
21+
22+
def test_format_language_valid(self):
23+
language = 'en-US'
24+
self.assertEqual(language, format_language(language))
25+
26+
def test_format_language_coerced(self):
27+
language = 'EN_us'
28+
self.assertEqual('en-US', format_language(language))
29+
30+
@raises(TwiMLException)
31+
def test_format_language_fail(self):
32+
format_language('this is invalid')

twilio/twiml/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import xml.etree.ElementTree as ET
23

34

@@ -9,6 +10,21 @@ def lower_camel(string):
910
return result[0].lower() + result[1:]
1011

1112

13+
def format_language(language):
14+
"""
15+
Attempt to format language parameter as 'ww-WW'.
16+
17+
:param string language: language parameter
18+
"""
19+
if not language:
20+
return language
21+
22+
if not re.match('^[a-zA-Z]{2}[_-][a-zA-Z]{2}$', language):
23+
raise TwiMLException('Invalid value for language parameter.')
24+
25+
return language[0:2].lower() + '-' + language[3:5].upper()
26+
27+
1228
class TwiMLException(Exception):
1329
pass
1430

twilio/twiml/voice_response.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
from six import string_types
33

4-
from twilio.twiml import TwiML
4+
from twilio.twiml import TwiML, format_language
55

66

77
class VoiceResponse(TwiML):
@@ -113,6 +113,7 @@ def gather(self,
113113
hints=None,
114114
barge_in=None,
115115
acknowledge_sound_url=None,
116+
input=None,
116117
**kwargs):
117118
"""
118119
Add a new <Gather> element
@@ -128,6 +129,7 @@ def gather(self,
128129
:param hints: speech recognition hints
129130
:param barge_in: stop playing media upon speech
130131
:param acknowledge_sound_url: url to hit when sound starts
132+
:param input: type Twilio should accept "dtfm", "speech", "dtfm speech"
131133
:param kwargs: additional attributes
132134
:return: <Response> element
133135
"""
@@ -137,6 +139,13 @@ def gather(self,
137139
timeout=timeout,
138140
finish_on_key=finish_on_key,
139141
num_digits=num_digits,
142+
partial_result_callback=partial_result_callback,
143+
partial_result_callback_method=partial_result_callback_method,
144+
language=format_language(language),
145+
hints=hints,
146+
barge_in=barge_in,
147+
acknowledge_sound_url=acknowledge_sound_url,
148+
input=input,
140149
**kwargs
141150
))
142151

0 commit comments

Comments
 (0)
0