alerta-contrib/plugins/twilio/alerta_twilio_sms.py

43 lines
1.3 KiB
Python
Raw Permalink Normal View History

2015-02-02 00:11:14 +00:00
2016-09-15 10:16:11 +01:00
import logging
2017-09-17 15:56:55 +01:00
import os
2015-02-02 00:11:14 +00:00
from twilio.rest import TwilioRestClient
2017-09-17 15:56:55 +01:00
try:
from alerta.plugins import app # alerta >= 5.0
except ImportError:
from alerta.app import app # alerta < 5.0
2015-02-02 00:11:14 +00:00
from alerta.plugins import PluginBase
2016-09-15 10:16:11 +01:00
LOG = logging.getLogger('alerta.plugins.twilio')
2015-02-02 00:11:14 +00:00
TWILIO_ACCOUNT_SID = os.environ.get('TWILIO_ACCOUNT_SID') or app.config['TWILIO_ACCOUNT_SID']
TWILIO_AUTH_TOKEN = os.environ.get('TWILIO_AUTH_TOKEN') or app.config['TWILIO_AUTH_TOKEN']
2015-02-02 00:11:14 +00:00
TWILIO_TO_NUMBER = os.environ.get('TWILIO_TO_NUMBER') or app.config['TWILIO_TO_NUMBER']
TWILIO_FROM_NUMBER = os.environ.get('TWILIO_FROM_NUMBER') or app.config['TWILIO_FROM_NUMBER']
2015-02-02 00:11:14 +00:00
class SendSMSMessage(PluginBase):
def pre_receive(self, alert):
return alert
def post_receive(self, alert):
if alert.repeat:
return
message = "%s: %s alert for %s - %s is %s" % (
alert.environment, alert.severity.capitalize(),
','.join(alert.service), alert.resource, alert.event
)
client = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
message = client.messages.create(body=message, to=TWILIO_TO_NUMBER, from_=TWILIO_FROM_NUMBER)
LOG.info("Twilio SMS Message ID: %s", message.sid)
def status_change(self, alert, status):
return