8000 Allow adding text to TwiML nodes by ekarson · Pull Request #422 · twilio/twilio-python · GitHub
[go: up one dir, main page]

Skip to content

Allow adding text to TwiML nodes #422

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
Jun 14, 2018
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
2 changes: 1 addition & 1 deletion tests/unit/twiml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def strip(self, xml):
@raises(TwiMLException)
def test_append_fail(self):
t = TwiML()
t.append('foobar')
t.append(12345)

def test_format_language_none(self):
language = None
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/twiml/test_messaging_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,25 @@ def test_redirect(self):
self.strip(r),
'<?xml version="1.0" encoding="UTF-8"?><Response><Redirect>example.com</Redirect></Response>'
)


class TestText(TwilioTest):
def test_text(self):
r = MessagingResponse()
r.append('No tags!')

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

def text_mixed(self):
r = MessagingResponse()
r.append('before')
r.append(Body('Content'))
r.append('after')

assert_equal(
self.strip(r),
'<?xml version="1.0" encoding="UTF-8"?><Response>before<Body>Content</Body>after</Response>'
)
22 changes: 22 additions & 0 deletions tests/unit/twiml/test_voice_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,25 @@ def test_nested_say_play_pause(self):
self.strip(r),
'<?xml version="1.0" encoding="UTF-8"?><Response><Gather><Say>Hey</Say><Play>hey.mp3</Play><Pause /></Gather></Response>'
)


class TestText(TwilioTest):
def test_text(self):
r = VoiceResponse()
r.append('No tags!')

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

def text_mixed(self):
r = VoiceResponse()
r.append('before')
r.say('Content')
r.append('after')

assert_equal(
self.strip(r),
'<?xml version="1.0" encoding="UTF-8"?><Response>before<Say>Content</Say>after</Response>'
)
28 changes: 13 additions & 15 deletions twilio/twiml/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
# coding=utf-8
"""
This code was generated by
\ / _ _ _| _ _
| (_)\/(_)(_|\/| |(/_ v1.0.0
/ /
"""

import json
import re
import xml.etree.ElementTree as ET
Expand Down Expand Up @@ -79,10 +71,7 @@ def append(self, verb):

:returns: self
"""
if not isinstance(verb, TwiML):
raise TwiMLException('Only appending of TwiML is allowed')

self.verbs.append(verb)
self.nest(verb)
return self

def nest(self, verb):
Expand All @@ -93,8 +82,8 @@ def nest(self, verb):

:returns: the TwiML verb
"""
if not isinstance(verb, TwiML):
raise TwiMLException('Only nesting of TwiML is allowed')
if not isinstance(verb, TwiML) and not isinstance(verb, str):
raise TwiMLException('Only nesting of TwiML and strings are allowed')

self.verbs.append(verb)
return verb
Expand All @@ -118,7 +107,16 @@ def xml(self):

el.text = self.value

last_child = None

for verb in self.verbs:
el.append(verb.xml())
if isinstance(verb, str):
if last_child:
last_child.tail = verb
else:
el.text = verb
else:
last_child = verb.xml()
el.append(last_child)

return el
0