8000 Allow adding text to TwiML nodes (#422) · BioComputing/twilio-python@e4e6ae3 · GitHub
[go: up one dir, main page]

Skip to content

Commit e4e6ae3

Browse files
authored
Allow adding text to TwiML nodes (twilio#422)
* Allow appending plain text to twiml nodes * add test for voice * missing newlines!
1 parent de2c2c8 commit e4e6ae3

File tree

4 files changed

+58
-16
lines changed

4 files changed

+58
-16
lines changed

tests/unit/twiml/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def strip(self, xml):
1818
@raises(TwiMLException)
1919
def test_append_fail(self):
2020
t = TwiML()
21-
t.append('foobar')
21+
t.append(12345)
2222

2323
def test_format_language_none(self):
2424
language = None

tests/unit/twiml/test_messaging_response.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,25 @@ def test_redirect(self):
8888
self.strip(r),
8989
'<?xml version="1.0" encoding="UTF-8"?><Response><Redirect>example.com</Redirect></Response>'
9090
)
91+
92+
93+
class TestText(TwilioTest):
94+
def test_text(self):
95+
r = MessagingResponse()
96+
r.append('No tags!')
97+
98+
assert_equal(
99+
self.strip(r),
100+
'<?xml version="1.0" encoding="UTF-8"?><Response>No tags!</Response>'
101+
)
102+
103+
def text_mixed(self):
104+
r = MessagingResponse()
105+
r.append('before')
106+
r.append(Body('Content'))
107+
r.append('after')
108+
109+
assert_equal(
110+
self.strip(r),
111+
'<?xml version="1.0" encoding="UTF-8"?><Response>before<Body>Content</Body>after</Response>'
112+
)

tests/unit/twiml/test_voice_response.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,3 +602,25 @@ def test_nested_say_play_pause(self):
602602
self.strip(r),
603603
'<?xml version="1.0" encoding="UTF-8"?><Response><Gather><Say>Hey</Say><Play>hey.mp3</Play><Pause /></Gather></Response>'
604604
)
605+
606+
607+
class TestText(TwilioTest):
608+
def test_text(self):
609+
r = VoiceResponse()
610+
r.append('No tags!')
611+
612+
assert_equal(
613+
self.strip(r),
614+
'<?xml version="1.0" encoding="UTF-8"?><Response>No tags!</Response>'
615+
)
616+
617+
def text_mixed(self):
618+
r = VoiceResponse()
619+
r.append('before')
620+
r.say('Content')
621+
r.append('after')
622+
623+
assert_equal(
624+
self.strip(r),
625+
'<?xml version="1.0" encoding="UTF-8"?><Response>before<Say>Content</Say>after</Response>'
626+
)

twilio/twiml/__init__.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
# coding=utf-8
2-
"""
3-
This code was generated by
4-
\ / _ _ _| _ _
5-
| (_)\/(_)(_|\/| |(/_ v1.0.0
6-
/ /
7-
"""
8-
91
import json
102
import re
113
import xml.etree.ElementTree as ET
@@ -79,10 +71,7 @@ def append(self, verb):
7971
8072
:returns: self
8173
"""
82-
if not isinstance(verb, TwiML):
83-
raise TwiMLException('Only appending of TwiML is allowed')
84-
85-
self.verbs.append(verb)
74+
self.nest(verb)
8675
return self
8776

8877
def nest(self, verb):
@@ -93,8 +82,8 @@ def nest(self, verb):
9382
9483
:returns: the TwiML verb
9584
"""
96-
if not isinstance(verb, TwiML):
97-
raise TwiMLException('Only nesting of TwiML is allowed')
85+
if not isinstance(verb, TwiML) and not isinstance(verb, str):
86+
raise TwiMLException('Only nesting of TwiML and strings are allowed')
9887

9988
self.verbs.append(verb)
10089
return verb
@@ -118,7 +107,16 @@ def xml(self):
118107

119108
el.text = self.value
120109

110+
last_child = None
111+
121112
for verb in self.verbs:
122-
el.append(verb.xml())
113+
if isinstance(verb, str):
114+
if last_child:
115+
last_child.tail = verb
116+
else:
117+
el.text = verb
118+
else:
119+
last_child = verb.xml()
120+
el.append(last_child)
123121

124122
return el

0 commit comments

Comments
 (0)
0