File size: 1,265 Bytes
52a5c61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# core/alerters.py
import datetime
import requests
import aiohttp

class BaseAlerter:
    def send(self, message: str):
        raise NotImplementedError("send() must be implemented.")

class ConsoleAlerter(BaseAlerter):
    def send(self, message: str):
        timestamp = datetime.datetime.now().isoformat()
        print(f"[Console Alert][{timestamp}] {message}")

class FileAlerter(BaseAlerter):
    def __init__(self, filepath="failsafe_errors.log"):
        self.filepath = filepath

    def send(self, message: str):
        timestamp = datetime.datetime.now().isoformat()
        with open(self.filepath, "a") as f:
            f.write(f"[File Alert][{timestamp}] {message}\n")

class WebhookAlerter(BaseAlerter):
    def __init__(self, url: str):
        self.url = url

    def send(self, message: str):
        # Синхронный webhook
        try:
            requests.post(self.url, json={"alert": message})
        except Exception as e:
            print("[WebhookAlert Failed]", e)

class AsyncWebhookAlerter(BaseAlerter):
    def __init__(self, url: str):
        self.url = url

    async def send(self, message: str):
        async with aiohttp.ClientSession() as session:
            await session.post(self.url, json={"alert": message})