Add Telegram webhook integration to plugin
This commit is contained in:
parent
e4de56768d
commit
9f56d3eaac
2 changed files with 20 additions and 1 deletions
plugins/telegram
|
@ -10,6 +10,8 @@ LOG = app.logger
|
|||
|
||||
TELEGRAM_TOKEN = os.environ.get('TELEGRAM_TOKEN') or app.config['TELEGRAM_TOKEN']
|
||||
TELEGRAM_CHAT_ID = os.environ.get('TELEGRAM_CHAT_ID') or app.config['TELEGRAM_CHAT_ID']
|
||||
TELEGRAM_WEBHOOK_URL = os.environ.get('TELEGRAM_WEBHOOK_URL') or app.config.get('TELEGRAM_WEBHOOK_URL', None)
|
||||
|
||||
DASHBOARD_URL = os.environ.get('DASHBOARD_URL') or app.config.get('DASHBOARD_URL', '')
|
||||
|
||||
|
||||
|
@ -20,6 +22,10 @@ class TelegramBot(PluginBase):
|
|||
self.bot = telepot.Bot(TELEGRAM_TOKEN)
|
||||
LOG.debug('Telegram: %s', self.bot.getMe())
|
||||
|
||||
if TELEGRAM_WEBHOOK_URL:
|
||||
self.bot.setWebhook(TELEGRAM_WEBHOOK_URL)
|
||||
LOG.debug('Telegram: %s', self.bot.getWebhookInfo())
|
||||
|
||||
super(TelegramBot, self).__init__(name)
|
||||
|
||||
def pre_receive(self, alert):
|
||||
|
@ -42,8 +48,21 @@ class TelegramBot(PluginBase):
|
|||
|
||||
LOG.debug('Telegram: message=%s', text)
|
||||
|
||||
if TELEGRAM_WEBHOOK_URL:
|
||||
keyboard = {
|
||||
'inline_keyboard': [
|
||||
[
|
||||
{'text':'ack', 'callback_data': '/ack ' + alert.id},
|
||||
{'text':'close', 'callback_data': '/close ' + alert.id},
|
||||
{'text':'blackout', 'callback_data': '/blackout %s|%s|%s' % (alert.environment, alert.resource, alert.event)}
|
||||
]
|
||||
]
|
||||
}
|
||||
else:
|
||||
keyboard = None
|
||||
|
||||
try:
|
||||
r = self.bot.sendMessage(TELEGRAM_CHAT_ID, text, parse_mode='Markdown')
|
||||
r = self.bot.sendMessage(TELEGRAM_CHAT_ID, text, parse_mode='Markdown', reply_markup=keyboard)
|
||||
except Exception as e:
|
||||
raise RuntimeError("Telegram: ERROR - %s", e)
|
||||
|
||||
|
|
Binary file not shown.
Before ![]() (image error) Size: 32 KiB After ![]() (image error) Size: 40 KiB ![]() ![]() |
Loading…
Add table
Reference in a new issue