Initial commit: Email alerts application
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
import json
|
||||
import re
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
|
||||
def lower_camel(string):
|
||||
if not string or "_" not in string:
|
||||
return string
|
||||
|
||||
result = "".join([x.title() for x in string.split("_")])
|
||||
return result[0].lower() + result[1:]
|
||||
|
||||
|
||||
def format_language(language):
|
||||
"""
|
||||
Attempt to format language parameter as 'ww-WW'.
|
||||
|
||||
:param string language: language parameter
|
||||
"""
|
||||
if not language:
|
||||
return language
|
||||
|
||||
if not re.match("^[a-zA-Z]{2}[_-][a-zA-Z]{2}$", language):
|
||||
raise TwiMLException("Invalid value for language parameter.")
|
||||
|
||||
return language[0:2].lower() + "-" + language[3:5].upper()
|
||||
|
||||
|
||||
class TwiMLException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class TwiML(object):
|
||||
MAP = {
|
||||
"from_": "from",
|
||||
"xml_lang": "xml:lang",
|
||||
"interpret_as": "interpret-as",
|
||||
"for_": "for",
|
||||
"break_": "break",
|
||||
}
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self.name = self.__class__.__name__
|
||||
self.value = 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(), encoding="utf-8").decode("utf-8")
|
||||
return (
|
||||
'<?xml version="1.0" encoding="UTF-8"?>{}'.format(xml)
|
||||
if xml_declaration
|
||||
else xml
|
||||
)
|
||||
|
||||
def append(self, verb):
|
||||
"""
|
||||
Add a TwiML doc
|
||||
|
||||
:param verb: TwiML Document
|
||||
|
||||
:returns: self
|
||||
"""
|
||||
self.nest(verb)
|
||||
return self
|
||||
|
||||
def nest(self, verb):
|
||||
"""
|
||||
Add a TwiML doc. Unlike `append()`, this returns the created verb.
|
||||
|
||||
:param verb: TwiML Document
|
||||
|
||||
:returns: the TwiML verb
|
||||
"""
|
||||
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
|
||||
|
||||
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.value:
|
||||
if isinstance(self.value, dict):
|
||||
self.value = json.dumps(self.value)
|
||||
|
||||
el.text = self.value
|
||||
|
||||
last_child = None
|
||||
|
||||
for verb in self.verbs:
|
||||
if isinstance(verb, str):
|
||||
if last_child is not None:
|
||||
last_child.tail = verb
|
||||
else:
|
||||
el.text = verb
|
||||
else:
|
||||
last_child = verb.xml()
|
||||
el.append(last_child)
|
||||
|
||||
return el
|
||||
|
||||
def add_child(self, name, value=None, **kwargs):
|
||||
return self.nest(GenericNode(name, value, **kwargs))
|
||||
|
||||
|
||||
class GenericNode(TwiML):
|
||||
def __init__(self, name, value, **kwargs):
|
||||
super(GenericNode, self).__init__(**kwargs)
|
||||
self.name = name
|
||||
self.value = value
|
||||
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,59 @@
|
||||
# coding=utf-8
|
||||
r"""
|
||||
This code was generated by
|
||||
\ / _ _ _| _ _
|
||||
| (_)\/(_)(_|\/| |(/_ v1.0.0
|
||||
/ /
|
||||
"""
|
||||
|
||||
from twilio.twiml import (
|
||||
TwiML,
|
||||
)
|
||||
|
||||
|
||||
class FaxResponse(TwiML):
|
||||
"""<Response> TwiML for Faxes"""
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super(FaxResponse, self).__init__(**kwargs)
|
||||
self.name = "Response"
|
||||
|
||||
def receive(
|
||||
self,
|
||||
action=None,
|
||||
method=None,
|
||||
media_type=None,
|
||||
page_size=None,
|
||||
store_media=None,
|
||||
**kwargs
|
||||
):
|
||||
"""
|
||||
Create a <Receive> element
|
||||
|
||||
:param action: Receive action URL
|
||||
:param method: Receive action URL method
|
||||
:param media_type: The media type used to store media in the fax media store
|
||||
:param page_size: What size to interpret received pages as
|
||||
:param store_media: Whether or not to store received media in the fax media store
|
||||
:param kwargs: additional attributes
|
||||
|
||||
:returns: <Receive> element
|
||||
"""
|
||||
return self.nest(
|
||||
Receive(
|
||||
action=action,
|
||||
method=method,
|
||||
media_type=media_type,
|
||||
page_size=page_size,
|
||||
store_media=store_media,
|
||||
**kwargs
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class Receive(TwiML):
|
||||
"""<Receive> TwiML Verb"""
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super(Receive, self).__init__(**kwargs)
|
||||
self.name = "Receive"
|
||||
@@ -0,0 +1,125 @@
|
||||
# coding=utf-8
|
||||
r"""
|
||||
This code was generated by
|
||||
\ / _ _ _| _ _
|
||||
| (_)\/(_)(_|\/| |(/_ v1.0.0
|
||||
/ /
|
||||
"""
|
||||
|
||||
from twilio.twiml import (
|
||||
TwiML,
|
||||
)
|
||||
|
||||
|
||||
class MessagingResponse(TwiML):
|
||||
"""<Response> TwiML for Messages"""
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super(MessagingResponse, self).__init__(**kwargs)
|
||||
self.name = "Response"
|
||||
|
||||
def message(
|
||||
self,
|
||||
body=None,
|
||||
to=None,
|
||||
from_=None,
|
||||
action=None,
|
||||
method=None,
|
||||
status_callback=None,
|
||||
**kwargs
|
||||
):
|
||||
"""
|
||||
Create a <Message> element
|
||||
|
||||
:param body: Message Body
|
||||
:param to: Phone Number to send Message to
|
||||
:param from: Phone Number to send Message from
|
||||
:param action: Action URL
|
||||
:param method: Action URL Method
|
||||
:param status_callback: Status callback URL. Deprecated in favor of action.
|
||||
:param kwargs: additional attributes
|
||||
|
||||
:returns: <Message> element
|
||||
"""
|
||||
return self.nest(
|
||||
Message(
|
||||
body=body,
|
||||
to=to,
|
||||
from_=from_,
|
||||
action=action,
|
||||
method=method,
|
||||
status_callback=status_callback,
|
||||
**kwargs
|
||||
)
|
||||
)
|
||||
|
||||
def redirect(self, url, method=None, **kwargs):
|
||||
"""
|
||||
Create a <Redirect> element
|
||||
|
||||
:param url: Redirect URL
|
||||
:param method: Redirect URL method
|
||||
:param kwargs: additional attributes
|
||||
|
||||
:returns: <Redirect> element
|
||||
"""
|
||||
return self.nest(Redirect(url, method=method, **kwargs))
|
||||
|
||||
|
||||
class Redirect(TwiML):
|
||||
"""<Redirect> TwiML Verb"""
|
||||
|
||||
def __init__(self, url, **kwargs):
|
||||
super(Redirect, self).__init__(**kwargs)
|
||||
self.name = "Redirect"
|
||||
self.value = url
|
||||
|
||||
|
||||
class Message(TwiML):
|
||||
"""<Message> TwiML Verb"""
|
||||
|
||||
def __init__(self, body=None, **kwargs):
|
||||
super(Message, self).__init__(**kwargs)
|
||||
self.name = "Message"
|
||||
if body:
|
||||
self.value = body
|
||||
|
||||
def body(self, message, **kwargs):
|
||||
"""
|
||||
Create a <Body> element
|
||||
|
||||
:param message: Message Body
|
||||
:param kwargs: additional attributes
|
||||
|
||||
:returns: <Body> element
|
||||
"""
|
||||
return self.nest(Body(message, **kwargs))
|
||||
|
||||
def media(self, url, **kwargs):
|
||||
"""
|
||||
Create a <Media> element
|
||||
|
||||
:param url: Media URL
|
||||
:param kwargs: additional attributes
|
||||
|
||||
:returns: <Media> element
|
||||
"""
|
||||
return self.nest(Media(url, **kwargs))
|
||||
|
||||
|
||||
class Media(TwiML):
|
||||
"""<Media> TwiML Noun"""
|
||||
|
||||
def __init__(self, url, **kwargs):
|
||||
super(Media, self).__init__(**kwargs)
|
||||
self.name = "Media"
|
||||
self.value = url
|
||||
|
||||
|
||||
class Body(TwiML):
|
||||
"""<Body> TwiML Noun"""
|
||||
|
||||
def __init__(self, message, **kwargs):
|
||||
super(Body, self).__init__(**kwargs)
|
||||
self.name = "Body"
|
||||
self.value = message
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user