2016-12-18 09:07:02 +00:00
import logging
2017-09-17 14:56:55 +00:00
import os
2016-12-11 00:49:40 +00:00
2023-03-20 22:39:34 +00:00
import telepot
from alerta . plugins import PluginBase
from jinja2 import Template , UndefinedError
2017-09-17 14:56:55 +00:00
try :
from alerta . plugins import app # alerta >= 5.0
except ImportError :
from alerta . app import app # alerta < 5.0
2016-12-11 00:49:40 +00:00
2017-11-10 14:10:35 +00:00
2023-03-20 22:39:34 +00:00
DEFAULT_TMPL = r """
2017-11-10 14:10:35 +00:00
{ % if customer % } Customer : ` { { customer } } ` { % endif % }
* [ { { status . capitalize ( ) } } ] { { environment } } { { severity . capitalize ( ) } } *
{ { event | replace ( " _ " , " \ _ " ) } } { { resource . capitalize ( ) } }
` ` `
{ { text } }
` ` `
"""
2016-12-11 00:49:40 +00:00
2016-12-18 09:07:02 +00:00
LOG = logging . getLogger ( ' alerta.plugins.telegram ' )
2016-12-11 00:49:40 +00:00
2017-11-10 14:10:35 +00:00
TELEGRAM_TOKEN = app . config . get ( ' TELEGRAM_TOKEN ' ) \
2023-03-20 22:39:34 +00:00
or os . environ . get ( ' TELEGRAM_TOKEN ' )
2017-11-10 14:10:35 +00:00
TELEGRAM_CHAT_ID = app . config . get ( ' TELEGRAM_CHAT_ID ' ) \
2023-03-20 22:39:34 +00:00
or os . environ . get ( ' TELEGRAM_CHAT_ID ' )
2017-11-10 14:10:35 +00:00
TELEGRAM_WEBHOOK_URL = app . config . get ( ' TELEGRAM_WEBHOOK_URL ' , None ) \
2023-03-20 22:39:34 +00:00
or os . environ . get ( ' TELEGRAM_WEBHOOK_URL ' )
2017-11-10 14:10:35 +00:00
TELEGRAM_TEMPLATE = app . config . get ( ' TELEGRAM_TEMPLATE ' ) \
2023-03-20 22:39:34 +00:00
or os . environ . get ( ' TELEGRAM_TEMPLATE ' )
2018-09-12 13:30:53 +00:00
TELEGRAM_PROXY = app . config . get ( ' TELEGRAM_PROXY ' ) \
2023-03-20 22:39:34 +00:00
or os . environ . get ( ' TELEGRAM_PROXY ' )
2018-09-12 13:30:53 +00:00
TELEGRAM_PROXY_USERNAME = app . config . get ( ' TELEGRAM_PROXY_USERNAME ' ) \
2023-03-20 22:39:34 +00:00
or os . environ . get ( ' TELEGRAM_PROXY_USERNAME ' )
2018-09-12 13:30:53 +00:00
TELEGRAM_PROXY_PASSWORD = app . config . get ( ' TELEGRAM_PROXY_PASSWORD ' ) \
2023-03-20 22:39:34 +00:00
or os . environ . get ( ' TELEGRAM_PROXY_PASSWORD ' )
2019-07-12 15:13:24 +00:00
TELEGRAM_SOUND_NOTIFICATION_SEVERITY = app . config . get ( ' TELEGRAM_SOUND_NOTIFICATION_SEVERITY ' ) \
2023-03-20 22:39:34 +00:00
or os . environ . get ( ' TELEGRAM_SOUND_NOTIFICATION_SEVERITY ' )
2021-04-19 19:27:18 +00:00
TELEGRAM_DISABLE_NOTIFICATION_SEVERITY = app . config . get ( ' TELEGRAM_DISABLE_NOTIFICATION_SEVERITY ' ) \
2023-03-20 22:39:34 +00:00
or os . environ . get ( ' TELEGRAM_DISABLE_NOTIFICATION_SEVERITY ' )
2016-12-15 01:07:20 +00:00
2017-11-10 14:10:35 +00:00
DASHBOARD_URL = app . config . get ( ' DASHBOARD_URL ' , ' ' ) \
2023-03-20 22:39:34 +00:00
or os . environ . get ( ' DASHBOARD_URL ' )
2016-12-11 00:49:40 +00:00
2018-09-12 13:30:53 +00:00
# use all the same, but telepot.aio.api.set_proxy for async telepot
if all ( [ TELEGRAM_PROXY , TELEGRAM_PROXY_USERNAME , TELEGRAM_PROXY_PASSWORD ] ) :
telepot . api . set_proxy (
TELEGRAM_PROXY , ( TELEGRAM_PROXY_USERNAME , TELEGRAM_PROXY_PASSWORD ) )
LOG . debug ( ' Telegram: using proxy %s ' , TELEGRAM_PROXY )
elif TELEGRAM_PROXY is not None :
telepot . api . set_proxy ( TELEGRAM_PROXY )
LOG . debug ( ' Telegram: using proxy %s ' , TELEGRAM_PROXY )
2016-12-11 00:49:40 +00:00
2023-03-20 22:39:34 +00:00
2016-12-11 00:49:40 +00:00
class TelegramBot ( PluginBase ) :
def __init__ ( self , name = None ) :
self . bot = telepot . Bot ( TELEGRAM_TOKEN )
LOG . debug ( ' Telegram: %s ' , self . bot . getMe ( ) )
2017-11-10 14:10:35 +00:00
if TELEGRAM_WEBHOOK_URL and \
2023-03-20 22:39:34 +00:00
TELEGRAM_WEBHOOK_URL != self . bot . getWebhookInfo ( ) [ ' url ' ] :
2016-12-15 01:07:20 +00:00
self . bot . setWebhook ( TELEGRAM_WEBHOOK_URL )
LOG . debug ( ' Telegram: %s ' , self . bot . getWebhookInfo ( ) )
2023-03-20 22:39:34 +00:00
super ( ) . __init__ ( name )
2018-08-27 18:24:00 +00:00
if TELEGRAM_TEMPLATE :
if os . path . exists ( TELEGRAM_TEMPLATE ) :
2023-03-20 22:39:34 +00:00
with open ( TELEGRAM_TEMPLATE ) as f :
2018-08-27 18:24:00 +00:00
self . template = Template ( f . read ( ) )
else :
self . template = Template ( TELEGRAM_TEMPLATE )
2017-11-10 14:10:35 +00:00
else :
self . template = Template ( DEFAULT_TMPL )
2016-12-11 00:49:40 +00:00
def pre_receive ( self , alert ) :
return alert
def post_receive ( self , alert ) :
if alert . repeat :
return
2017-11-10 14:10:35 +00:00
try :
text = self . template . render ( alert . __dict__ )
except UndefinedError :
2023-03-20 22:39:34 +00:00
text = ' Something bad has happened but also we ' \
2017-11-10 14:10:35 +00:00
" can ' t handle your telegram template message. "
2016-12-11 00:49:40 +00:00
LOG . debug ( ' Telegram: message= %s ' , text )
2016-12-15 01:07:20 +00:00
if TELEGRAM_WEBHOOK_URL :
keyboard = {
' inline_keyboard ' : [
[
2017-11-10 14:10:35 +00:00
{ ' text ' : ' ack ' , ' callback_data ' : ' /ack ' + alert . id } ,
{ ' text ' : ' close ' , ' callback_data ' : ' /close ' + alert . id } ,
{ ' text ' : ' blackout ' ,
2020-06-10 22:38:35 +00:00
' callback_data ' : ' /blackout ' + alert . id }
2016-12-15 01:07:20 +00:00
]
]
}
else :
keyboard = None
2019-07-12 15:13:24 +00:00
if TELEGRAM_SOUND_NOTIFICATION_SEVERITY :
disable_notification = True
if alert . severity in TELEGRAM_SOUND_NOTIFICATION_SEVERITY :
disable_notification = False
else :
disable_notification = False
2021-04-19 19:27:18 +00:00
if TELEGRAM_DISABLE_NOTIFICATION_SEVERITY :
if alert . severity in TELEGRAM_DISABLE_NOTIFICATION_SEVERITY :
return
2021-10-18 21:36:32 +00:00
if alert . severity in [ ' ok ' , ' normal ' , ' cleared ' , app . config . get ( ' DEFAULT_NORMAL_SEVERITY ' ) ] and alert . previous_severity in TELEGRAM_DISABLE_NOTIFICATION_SEVERITY :
2023-03-20 22:39:34 +00:00
LOG . debug ( ' Alert severity is {} but previous_severity was {} (included in DEFAULT_NORMAL_SEVERITY list), thus it will not be forwarded to Telegram. ' . format (
alert . severity , alert . previous_severity ) )
2021-10-18 21:36:32 +00:00
return
2023-03-20 22:39:34 +00:00
LOG . debug ( ' Telegram: post_receive sendMessage disable_notification= %s ' , str (
disable_notification ) )
2019-07-12 15:13:24 +00:00
2023-03-20 22:39:34 +00:00
chat_ids = TELEGRAM_CHAT_ID . split ( ' , ' )
2021-01-06 22:20:56 +00:00
for chat_id in chat_ids :
try :
response = self . bot . sendMessage ( chat_id ,
text ,
parse_mode = ' Markdown ' ,
disable_notification = disable_notification ,
reply_markup = keyboard )
except telepot . exception . TelegramError as e :
2023-03-20 22:39:34 +00:00
raise RuntimeError ( ' Telegram (ChatId: %s ): ERROR - %s , description= %s , json= %s ' ,
chat_id ,
e . error_code ,
e . description ,
e . json )
2021-01-06 22:20:56 +00:00
except Exception as e :
2023-03-20 22:39:34 +00:00
raise RuntimeError ( ' Telegram: ERROR - %s ' , e )
2021-01-06 22:20:56 +00:00
LOG . debug ( ' Telegram (ChatId: %s ): %s ' , chat_id , response )
2016-12-11 00:49:40 +00:00
def status_change ( self , alert , status , summary ) :
return