def send_message_reply(alert: Alert, action: str, user: str, data: JSON) -> None: try: import telepot # type: ignore except ImportError as e: current_app.logger.warning("You have configured Telegram but 'telepot' client is not installed", exc_info=True) return try: bot_id = os.environ.get('TELEGRAM_TOKEN') or current_app.config.get('TELEGRAM_TOKEN') dashboard_url = os.environ.get('DASHBOARD_URL') or current_app.config.get('DASHBOARD_URL') chat_id = os.environ.get('TELEGRAM_CHAT_ID') or current_app.config.get('TELEGRAM_CHAT_ID') bot = telepot.Bot(bot_id) # message info message_id = data['callback_query']['message']['message_id'] message_log = '\n'.join(data['callback_query']['message']['text'].split('\n')[1:]) # process buttons for reply text inline_keyboard, reply = [], 'The status of alert {alert} is *{status}* now!' # type: List[List[JSON]], str actions = ['watch', 'unwatch'] if action in actions: reply = 'User `{user}` is _{status}ing_ alert {alert}' next_action = actions[(actions.index(action) + 1) % len(actions)] inline_keyboard = [ [ {'text': next_action.capitalize(), 'callback_data': '/{} {}'.format(next_action, alert.id)}, {'text': 'Ack', 'callback_data': '{} {}'.format('/ack', alert.id)}, {'text': 'Close', 'callback_data': '{} {}'.format('/close', alert.id)} ] ] # format message response alert_short_id = alert.get_id(short=True) alert_url = '{}/#/alert/{}'.format(dashboard_url, alert.id) reply = reply.format(alert=alert_short_id, status=action, user=user) message = '{alert} *{level} - {event} on {resouce}*\n{log}\n{reply}'.format( alert='[{}]({})'.format(alert_short_id, alert_url), level=alert.severity.capitalize(), event=alert.event, resouce=alert.resource, log=message_log, reply=reply) # send message bot.editMessageText( msg_identifier=(chat_id, message_id), text=message, parse_mode='Markdown', reply_markup={'inline_keyboard': inline_keyboard} ) except Exception as e: current_app.logger.warning('Error sending reply message', exc_info=True)
def build_slack_response(alert: Alert, action: str, user: str, data: ImmutableMultiDict) -> JSON: response = json.loads(data['payload']).get('original_message', {}) actions = ['watch', 'unwatch'] message = ('User {user} is {action}ing alert {alert}' if action in actions else 'The status of alert {alert} is {status} now!').format( alert=alert.get_id(short=True), status=alert.status.capitalize(), action=action, user=user) attachment_response = { 'fallback': message, 'pretext': 'Action done!', 'color': '#808080', 'title': message, 'title_link': absolute_url('/alert/' + alert.id) } # clear interactive buttons and add new attachment as response of action if action not in actions: attachments = response.get('attachments', []) for attachment in attachments: attachment.pop('actions', None) attachments.append(attachment_response) response['attachments'] = attachments return response # update the interactive button of all actions next_action = actions[(actions.index(action) + 1) % len(actions)] for attachment in response.get('attachments', []): for attached_action in attachment.get('actions', []): if action == attached_action.get('value'): attached_action.update({ 'name': next_action, 'value': next_action, 'text': next_action.capitalize() }) return response
def build_slack_response(alert: Alert, action: str, user: str, data: ImmutableMultiDict) -> JSON: response = json.loads(data['payload']).get('original_message', {}) actions = ['watch', 'unwatch'] message = ( 'User {user} is {action}ing alert {alert}' if action in actions else 'The status of alert {alert} is {status} now!').format( alert=alert.get_id(short=True), status=alert.status.capitalize(), action=action, user=user ) attachment_response = { 'fallback': message, 'pretext': 'Action done!', 'color': '#808080', 'title': message, 'title_link': absolute_url('/alert/' + alert.id) } # clear interactive buttons and add new attachment as response of action if action not in actions: attachments = response.get('attachments', []) for attachment in attachments: attachment.pop('actions', None) attachments.append(attachment_response) response['attachments'] = attachments return response # update the interactive button of all actions next_action = actions[(actions.index(action) + 1) % len(actions)] for attachment in response.get('attachments', []): for attached_action in attachment.get('actions', []): if action == attached_action.get('value'): attached_action.update({ 'name': next_action, 'value': next_action, 'text': next_action.capitalize() }) return response
def send_message_reply(alert: Alert, action: str, user: str, data: JSON) -> None: try: import telepot # type: ignore except ImportError as e: current_app.logger.warning( "You have configured Telegram but 'telepot' client is not installed", exc_info=True) return try: bot_id = os.environ.get('TELEGRAM_TOKEN') or current_app.config.get( 'TELEGRAM_TOKEN') dashboard_url = os.environ.get( 'DASHBOARD_URL') or current_app.config.get('DASHBOARD_URL') chat_id = os.environ.get('TELEGRAM_CHAT_ID') or current_app.config.get( 'TELEGRAM_CHAT_ID') bot = telepot.Bot(bot_id) # message info message_id = data['callback_query']['message']['message_id'] message_log = '\n'.join( data['callback_query']['message']['text'].split('\n')[1:]) # process buttons for reply text inline_keyboard, reply = [], 'The status of alert {alert} is *{status}* now!' # type: List[List[JSON]], str actions = ['watch', 'unwatch'] if action in actions: reply = 'User `{user}` is _{status}ing_ alert {alert}' next_action = actions[(actions.index(action) + 1) % len(actions)] inline_keyboard = [[{ 'text': next_action.capitalize(), 'callback_data': '/{} {}'.format(next_action, alert.id) }, { 'text': 'Ack', 'callback_data': '{} {}'.format('/ack', alert.id) }, { 'text': 'Close', 'callback_data': '{} {}'.format('/close', alert.id) }]] # format message response alert_short_id = alert.get_id(short=True) alert_url = '{}/#/alert/{}'.format(dashboard_url, alert.id) reply = reply.format(alert=alert_short_id, status=action, user=user) message = '{alert} *{level} - {event} on {resouce}*\n{log}\n{reply}'.format( alert='[{}]({})'.format(alert_short_id, alert_url), level=alert.severity.capitalize(), event=alert.event, resouce=alert.resource, log=message_log, reply=reply) # send message bot.editMessageText(msg_identifier=(chat_id, message_id), text=message, parse_mode='Markdown', reply_markup={'inline_keyboard': inline_keyboard}) except Exception as e: current_app.logger.warning('Error sending reply message', exc_info=True)