from flask import request from flask_restful import Resource from utils.error_handler import error_handler from app.app import app from app.api import api from munch import Munch import requests class BarkResource(Resource): def post(self): try: data = Munch.fromDict(request.get_json(silent=True)) app.logger.debug(data) config = Munch.fromDict(app.config) content = { 'title': config.title.firing if data.status == 'firing' else config.title.resolved if data.status == 'resolved' else config.title.default, 'category': 'category' } # TODO: alertmanager message to bark content. ''' { "version": "4", "groupKey": , // key identifying the group of alerts (e.g. to deduplicate) "truncatedAlerts": , // how many alerts have been truncated due to "max_alerts" "status": "", "receiver": , "groupLabels": , "commonLabels": , "commonAnnotations": , "externalURL": , // backlink to the Alertmanager. "alerts": [ { "status": "", "labels": , "annotations": , "startsAt": "", "endsAt": "", "generatorURL": , // identifies the entity that caused the alert "fingerprint": // fingerprint to identify the alert }, ... ] } ''' content.body = app.jinja_environment.get_template(config.template_file).render(data) app.logger.debug(content.body) for to in config.to: content['device_key'] = to response = requests.post( url = config.bark_api, headers = { "Content-Type": "application/json; charset=utf-8", }, data=content ) app.logger.info(response) return { 'success': True, 'message': '', 'data': {}, } except Exception as e: return error_handler(e) api.add_resource(BarkResource, '/api/send')