aboutsummaryrefslogtreecommitdiffstats
path: root/app/EmailAlerter.py
blob: d4406fc260fd954664fa4439f5c29a9ec9ee941e (plain)
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
43
44
45
46
import requests
import traceback
from datetime import datetime
from app.AggroConfig import AggroConfigEmailAlerter
from dataclasses import asdict


class EmailAlerter:
    @staticmethod
    def from_config(config: AggroConfigEmailAlerter) -> "EmailAlerter":
        return EmailAlerter(**asdict(config))

    def __init__(
        self,
        api_url: str,
        api_headers: dict[str, str],
        email_from: str,
        email_to: list[str],
    ):
        self.api_url = api_url
        self.email_from = email_from
        self.email_to = email_to
        self.api_headers = api_headers

    def send_alert(self, text: str):
        try:
            now = datetime.now()
            now_text = now.strftime("%a, %d %b %Y %H:%M:%S +0000")
            data = {
                "from": self.email_from,
                "to": self.email_to,
                "subject": f"Aggro alert on {now_text}",
                "text": text,
            }
            r = requests.post(
                self.api_url,
                data=data,
                headers=self.api_headers,
            )
            if r.status_code >= 400:
                raise Exception(
                    f"[EmailAlerter] sending alert email via HTTP returned code {r.status_code} and body:\n{r.text}"
                )

        except:
            traceback.print_exc()