8000 Merge pull request #143 from twilio/play-digits · twilio/twilio-python@97a66c8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 97a66c8

Browse files
committed
Merge pull request #143 from twilio/play-digits
Support <Play digits='...'> in TwiML generator
2 parents 670a6ba + 9b10f30 commit 97a66c8

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

tests/test_twiml.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ def testPlayBadAppend(self):
147147
""" should raise exceptions for wrong appending"""
148148
self.improperAppend(twiml.Play(""))
149149

150+
def testPlayDigits(self):
151+
"""should play digits"""
152+
r = Response()
153+
r.append(twiml.Play(digits='w123'))
154+
r = self.strip(r)
155+
self.assertEqual(r, '<?xml version="1.0" encoding="UTF-8"?><Response><Play digits="w123" /></Response>')
156+
157+
def testUrlOrDigitsRequired(self):
158+
self.assertRaises(TwimlException, twiml.Play)
159+
150160

151161
class TestRecord(TwilioTest):
152162

twilio/twiml.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Make sure to check out the TwiML overview and tutorial
33
"""
44
import xml.etree.ElementTree as ET
5-
from six import iteritems
65

76

87
class TwimlException(Exception):
@@ -115,10 +114,10 @@ def say(self, text, **kwargs):
115114
:class:`Response` """
116115
return self.append(Say(text, **kwargs))
117116

118-
def play(self, url, **kwargs):
117+
def play(self, url=None, digits=None, **kwargs):
119118
"""Return a newly created :class:`Play` verb, nested inside this
120119
:class:`Response` """
121-
return self.append(Play(url, **kwargs))
120+
return self.append(Play(url=url, digits=digits, **kwargs))
122121

123122
def pause(self, **kwargs):
124123
"""Return a newly created :class:`Pause` verb, nested inside this
@@ -238,18 +237,34 @@ def __init__(self, text, **kwargs):
238237

239238

240239
class Play(Verb):
241-
"""Play an audio file at a URL
240+
"""Play DTMF digits or audio from a URL.
242241
243-
:param url: point to af audio file. The MIME type on the file must be set
244-
correctly.
242+
:param str url: point to an audio file. The MIME type on the file must be
243+
set correctly. At least one of `url` and `digits` must be
244+
specified. If both are given, the digits will play prior
245+
to the audio from the URL.
245246
246-
:param loop: specifies how many times you'd like the text repeated.
247+
:param str digits: a string of digits to play. To pause before playing
248+
digits, use leading 'w' characters. Each 'w' will cause
249+
Twilio to wait 0.5 seconds instead of playing a digit.
250+
At least one of `url` and `digits` must be specified.
251+
If both are given, the digits will play first.
252+
253+
:param int loop: specifies how many times you'd like the text repeated.
247254
Specifying '0' will cause the the :class:`Play` verb to loop
248255
until the call is hung up. Defaults to 1.
249256
"""
250-
def __init__(self, url, **kwargs):
257+
def __init__(self, url=None, digits=None, **kwargs):
258+
if url is None and digits is None:
259+
raise TwimlException(
260+
"Please specify either a url or digits to play.",
261+
)
262+
263+
if digits is not None:
264+
kwargs['digits'] = digits
251265
super(Play, self).__init__(**kwargs)
252-
self.body = url
266+
if url is not None:
267+
self.body = url
253268

254269

255270
class Pause(Verb):

0 commit comments

Comments
 (0)
0