forked from ducksboard/twilio-zabbix-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twilio-alert.rpy
51 lines (37 loc) · 1.13 KB
/
twilio-alert.rpy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from xml.sax import saxutils
from twisted.web import resource
class AlertTwiML(resource.Resource):
isLeaf = True
HANGUP = """<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Hangup/>
</Response>
"""
SAY = """<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say voice="woman">Alert! %s</Say>
<Hangup/>
</Response>
"""
def render_GET(self, request):
output = ''
try:
message = self.extract_message(request)
except Exception, e:
output = self.HANGUP
else:
output = self.generate_say(message)
request.setHeader('Content-Type', 'application/xml')
request.setHeader('Content-Length', len(output))
return output
render_POST = render_GET
def extract_message(self, request):
subject = request.args["subject"][0]
if subject.startswith("ALERT:"):
subject = subject[len("ALERT:"):]
subject = subject.strip()
return subject
def generate_say(self, message):
return self.SAY % saxutils.escape(message)
resource = resource.Resource()
resource.putChild("", AlertTwiML())