Skip to content

Instantly share code, notes, and snippets.

@freeseacher
Last active September 3, 2016 15:04
Show Gist options
  • Save freeseacher/d8fa1683831dff31e882a59d78b71947 to your computer and use it in GitHub Desktop.
Save freeseacher/d8fa1683831dff31e882a59d78b71947 to your computer and use it in GitHub Desktop.
import os
from alerta.app import app
from alerta.plugins import PluginBase
import telebot
LOG = app.logger
TELEGRAM_TOKEN = os.environ.get('TELEGRAM_TOKEN') or app.config['TELEGRAM_TOKEN']
TELEGRAM_CHATID = os.environ.get('TELEGRAM_CHATID') or app.config['TELEGRAM_CHATID']
ALERTA_UI_URL = os.environ.get('ALERTA_UI_URL', 'http://try.alerta.io')
bot = telebot.TeleBot(TELEGRAM_TOKEN)
class TelegramIntegration(PluginBase):
def pre_receive(self, alert):
return alert
def post_receive(self, alert):
url="[more](%s/#/alert/%s)" % (ALERTA_UI_URL, alert.id)
if alert.repeat:
return
summary = """
*[%s] %s %s - %s on %s*:
%s
%s
""" % (
alert.status.capitalize(),
alert.environment,
alert.severity.capitalize(),
alert.event,
alert.resource,
alert.text,
url
)
LOG.debug('Telegram text: %s', summary)
try:
r = bot.send_message(TELEGRAM_CHATID, summary, parse_mode="Markdown", disable_web_page_preview=True)
except Exception as e:
raise RuntimeError("Telegram connection error: %s", e)
LOG.debug('Telegram response: %s', r)
def status_change(self, alert, status, summary):
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment