-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
76 lines (62 loc) · 1.95 KB
/
main.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
from uuid import uuid4
import urllib.request
import json
from slackblocks import (
WebhookMessage,
SectionBlock,
Button,
ContextBlock,
Image,
Text,
ResponseType,
)
from actions import GITHUB, PARAMS
def main():
title = f"*{GITHUB.name.upper()}*: new version _<{GITHUB.url}|{GITHUB.version}>_ was released!"
preview = f"{GITHUB.name.upper()}: new version {GITHUB.version} was released by {GITHUB.user_name}!"
message = WebhookMessage(
response_type=ResponseType.IN_CHANNEL,
text=preview,
blocks=[
SectionBlock(
text=title,
accessory=Button(
text=f":{PARAMS.button_emoji}: {PARAMS.button_text}",
url=PARAMS.button_url,
action_id=str(uuid4()),
),
),
ContextBlock(
elements=[
Image(
image_url=GITHUB.user_image_url,
),
Text(
text=f"by {GITHUB.user_name}",
),
]
),
],
)
send(PARAMS.slack_webhook_url, message)
def send(url: str, message: WebhookMessage):
data = message.json().encode("utf-8")
req = urllib.request.Request(
url,
data=data,
headers={
"Content-Type": "application/json",
},
)
try:
with urllib.request.urlopen(req) as response:
status_code = response.getcode()
if status_code != 200:
response = response.read().decode("utf-8")
raise Exception(f"Failed to send message to Slack > {status_code} {response}")
else:
print(f"Message was sent successfully > {message.text}")
except urllib.error.HTTPError as e:
raise Exception(f"Failed to send message to Slack > {e.code} {e.reason}")
if __name__ == "__main__":
main()