-
Notifications
You must be signed in to change notification settings - Fork 4
/
discordWebhooks.py
112 lines (100 loc) · 4.07 KB
/
discordWebhooks.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#coding: utf-8
import json
import requests
class Webhook():
def __init__(self, url, content, username="", icon_url=""):
"""
Initialize a Discord Webhook object.
@param {String} url - The webhook url where to make requests.
@param {String} content - TODO: Document this variable usage.
@param {String} username - The username to use while sending data to the webhook, may be left blank.
@param {String} icon_url - An icon url, may be left blank.
"""
self.url = url if "/slack" in url else url + "/slack"
self.content = content
self.username = username
self.icon_url = icon_url
self.formated = ""
self.attachments = []
def addAttachment(self, attachment):
"""
Add a specified Attachment to self.attachments for later usage.
@param {Attachment} attachment - The Attachment object to append.
"""
if isinstance(attachment, Attachment):
self.attachments.append(attachment)
else:
raise Exception("The attachment is not a correct attachment object")
def format(self):
"""
Format the current object as a valid JSON object.
"""
data = {}
data["username"] = self.username
data["text"] = self.content
data["icon_url"] = self.icon_url
data["attachments"] = []
for attachment in self.attachments:
att = {}
att["author_name"] = attachment.author_name
att["author_icon"] = attachment.author_icon
att["color"] = attachment.color
att["pretext"] = attachment.pretext
att["title"] = attachment.title
att["title_link"] = attachment.title_link
att["image_url"] = attachment.image_url
att["footer"] = attachment.footer
att["footer_icon"] = attachment.footer_icon
att["ts"] = attachment.ts
att["fields"] = []
for field in attachment.fields:
f = {}
f["title"] = field.title
f["value"] = field.value
f["short"] = field.short
att["fields"].append(f)
data["attachments"].append(att)
self.formated = json.dumps(data)
def post(self):
"""
Send the JSON formated object to the specified `self.url`.
"""
self.format()
result = requests.post(self.url, data=self.formated)
return result
class Attachment(classmethod):
def __init__(self, **args):
"""
Initialize an Attachment object and fill the properties from given $args.
"""
self.author_name = args["author_name"] if "author_name" in args else ""
self.author_icon = args["author_icon"] if "author_icon" in args else ""
self.color = args["color"] if "color" in args else ""
self.pretext = args["pretext"] if "pretext" in args else ""
self.title = args["title"] if "title" in args else ""
self.title_link = args["title_link"] if "title_link" in args else ""
self.image_url = args["image_url"] if "image_url" in args else ""
self.footer = args["footer"] if "footer" in args else ""
self.footer_icon = args["footer_icon"] if "footer_icon" in args else ""
self.ts = args["ts"] if "ts" in args else 0
self.fields = []
def addField(self, field):
"""
Add a field to the current Attachment object.
@param {Fields} field - The field object to add to this attachment.
"""
if isinstance(field, Field):
self.fields.append(field)
else:
raise Exception("The field is not a correct field object")
class Field():
def __init__(self, title="", value="", short=False):
"""
Initialize a Field object.
@param {String} title - The field title (aka. key).
@param {String} value - The field value.
@param {Boolean} short - TODO: Document usage of this variable.
"""
self.title = title
self.value = value
self.short = short