10000 #283 -- Add Task verb to voice_response.py by jmctwilio · Pull Request #336 · twilio/twilio-python · GitHub
[go: up one dir, main page]

Skip to content

#283 -- Add Task verb to voice_response.py #336

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 1 commit into from
May 10, 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
50 changes: 49 additions & 1 deletion tests/unit/twiml/test_voice_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from nose.tools import assert_equal
from six import u
from tests.unit.twiml import TwilioTest
from twilio.twiml.voice_response import VoiceResponse, Dial, Gather
from twilio.twiml.voice_response import VoiceResponse, Dial, Enqueue, Gather


class TestResponse(TwilioTest):
Expand Down Expand Up @@ -349,6 +349,18 @@ def test_queue(self):
)


class TestEcho(TwilioTest):

def test_echo(self):
r = VoiceResponse()
r.echo()

assert_equal(
self.strip(r),
'<?xml version="1.0" encoding="UTF-8"?><Response><Echo /></Response>'
)


class TestEnqueue(TwilioTest):

def test_enqueue(self):
Expand All @@ -366,6 +378,30 @@ def test_enqueue(self):
'<?xml version="1.0" encoding="UTF-8"?><Response><Enqueue action="act" method="GET" waitUrl="wait" waitUrlMethod="POST">TestEnqueueAttribute</Enqueue></Response>'
)

def test_task_string(self):
e = Enqueue(None, workflowSid='123123123')
e.task('{"account_sid": "AC123123123"}')

r = VoiceResponse()
r.append(e)

assert_equal(
self.strip(r),
'<?xml version="1.0" encoding="UTF-8"?><Response><Enqueue workflowsid="123123123"><Task>{"account_sid": "AC123123123"}</Task></Enqueue></Response>'
)

def test_task_dict(self):
e = Enqueue(None, workflowSid='123123123')
e.task({"account_sid": "AC123123123"})

r = VoiceResponse()
r.append(e)

assert_equal(
self.strip(r),
'<?xml version="1.0" encoding="UTF-8"?><Response><Enqueue workflowsid="123123123"><Task>{"account_sid": "AC123123123"}</Task></Enqueue></Response>'
)


class TestDial(TwilioTest):

Expand All @@ -379,6 +415,18 @@ def test_dial(self):
'<?xml version="1.0" encoding="UTF-8"?><Response><Dial>1231231234</Dial></Response>'
)

def test_sim(self):
d = Dial()
d.sim('123123123')

r = VoiceResponse()
r.append(d)

assert_equal(
self.strip(r),
'<?xml version="1.0" encoding="UTF-8"?><Response><Dial><Sim>123123123</Sim></Dial></Response>'
)

def test_sip(self):
""" should redirect the call """
d = Dial()
Expand Down
75 changes: 75 additions & 0 deletions twilio/twiml/voice_response.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from twilio.twiml import TwiML


Expand Down Expand Up @@ -57,6 +59,15 @@ def dial(self,
**kwargs
))

def echo(self,
**kwargs):
"""
Add a new <Echo> element
"""
return self.append(Echo(
**kwargs
))

def enqueue(self,
name,
action=None,
Expand Down Expand Up @@ -444,6 +455,19 @@ def queue(self,
**kwargs
))

def sim(self,
sid,
**kwargs):
"""
Add a <Sim> element

:param sid: sim sid
"""
return self.append(Sim(
sid,
**kwargs
))

def sip(self,
uri,
username=None,
Expand Down Expand Up @@ -541,6 +565,20 @@ def __init__(self, queue_name, **kwargs):
self.value = queue_name


class Sim(TwiML):
"""
<Sim> element
"""
def __init__(self, sid, **kwargs):
"""
Create a new <Sim> element

:param sid: sim sid
"""
super(Sim, self).__init__(**kwargs)
self.value = sid


class Sip(TwiML):
"""
<Sip> element
Expand All @@ -556,6 +594,17 @@ def __init__(self, uri, **kwargs):
self.value = uri


class Echo(TwiML):
"""
<Echo> element
"""
def __init__(self, **kwargs):
"""
Create a new <Echo> element
"""
super(Echo, self).__init__(**kwargs)


class Enqueue(TwiML):
"""
<Enqueue> element
Expand All @@ -570,6 +619,32 @@ def __init__(self, name, **kwargs):
super(Enqueue, self).__init__(**kwargs)
self.value = name

def task(self, attributes, **kwargs):
"""
Add a <Task> element

:param attributes: Attributes for a task
:return: <Task> element
"""
return self.append(Task(attributes, **kwargs))


class Task(TwiML):
"""
<Task> element
"""
def __init__(self, attributes, **kwargs):
"""
Create a new <Task> element

:param attributes: Attributes for a task
"""
super(Task, self).__init__(**kwargs)
if isinstance(attributes, basestring):
self.value = attributes
else:
self.value = json.dumps(attributes)


class Gather(TwiML):
"""
Expand Down
0