From 3508e834111c89aeadd6ab1ea6724708bf0bd673 Mon Sep 17 00:00:00 2001 From: Sam Kimbrel Date: Sat, 2 Nov 2013 11:57:19 -0700 Subject: [PATCH 1/3] Support in TwiML generator --- tests/test_twiml.py | 13 +++++++++++++ twilio/twiml.py | 31 +++++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/tests/test_twiml.py b/tests/test_twiml.py index da33c76a5b..de4a1f6701 100644 --- a/tests/test_twiml.py +++ b/tests/test_twiml.py @@ -147,6 +147,19 @@ def testPlayBadAppend(self): """ should raise exceptions for wrong appending""" self.improperAppend(twiml.Play("")) + def testPlayDigits(self): + """should play digits""" + r = Response() + r.append(twiml.Play(digits='w123')) + r = self.strip(r) + self.assertEqual(r, '') + + def testUrlOrDigitsRequired(self): + self.assertRaises(TwimlException, twiml.Play) + + def testEitherUrlOrDigits(self): + self.assertRaises(TwimlException, twiml.Play, url='http://example.com', digits='1') + class TestRecord(TwilioTest): diff --git a/twilio/twiml.py b/twilio/twiml.py index 52501b43d7..56f1d4028e 100644 --- a/twilio/twiml.py +++ b/twilio/twiml.py @@ -115,10 +115,10 @@ def say(self, text, **kwargs): :class:`Response` """ return self.append(Say(text, **kwargs)) - def play(self, url, **kwargs): + def play(self, url=None, digits=None, **kwargs): """Return a newly created :class:`Play` verb, nested inside this :class:`Response` """ - return self.append(Play(url, **kwargs)) + return self.append(Play(url=url, digits=digits, **kwargs)) def pause(self, **kwargs): """Return a newly created :class:`Pause` verb, nested inside this @@ -238,18 +238,33 @@ def __init__(self, text, **kwargs): class Play(Verb): - """Play an audio file at a URL + """Play DTMF digits or audio from a URL. - :param url: point to af audio file. The MIME type on the file must be set - correctly. + :param str url: point to af audio file. The MIME type on the file must be set + correctly. Either `url` or `digits` must be specified. - :param loop: specifies how many times you'd like the text repeated. + :param str digits: a string of digits to play. To pause before playing digits, + use leading 'w' characters. Each 'w' will cause Twilio to + wait 0.5 seconds instead of playing a digit. + Either `url` or `digits` must be specified. + + :param int loop: specifies how many times you'd like the text repeated. Specifying '0' will cause the the :class:`Play` verb to loop until the call is hung up. Defaults to 1. """ - def __init__(self, url, **kwargs): + def __init__(self, url=None, digits=None, **kwargs): + if url is None and digits is None: + raise TwimlException( + "Please specify either a url or digits to play.", + ) + if url is not None and digits is not None: + raise TwimlException("Please specify only one of: (url, digits).") + + if digits is not None: + kwargs['digits'] = digits super(Play, self).__init__(**kwargs) - self.body = url + if url is not None: + self.body = url class Pause(Verb): From ca7814af8e78abe783eb3f4f945d78a759707cb4 Mon Sep 17 00:00:00 2001 From: Sam Kimbrel Date: Sat, 2 Nov 2013 12:05:34 -0700 Subject: [PATCH 2/3] Fix line length --- twilio/twiml.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/twilio/twiml.py b/twilio/twiml.py index 56f1d4028e..17e768a836 100644 --- a/twilio/twiml.py +++ b/twilio/twiml.py @@ -240,12 +240,12 @@ def __init__(self, text, **kwargs): class Play(Verb): """Play DTMF digits or audio from a URL. - :param str url: point to af audio file. The MIME type on the file must be set - correctly. Either `url` or `digits` must be specified. + :param str url: point to af audio file. The MIME type on the file must be + set correctly. Either `url` or `digits` must be specified. - :param str digits: a string of digits to play. To pause before playing digits, - use leading 'w' characters. Each 'w' will cause Twilio to - wait 0.5 seconds instead of playing a digit. + :param str digits: a string of digits to play. To pause before playing + digits, use leading 'w' characters. Each 'w' will cause + Twilio to wait 0.5 seconds instead of playing a digit. Either `url` or `digits` must be specified. :param int loop: specifies how many times you'd like the text repeated. From 9b10f302ddbf49c9171889c5aa4cbd98825bc2f6 Mon Sep 17 00:00:00 2001 From: Sam Kimbrel Date: Mon, 4 Nov 2013 13:15:43 -0800 Subject: [PATCH 3/3] Allow both digits and url; remove unused import --- tests/test_twiml.py | 3 --- twilio/twiml.py | 12 ++++++------ 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/tests/test_twiml.py b/tests/test_twiml.py index de4a1f6701..cc15513541 100644 --- a/tests/test_twiml.py +++ b/tests/test_twiml.py @@ -157,9 +157,6 @@ def testPlayDigits(self): def testUrlOrDigitsRequired(self): self.assertRaises(TwimlException, twiml.Play) - def testEitherUrlOrDigits(self): - self.assertRaises(TwimlException, twiml.Play, url='http://example.com', digits='1') - class TestRecord(TwilioTest): diff --git a/twilio/twiml.py b/twilio/twiml.py index 17e768a836..56ce588066 100644 --- a/twilio/twiml.py +++ b/twilio/twiml.py @@ -2,7 +2,6 @@ Make sure to check out the TwiML overview and tutorial """ import xml.etree.ElementTree as ET -from six import iteritems class TwimlException(Exception): @@ -240,13 +239,16 @@ def __init__(self, text, **kwargs): class Play(Verb): """Play DTMF digits or audio from a URL. - :param str url: point to af audio file. The MIME type on the file must be - set correctly. Either `url` or `digits` must be specified. + :param str url: point to an audio file. The MIME type on the file must be + set correctly. At least one of `url` and `digits` must be + specified. If both are given, the digits will play prior + to the audio from the URL. :param str digits: a string of digits to play. To pause before playing digits, use leading 'w' characters. Each 'w' will cause Twilio to wait 0.5 seconds instead of playing a digit. - Either `url` or `digits` must be specified. + At least one of `url` and `digits` must be specified. + If both are given, the digits will play first. :param int loop: specifies how many times you'd like the text repeated. Specifying '0' will cause the the :class:`Play` verb to loop @@ -257,8 +259,6 @@ def __init__(self, url=None, digits=None, **kwargs): raise TwimlException( "Please specify either a url or digits to play.", ) - if url is not None and digits is not None: - raise TwimlException("Please specify only one of: (url, digits).") if digits is not None: kwargs['digits'] = digits