def incoming(self, query_string, payload): # Note: This doesn't validate MS Teams Authorization: Bearer JWT # instead we're relying on alerta to validate X-API-Key header action = payload.get('action', 'missing') if action not in [ 'ack', 'close', 'blackout' ]: resp = make_response(jsonify(status='error', message='Invalid action'), 400) return resp if action in [ 'ack', 'close' ]: alert_id = payload.get('alert_id', None) err = make_response(jsonify(status='error', message='Missing/invalid alert_id'), 400) if not alert_id: return err try: # check that alert_id looks like uuid uuidval = UUID(alert_id, version=4) if str(uuidval) != alert_id.lower(): return err except Exception: return err alert = Alert.find_by_id(alert_id, customers=g.get('customers', None)) if not alert: return err else: alert.set_status(status=action, text='status changed via MS Teams webhook') resp = make_response(jsonify(status='ok', message='status changed'), 200) resp.headers['CARD-ACTION-STATUS'] = 'Alert {}d'.format(action.capitalize()) text = 'alert updated via msteams webhook' write_audit_trail.send(current_app._get_current_object(), event='webhook-updated', message=text, user=g.login, customers=g.customers, scopes=g.scopes, resource_id=alert.id, type='alert', request=request) elif action == 'blackout': environment = payload.get('environment', None) resource = payload.get('resource', None) event = payload.get('event', None) if environment and resource and event: duration = payload.get('duration', None) or current_app.config['BLACKOUT_DURATION'] try: if not duration or float(duration) < 0.0: # Should not happen: set default duration duration = 3600 except ValueError: # Should not happen: set default duration duration = 3600 blackout = Blackout(environment, resource=resource, event=event, duration=duration) blackout.create() resp = make_response(jsonify(status='ok', message='blackout created'), 201) resp.headers['CARD-ACTION-STATUS'] = 'Blackout created for {0:.1f} hours'.format(float(duration) / 3600) else: # Missging env, resource or event resp = make_response(jsonify(status='error', message='Missing blackout params'), 412) return resp
def telegram(): data = request.json if 'callback_query' in data: author = data['callback_query']['from'] user = "******".format(author.get('first_name'), author.get('last_name')) command, alert_id = data['callback_query']['data'].split(' ', 1) alert = Alert.find_by_id(alert_id) if not alert: jsonify(status="error", message="alert not found for Telegram message") action = command.lstrip('/') if action in ['open', 'ack', 'close']: alert.set_status(status=action, text='status change via Telegram') elif action in ['watch', 'unwatch']: alert.untag(tags=["{}:{}".format(action, user)]) elif action == 'blackout': environment, resource, event = alert.split('|', 2) blackout = Blackout(environment, resource=resource, event=event) blackout.create() send_message_reply(alert, action, user, data) return jsonify(status="ok") else: return jsonify(status="error", message="no callback_query in Telegram message"), 400
def incoming(self, path, query_string, payload): if 'callback_query' in payload: author = payload['callback_query']['from'] user = '******'.format(author.get('first_name'), author.get('last_name')) command, alert_id = payload['callback_query']['data'].split(' ', 1) customers = g.get('customers', None) alert = Alert.find_by_id(alert_id, customers=customers) action = command.lstrip('/') if not alert and action != 'blackout': return jsonify(status='error', message='alert not found for Telegram message') if action in ['open', 'ack', 'close']: alert.from_action(action, text='status change via Telegram') elif action in ['watch', 'unwatch']: alert.untag(tags=['{}:{}'.format(action, user)]) elif action == 'blackout': if alert: # new style paremeters: only alert_id environment = alert.environment resource = alert.resource event = alert.event blackout = Blackout(environment, resource=resource, event=event) blackout.create() send_message_reply(alert, action, user, payload) text = 'alert updated via telegram webhook' write_audit_trail.send(current_app._get_current_object(), event='webhook-updated', message=text, user=g.login, customers=g.customers, scopes=g.scopes, resource_id=alert.id, type='alert', request=request) return jsonify(status='ok') else: return jsonify(status='ok', message='no callback_query in Telegram message')
def telegram(): data = request.json if 'callback_query' in data: author = data['callback_query']['from'] user = '******'.format(author.get('first_name'), author.get('last_name')) command, alert_id = data['callback_query']['data'].split(' ', 1) customers = g.get('customers', None) alert = Alert.find_by_id(alert_id, customers=customers) if not alert: jsonify(status='error', message='alert not found for Telegram message') action = command.lstrip('/') if action in ['open', 'ack', 'close']: alert.set_status(status=action, text='status change via Telegram') elif action in ['watch', 'unwatch']: alert.untag(tags=['{}:{}'.format(action, user)]) elif action == 'blackout': environment, resource, event = command.split('|', 2) blackout = Blackout(environment, resource=resource, event=event) blackout.create() send_message_reply(alert, action, user, data) text = 'alert updated via telegram webhook' write_audit_trail.send(current_app._get_current_object(), event='webhook-updated', message=text, user=g.user, customers=g.customers, scopes=g.scopes, resource_id=alert.id, type='alert', request=request) return jsonify(status='ok') else: return jsonify(status='ok', message='no callback_query in Telegram message')