Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DEVX-5006] Add input parameter to 'gather' and coerce language format. #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion tests/unit/twiml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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')
16 changes: 16 additions & 0 deletions twilio/twiml/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import xml.etree.ElementTree as ET


Expand All @@ -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('^[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()


class TwiMLException(Exception):
pass

Expand Down
11 changes: 10 additions & 1 deletion twilio/twiml/voice_response.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -111,6 +111,7 @@ def gather(self,
hints=None,
barge_in=None,
acknowledge_sound_url=None,
input=None,
**kwargs):
"""
Add a new <Gather> element
Expand All @@ -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: <Gather> element
"""
Expand All @@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

input is a builtin function, so this might cause some trouble. Did these tests pass?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They did. While it's a built-in function, it's not reserved like "from". One could argue that there's potential that overwriting the value of input in this scope could cause errors; but, I seriously doubt it would. Happy to change it to "input_" if that's a preferred strategy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving this alone will be fine in practice thanks to scoping.

**kwargs
))

Expand Down