Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gehadshaat committed Jun 17, 2017
1 parent c5e84ee commit 3359032
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 2 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Gehad Shaat
Copyright (c) 2017 WavyCloud LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# bandwidth_bxml
Bandwidth BXML Generator
Bandwidth BXML Generator for Python

Pure python implmenetation of Bandwidth BXML generator that works without needing to install lxml. This library works on AWS Lambda without needing to compile lxml.

175 changes: 175 additions & 0 deletions bandwidth_bxml/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import abc

from xml.etree.ElementTree import Element, tostring
import xml.dom.minidom


class Bxml(object):
__metaclass__ = abc.ABCMeta
root = None
gender = "female"
voice = "susan"
locale = "en_US"
nestable_verbs = ()

def __init__(self, text=None, attrib=None, **kwargs):
attrib = attrib or {}
attrib.update(kwargs)
attrib = {k: v for k, v in attrib.items() if v is not None}
self.root = Element(self.classname, attrib=attrib)
if text:
self.root.text = text

def append(self, element):
assert not self.nestable_verbs or isinstance(element, self.nestable_verbs), "Cannot add {} verb to {}".format(
element.classname, self.classname)
root = element.root
self.root.append(root)
return element

def to_string(self, pretty=False):
response_str = '<?xml version="1.0" encoding="UTF-8"?>'
response_str += tostring(self.root)
# response_str = response_str.replace('requestURL', 'requestUrl')

if pretty:
xml_parsed = xml.dom.minidom.parseString(response_str) # or xml.dom.minidom.parseString(xml_string)
response_str = xml_parsed.toprettyxml()

return response_str

@property
def classname(self):
return self.__class__.__name__

def speak(self, text, gender=None, voice=None, locale=None, **kwargs):
""":rtype : SpeakSentence"""
gender = gender or self.gender
voice = voice or self.voice
locale = locale or self.locale

return self.append(SpeakSentence(text, gender, voice, locale=locale, **kwargs))

def play(self, url, digits=None, **kwargs):
""":rtype : PlayAudio"""
return self.append(PlayAudio(url, digits=digits, **kwargs))

def hangup(self):
""":rtype : Hangup"""
return self.append(Hangup())

def record(self, requestUrl=None, requestUrlTimeout=None, fileFormat=None, terminatingDigits=None,
maxDuration=None, transcribe=None, transcribeCallbackUrl=None, **kwargs):
""":rtype : Record"""
return self.append(Record(requestUrl=requestUrl,
requestUrlTimeout=requestUrlTimeout,
fileFormat=fileFormat,
terminatingDigits=terminatingDigits,
maxDuration=maxDuration,
transcribe=transcribe,
transcribeCallbackUrl=transcribeCallbackUrl,
**kwargs))

def transfer(self, transferTo, transferCallerId, callTimeout=None, requestUrl=None, requestUrlTimeout=None,
tag=None, **kwargs):
""":rtype : Transfer"""
return self.append(Transfer(transferTo=transferTo, transferCallerId=transferCallerId,
callTimeout=callTimeout, requestUrl=requestUrl,
requestUrlTimeout=requestUrlTimeout, tag=tag, **kwargs))

def gather(self, requestUrl, requestUrlTimeout=None, terminatingDigits=None, maxDigits=None,
interDigitTimeout=None, bargeable=None, **kwargs):
""":rtype : Gather"""
return self.append(Gather(requestUrl=requestUrl,
requestUrlTimeout=requestUrlTimeout,
terminatingDigits=terminatingDigits,
maxDigits=maxDigits,
interDigitTimeout=interDigitTimeout,
bargeable=bargeable,
**kwargs))

def redirect(self, requestUrl, requestUrlTimeout, context=None, **kwargs):
""":rtype : Redirect"""
return self.append(
Redirect(requestUrl=requestUrl, requestUrlTimeout=requestUrlTimeout, context=context, **kwargs))


class Response(Bxml):
pass


class Pause(Bxml):
def __init__(self, duration):
super(Pause, self).__init__(duration=duration)


class SpeakSentence(Bxml):
def __init__(self, text, gender=None, voice=None, locale=None, **kwargs):
gender = gender or self.gender
voice = voice or self.voice
locale = locale or self.locale
super(SpeakSentence, self).__init__(text=text, gender=gender, voice=voice, locale=locale, **kwargs)


class PlayAudio(Bxml):
def __init__(self, url, digits=None, **kwargs):
super(PlayAudio, self).__init__(text=url, digits=digits, **kwargs)


class Hangup(Bxml):
pass


class PhoneNumber(Bxml):
def __init__(self, phone_number):
super(PhoneNumber, self).__init__(text=phone_number)


class Redirect(Bxml):
def __init__(self, requestUrl, requestUrlTimeout, context=None, **kwargs):
super(Redirect, self).__init__(requestUrl=requestUrl, requestUrlTimeout=requestUrlTimeout, context=context,
**kwargs)


class Record(Bxml):
def __init__(self, requestUrl=None, requestUrlTimeout=None, fileFormat=None, terminatingDigits=None,
maxDuration=None, transcribe=None, transcribeCallbackUrl=None, **kwargs):
super(Record, self).__init__(requestUrl=requestUrl,
requestUrlTimeout=requestUrlTimeout,
fileFormat=fileFormat,
terminatingDigits=terminatingDigits,
maxDuration=maxDuration,
transcribe=transcribe,
transcribeCallbackUrl=transcribeCallbackUrl,
**kwargs)


class Transfer(Bxml):
nestable_verbs = (PlayAudio, SpeakSentence, Record, PhoneNumber)

def __init__(self, transferTo, transferCallerId, callTimeout=None, requestUrl=None, requestUrlTimeout=None,
tag=None, **kwargs):
if isinstance(transferTo, (list)):
super(Transfer, self).__init__(transferCallerId=transferCallerId, callTimeout=callTimeout,
requestUrl=requestUrl, requestUrlTimeout=requestUrlTimeout, tag=tag,
**kwargs)
for phone_number in transferTo:
self.append(PhoneNumber(phone_number))
else:
super(Transfer, self).__init__(transferTo=transferTo, transferCallerId=transferCallerId,
callTimeout=callTimeout, requestUrl=requestUrl,
requestUrlTimeout=requestUrlTimeout, tag=tag, **kwargs)


class Gather(Bxml):
nestable_verbs = (PlayAudio, SpeakSentence)

def __init__(self, requestUrl, requestUrlTimeout=None, terminatingDigits=None, maxDigits=None,
interDigitTimeout=None, bargeable=None, **kwargs):
super(Gather, self).__init__(requestUrl=requestUrl,
requestUrlTimeout=requestUrlTimeout,
terminatingDigits=terminatingDigits,
maxDigits=maxDigits,
interDigitTimeout=interDigitTimeout,
bargeable=bargeable,
**kwargs)
12 changes: 12 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from distutils.core import setup

setup(
name='bandwidth_bxml',
version='0.1',
packages=[''],
url='https://github.com/wavycloud/bandwidth_bxml',
license='',
author='WavyCloud',
author_email='[email protected]',
description=''
)

0 comments on commit 3359032

Please sign in to comment.