8000 Move TwiML to unique types · DataXujing/twilio-python@0af4776 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0af4776

Browse files
committed
Move TwiML to unique types
1 parent 1fcd2c6 commit 0af4776

File tree

3 files changed

+530
-0
lines changed

3 files changed

+530
-0
lines changed

twilio/twiml/__init__.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import xml.etree.ElementTree as ET
2+
3+
4+
def lower_camel(string):
5+
result = "".join([x.title() for x in string.split('_')])
6+
if not result:
7+
return result
8+
9+
return result[0].lower() + result[1:]
10+
11+
12+
class TwiML(object):
13+
"""
14+
Twilio basic verb object.
15+
"""
16+
MAP = {
17+
'from_': 'from'
18+
}
19+
20+
def __init__(self, **kwargs):
21+
self.name = self.__class__.__name__
22+
self.body = None
23+
self.verbs = []
24+
self.attrs = {}
25+
26+
for k, v in kwargs.items():
27+
if v is not None:
28+
self.attrs[lower_camel(self.MAP.get(k, k))] = v
29+
30+
def __str__(self):
31+
return self.to_xml()
32+
33+
def __enter__(self):
34+
return self
35+
36+
def __exit__(self, exc_type, exc_value, traceback):
37+
return False
38+
39+
def to_xml(self, xml_declaration=True):
40+
"""
41+
Return the contents of this verb as an XML string
42+
43+
:param bool xml_declaration: Include the XML declaration. Defaults to
44+
True
45+
"""
46+
xml = ET.tostring(self.xml()).decode('utf-8')
47+
48+
if xml_declaration:
49+
return '<?xml version="1.0" encoding="UTF-8"?>' + xml
50+
else:
51+
return xml
52+
53+
def xml(self):
54+
el = ET.Element(self.name)
55+
56+
keys = self.attrs.keys()
57+
keys = sorted(keys)
58+
for a in keys:
59+
value = self.attrs[a]
60+
61+
if isinstance(value, bool):
62+
el.set(a, str(value).lower())
63+
else:
64+
el.set(a, str(value))
65+
66+
if self.body:
67+
el.text = self.body
68+
69+
for verb in self.verbs:
70+
el.append(verb.xml())
71+
72+
return el
73+
74+
def append(self, verb):
75+
self.verbs.append(verb)
76+
return verb

twilio/twiml/messaging_response.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from twilio.twiml import TwiML
2+
3+
4+
class MessagingResponse(TwiML):
5+
6+
def __init__(self):
7+
super(MessagingResponse, self).__init__()
8+
self.name = 'Response'
9+
10+
def message(self,
11+
body,
12+
to=None,
13+
from_=None,
14+
method=None,
15+
action=None,
16+
status_callback=None,
17+
**kwargs):
18+
return self.append(Message(
19+
body=body,
20+
to=to,
21+
from_=from_,
22+
method=method,
23+
action=action,
24+
status_callback=status_callback,
25+
**kwargs
26+
))
27+
28+
def redirect(self, method=None, url=None, **kwargs):
29+
return self.append(Redirect(
30+
6D40 method=method,
31+
url=url,
32+
**kwargs
33+
))
34+
35+
36+
class Message(TwiML):
37+
38+
def __init__(self, body=None, **kwargs):
39+
super(Message, self).__init__(**kwargs)
40+
if body:
41+
self.body = body
42+
43+
def body(self, body):
44+
return self.append(Body(body))
45+
46+
def media(self, url):
47+
return self.append(Media(url))
48+
49+
50+
class Body(TwiML):
51+
def __init__(self, body):
52+
super(Body, self).__init__()
53+
self.body = body
54+
55+
56+
class Media(TwiML):
57+
def __init__(self, url):
58+
super(Media, self).__init__()
59+
self.body = url
60+
61+
62+
class Redirect(TwiML):
63+
pass
64+

0 commit comments

Comments
 (0)
0