-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.py
53 lines (43 loc) · 1.34 KB
/
webhook.py
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
47
48
49
50
51
52
53
import requests
import os
from dataclasses import dataclass, field
from functools import partial
from typing import Dict, Any, List
@dataclass
class WebhookEmbed():
"""Represents an embed in a Discord webhook message
"""
title: str
url: str
description: str
color: int
def to_json(self) -> Dict[str, Any]:
json = {
"title": self.title,
"url": self.url,
"description": self.description,
"color": self.color
}
return json
@dataclass
class WebhookMessage():
"""Represents a Discord webhook message
"""
embeds: List[WebhookEmbed]
username: str = field(default_factory=lambda: os.environ.get('WEBHOOK_USERNAME', 'Webhook'))
avatar_url: str = field(default_factory=lambda: os.environ.get('WEBHOOK_AVATAR_URL', ''))
def to_json(self) -> Dict[str, Any]:
json = {
"username": self.username,
"avatar_url": self.avatar_url,
"embeds": [embed.to_json() for embed in self.embeds]
}
return json
def generate_message(embeds: List[WebhookEmbed]) -> WebhookMessage:
message = WebhookMessage(
embeds=embeds
)
return message
def send_message(message: WebhookMessage):
result = requests.post(os.environ["DISCORD_WEBHOOK_URL"], json=message.to_json())
return result