-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.py
83 lines (71 loc) · 2.49 KB
/
slack.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
import json
import requests
def post_slack_message(webhook_url, json_message):
slack_request = requests.post(url=webhook_url, headers={'Content-type': 'application/json'},
data=json.dumps(json_message))
if slack_request.status_code == 200:
print('Successfully sent slack message')
else:
exit(f'Error[{slack_request.status_code}] sending slack message: {slack_request.text}')
def get_placement_emoji(_placement):
# Number emojis from https://www.flaticon.com/packs/numbers-0-to-100-108
# Medal emojis from https://www.flaticon.com/packs/winning-8
if _placement > 30:
return _placement
match _placement:
case 1:
return ':first_place_medal_1:'
case 2:
return ':second_place_medal_2:'
case 3:
return ':third_place_medal_3:'
case _:
return f':number-{_placement}:'
def format_message(members, join_code, leaderboard_url):
blocks = {
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Toppliste for Advent of Code Norge 2024"
}
}
]
}
if len(members) != 0:
rank = 0
previous_score = None
for index, member in enumerate(members):
current_score = member["local_score"]
if current_score != previous_score:
rank = index + 1
placement = get_placement_emoji(rank)
padded_score = f'{current_score}'.rjust(4, ' ')
padded_stars = f'{member["stars"]}'.rjust(4, ' ')
section_member = {
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": f'{placement} {member["name"]}'
},
{
"type": "mrkdwn",
"text": f'*{padded_score}* | {padded_stars} :advent_of_code_star:'
}
]
}
blocks['blocks'].append(section_member)
previous_score = current_score
section_footer = {
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": f"<{leaderboard_url}|Sjekk ut topplisten direkte> | Join-code: `{join_code}`"
}
]
}
blocks['blocks'].append(section_footer)
return blocks