-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpost_to_slack.py
executable file
·62 lines (50 loc) · 1.42 KB
/
post_to_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
import urllib.request
import json
import dotenv
import os
dotenv.load_dotenv()
slack_channel = os.getenv('slack_channel')
slack_webhook_url = os.getenv('slack_webhook_url')
log_file = "vat_abcd_crawler.log"
log_file_content = open(log_file, 'r').readlines()
reduced_content = []
info_count = 0
for line in log_file_content:
if "[INFO]" in line:
info_count += 1
else:
reduced_content.append(line)
summary = "[SUMMARY] Log contains {} lines and {}x [INFO].".format(
len(log_file_content),
info_count,
)
body = {
'channel': '#{}'.format(slack_channel),
'username': 'VAT Notifications',
'icon_emoji': ':volcano:',
'attachments': [
{
'fallback': 'VAT ABCD Importer',
'color': 'good',
'title': 'VAT ABCD Importer',
'type': 'plain_text',
'verbatim': True,
'text': summary,
},
{
'color': 'warn',
'type': 'plain_text',
'verbatim': True,
'text': "\n".join(reduced_content),
},
],
}
print(log_file_content)
print(body)
json_data = json.dumps(body)
json_data_bytes = json_data.encode('utf-8')
request = urllib.request.Request(slack_webhook_url)
request.add_header('Content-Type', 'application/json; charset=utf-8')
request.add_header('Content-Length', len(json_data_bytes))
response = urllib.request.urlopen(request, json_data_bytes)
print(response.getcode())