41 lines
989 B
Python
41 lines
989 B
Python
import logging
|
|
|
|
from aiohttp import ClientSession
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
class DiscordAlert:
|
|
def __init__(self, alert_name: str, alert: dict):
|
|
self.name = alert_name
|
|
self.url = alert['url']
|
|
self.session = ClientSession()
|
|
|
|
def _make_payload(self, service, status):
|
|
serv_name = service['name']
|
|
is_up = status['status']
|
|
|
|
err = status.get('error', 'No error provided')
|
|
color = 0x00ff00 if is_up else 0xff0000
|
|
|
|
embed = {
|
|
'title': serv_name,
|
|
'color': color
|
|
}
|
|
|
|
if not is_up:
|
|
embed['description'] = err
|
|
|
|
return {
|
|
'content': '',
|
|
'embeds': [embed],
|
|
}
|
|
|
|
async def post(self, service: dict, status: dict):
|
|
payload = self._make_payload(service, status)
|
|
|
|
log.warning(f'Posting an alert! {status.get("error")}')
|
|
async with self.session.post(self.url, json=payload) as resp:
|
|
return resp
|