Skip to content
Snippets Groups Projects

Add voice/SMS contacts with Twilio

Merged Leo Pound Singer requested to merge leo-singer/gracedb:twilio into master
Compare and
10 files
+ 106
11
Compare changes
  • Side-by-side
  • Inline
Files
10
+ 38
7
@@ -9,6 +9,8 @@ import json
import logging
from django_twilio.client import twilio_client
from utils import gpsToUtc
from query import filter_for_labels
@@ -24,9 +26,9 @@ if settings.USE_LVALERT_OVERSEER:
log = logging.getLogger('gracedb.alert')
def issueAlert(event, location, event_url, serialized_object=None):
def issueAlert(event, location, event_url, twiml_url, serialized_object=None):
issueXMPPAlert(event, location, serialized_object=serialized_object)
issueEmailAlert(event, event_url)
issueEmailAlert(event, event_url, twiml_url)
def indent(nindent, text):
return "\n".join([(nindent*' ')+line for line in text.split('\n')])
@@ -56,11 +58,12 @@ def issueAlertForUpdate(event, description, doxmpp, filename="", serialized_obje
# XXX No emails for this. Argh.
# The only kind of serialized object relevant for a Label is an event.
def issueAlertForLabel(event, label, doxmpp, serialized_event=None, event_url=None):
def issueAlertForLabel(event, label, doxmpp, serialized_event=None, event_url=None, twiml_url=None):
if doxmpp:
issueXMPPAlert(event, "", "label", label, serialized_event)
# Email
profileRecips = []
phoneRecips = []
pipeline = event.pipeline
# Triggers on given label matching pipeline OR with no pipeline (wildcard type)
triggers = label.trigger_set.filter(pipelines=pipeline)
@@ -76,7 +79,10 @@ def issueAlertForLabel(event, label, doxmpp, serialized_event=None, event_url=No
continue
for recip in trigger.contacts.all():
profileRecips.append(recip.email)
if recip.email:
profileRecips.append(recip.email)
if recip.phone:
phoneRecips.append(recip.phone)
if event.search:
subject = "[gracedb] %s / %s / %s / %s" % (label.name, event.pipeline.name, event.search.name, event.graceid())
@@ -104,8 +110,17 @@ def issueAlertForLabel(event, label, doxmpp, serialized_event=None, event_url=No
email = EmailMessage(subject, message, fromaddress, toaddresses, bccaddresses)
email.send()
if twiml_url is not None:
from_ = twilio_client.phone_numbers.iter().next().phone_number
for recip in phoneRecips:
try:
log.info('issueAlertForLabel: calling %s', recip)
twilio_client.calls.create(recip, from_, twiml_url, method='GET')
except:
log.exception('Failed to create call')
def issueEmailAlert(event, event_url):
def issueEmailAlert(event, event_url, twiml_url):
# The right way of doing this is to make the email alerts filter-able
# by search. But this is a low priority dev task. For now, we simply
@@ -118,6 +133,7 @@ def issueEmailAlert(event, event_url):
fromaddress = settings.ALERT_TEST_EMAIL_FROM
toaddresses = settings.ALERT_TEST_EMAIL_TO
bccaddresses = []
twilio_recips = []
else:
fromaddress = settings.ALERT_EMAIL_FROM
toaddresses = settings.ALERT_EMAIL_TO
@@ -127,15 +143,22 @@ def issueEmailAlert(event, event_url):
# See: https://bugs.ligo.org/redmine/issues/2185
#bccaddresses = settings.ALERT_EMAIL_BCC
bccaddresses = []
twilio_recips = []
pipeline = event.pipeline
triggers = pipeline.trigger_set.filter(labels=None)
for trigger in triggers:
for recip in trigger.contacts.all():
if not trigger.farThresh:
bccaddresses.append(recip.email)
if recip.email:
bccaddresses.append(recip.email)
if recip.phone:
twilio_recips.append(recip.phone)
else:
if event.far and event.far < trigger.farThresh:
bccaddresses.append(recip.email)
if recip.email:
bccaddresses.append(recip.email)
if recip.phone:
twilio_recips.append(recip.phone)
subject = "[gracedb] %s event. ID: %s" % (event.pipeline.name, event.graceid())
message = """
New Event
@@ -161,6 +184,14 @@ Event Summary:
#send_mail(subject, message, fromaddress, toaddresses)
from_ = twilio_client.phone_numbers.iter().next().phone_number
for recip in twilio_recips:
try:
log.info('issueEmailAlert: calling %s', recip)
twilio_client.calls.create(recip, from_, twiml_url, method='GET')
except:
log.exception('Failed to create call')
def issueXMPPAlert(event, location, alert_type="new", description="", serialized_object=None):
nodename = "%s_%s" % (event.group.name, event.pipeline.name)
Loading