-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.py
67 lines (47 loc) · 2.15 KB
/
middleware.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from __future__ import unicode_literals
from twilio.rest import Client
from django.core.exceptions import MiddlewareNotUsed
import os
import logging
import json
logger = logging.getLogger(__name__)
MESSAGE = """[This is a test] ALERT! It appears the server is having issues.
Exception: %s. Go to: http://newrelic.com for more details."""
NOT_CONFIGURED_MESSAGE = """Cannot initialize Twilio notification
middleware. Required enviroment variables TWILIO_ACCOUNT_SID, or
TWILIO_AUTH_TOKEN or TWILIO_NUMBER missing"""
def load_admins_file():
with open('config/administrators.json') as adminsFile:
admins = json.load(adminsFile)
return admins
def load_twilio_config():
twilio_account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
twilio_auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
twilio_number = os.environ.get('TWILIO_NUMBER')
if not all([twilio_account_sid, twilio_auth_token, twilio_number]):
logger.error(NOT_CONFIGURED_MESSAGE)
raise MiddlewareNotUsed
return (twilio_number, twilio_account_sid, twilio_auth_token)
class MessageClient(object):
def __init__(self):
(twilio_number, twilio_account_sid,
twilio_auth_token) = load_twilio_config()
self.twilio_number = twilio_number
self.twilio_client = Client(twilio_account_sid,
twilio_auth_token)
def send_message(self, body, to):
self.twilio_client.messages.create(body=body, to=to,
from_=self.twilio_number,
# media_url=['https://demo.twilio.com/owl.png'])
)
class TwilioNotificationsMiddleware(object):
def __init__(self):
self.administrators = load_admins_file()
self.client = MessageClient()
def process_exception(self, request, exception):
exception_message = str(exception)
message_to_send = MESSAGE % exception_message
for admin in self.administrators:
self.client.send_message(message_to_send, admin['phone_number'])
logger.info('Administrators notified')
return None