8000 Support <Play digits='...'> in TwiML generator by skimbrel · Pull Request #143 · twilio/twilio-python · GitHub
[go: up one dir, main page]

Skip to content

Support <Play digits='...'> in TwiML generator #143

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 3 commits into from
Nov 4, 2013
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
10 changes: 10 additions & 0 deletions tests/test_twiml.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ 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, '<?xml version="1.0" encoding="UTF-8"?><Response><Play digits="w123" /></Response>')

def testUrlOrDigitsRequired(self):
self.assertRaises(TwimlException, twiml.Play)


class TestRecord(TwilioTest):

Expand Down
33 changes: 24 additions & 9 deletions twilio/twiml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -115,10 +114,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
Expand Down Expand Up @@ -238,18 +237,34 @@ 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 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 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.
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
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 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):
Expand Down
0