-
Notifications
You must be signed in to change notification settings - Fork 774
Next gen twiml #316
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
Next gen twiml #316
Changes from 1 commit
0af4776
bab5f05
159a5ad
b84ed80
041760d
c374d0b
664c1fb
ed160fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import xml.etree.ElementTree as ET | ||
|
||
|
||
def lower_camel(string): | ||
result = "".join([x.title() for x in string.split('_')]) | ||
if not result: | ||
return result | ||
|
||
return result[0].lower() + result[1:] | ||
|
||
|
||
class TwiML(object): | ||
""" | ||
Twilio basic verb object. | ||
""" | ||
MAP = { | ||
'from_': 'from' | ||
} | ||
|
||
def __init__(self, **kwargs): | ||
self.name = self.__class__.__name__ | ||
self.body = None | ||
self.verbs = [] | ||
self.attrs = {} | ||
|
||
for k, v in kwargs.items(): | ||
if v is not None: | ||
self.attrs[lower_camel(self.MAP.get(k, k))] = v | ||
|
||
def __str__(self): | ||
return self.to_xml() | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_value, traceback): | ||
return False | ||
|
||
def to_xml(self, xml_declaration=True): | ||
""" | ||
Return the contents of this verb as an XML string | ||
|
||
:param bool xml_declaration: Include the XML declaration. Defaults to | ||
True | ||
""" | ||
xml = ET.tostring(self.xml()).decode('utf-8') | ||
|
||
if xml_declaration: | ||
return '<?xml version="1.0" encoding="UTF-8"?>' + xml | ||
else: | ||
return xml | ||
|
||
def xml(self): | ||
el = ET.Element(self.name) | ||
|
||
keys = self.attrs.keys() | ||
keys = sorted(keys) | ||
for a in keys: | ||
value = self.attrs[a] | ||
|
||
if isinstance(value, bool): | ||
el.set(a, str(value).lower()) | ||
else: | ||
el.set(a, str(value)) | ||
|
||
if self.body: | ||
el.text = self.body | ||
|
||
for verb in self.verbs: | ||
el.append(verb.xml()) | ||
|
||
return el | ||
|
||
def append(self, verb): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check if its an instance of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this, it would help prevent mistakes. |
||
self.verbs.append(verb) | ||
return verb | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be nicer to return
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from twilio.twiml import TwiML | ||
|
||
|
||
class MessagingResponse(TwiML): | ||
|
||
def __init__(self): | ||
super(MessagingResponse, self).__init__() | ||
self.name = 'Response' | ||
|
||
def message(self, | ||
body, | ||
to=None, | ||
from_=None, | ||
method=None, | ||
action=None, | ||
status_callback=None, | ||
**kwargs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...ew one line per param? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fine and valid. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the other style where you keep them inline until the length limit then wrap to the next line. but not a big deal. |
||
return self.append(Message( | ||
body=body, | ||
to=to, | ||
from_=from_, | ||
method=method, | ||
action=action, | ||
status_callback=status_callback, | ||
**kwargs | ||
)) | ||
|
||
def redirect(self, method=None, url=None, **kwargs): | ||
return self.append(Redirect( | ||
method=method, | ||
url=url, | ||
**kwargs | ||
)) | ||
|
||
|
||
class Message(TwiML): | ||
|
||
def __init__(self, body=None, **kwargs): | ||
super(Message, self).__init__(**kwargs) | ||
if body: | ||
self.body = body | ||
|
||
def body(self, body): | ||
return self.append(Body(body)) | ||
|
||
def media(self, url): | ||
return self.append(Media(url)) | ||
|
||
|
||
class Body(TwiML): | ||
def __init__(self, body): | ||
super(Body, self).__init__() | ||
self.body = body | ||
|
||
|
||
class Media(TwiML): | ||
def __init__(self, url): | ||
super(Media, self).__init__() | ||
self.body = url | ||
|
||
|
||
class Redirect(TwiML): | ||
pass | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or prefix with _ if not public