-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathntfy.py
195 lines (155 loc) · 5.92 KB
/
ntfy.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import requests
from time import sleep
import base64
from endpoints import SERVER_URL
from env import DEBUG, CHANNEL1, CHANNEL2
import os
import logging
if DEBUG:
CHANNEL1 = "o13srdbtydg55Sjc3h_testing"
CHANNEL2 = "xhoxnvpun6lvjj23er_debug"
def send_to_ntfy(message):
url = SERVER_URL + message['Type'].lower()
token = os.getenv('MAINSTREAM_TOKEN')
if DEBUG:
url = SERVER_URL + CHANNEL1
token = os.getenv('TESTING_TOKEN')
try:
more_headers = beautify_message(message['Subject'].strip())
headers={
"Authorization": f'Bearer {token}',
"Title": message['Title'].strip(),
"Message": encode_rfc_2047_base64(message['body']),
"Tags": more_headers['Tags'],
"Priority": more_headers['Priority'],
# "Icon": more_headers['Icon'],
}
if 'attachment' in message:
headers["Filename"] = "notice.pdf"
details = requests.put(url,
data=message['attachment'],
headers=headers
)
else:
details = requests.put(url,
headers=headers
)
details.raise_for_status()
except Exception as e:
logging.error(f" Failed to Send Notice3 : {message['Subject']} ~ {str(e)}")
message['Title'] = f" Failed to Send : {message['Title']}"
error_ntfy(message)
def schedule_ntfy(message):
url = SERVER_URL + message['Type'].lower()
token = os.getenv('MAINSTREAM_TOKEN')
if DEBUG:
url = SERVER_URL + CHANNEL1
token = os.getenv('TESTING_TOKEN')
try:
headers={
"Authorization": f'Bearer {token}',
"Title": message['Title'].strip(),
"Message": encode_rfc_2047_base64(message['body']),
"Tags": 'hourglass_flowing_sand,bell',
"Priority": '4',
'Delay': message['Delay'],
# "Icon": more_headers['Icon'],
}
if 'attachment' in message:
headers["Filename"] = "notice.pdf"
details = requests.put(url,
data=message['attachment'],
headers=headers
)
else:
details = requests.put(url,
headers=headers
)
details.raise_for_status()
except Exception as e:
logging.error(f" Failed to Send Notice3 : {message['Subject']} ~ {str(e)}")
message['Title'] = f" Failed to Schedule : {message['Title']}"
error_ntfy(message)
## NO ERROR ZONE EXPLICITY CHECK
def error_ntfy(message):
url = SERVER_URL + CHANNEL2
token = os.getenv('ERROR_TOKEN')
if DEBUG:
url = SERVER_URL + CHANNEL2
token = os.getenv('DEBUG_TOKEN')
try:
details = requests.put(f"{url}",
data="There is an error in sending the notice please check the notice board",
headers={
"Authorization": f'Bearer {token}',
"Title": encode_rfc_2047_base64(message['Title'].strip()), #remove whitespace error
"Tags": "warning,lady_beetle",
"Priority": "5",
# "Icon": "https://styles.redditmedia.com/t5_32uhe/styles/communityIcon_xnt6chtnr2j21.png",
}
)
details.raise_for_status()
except Exception as e:
logging.error(f" Failed to Send Notice4 : {message['Subject']} ~ {str(e)}")
sleep(120)
try:
details = requests.put(f"{url}",
data="There is an error in sending the notice please check the notice board",
headers={
"Authorization": f'Bearer {token}',
"Title": encode_rfc_2047_base64(message['Title'].strip()), #remove whitespace error
"Tags": "warning,lady_beetle",
"Priority": "5",
# "Icon": "https://styles.redditmedia.com/t5_32uhe/styles/communityIcon_xnt6chtnr2j21.png",
}
)
details.raise_for_status()
except Exception as e:
logging.error(f" Failed to Send Notice5 : {message['Subject']} ~ {str(e)}")
def beautify_message(subject):
headers = {}
if subject == 'PPO':
headers['Priority'] = '3'
headers['Tags'] = 'love_letter'
headers['Icon'] = '❤️'
elif subject == 'Urgent':
headers['Priority'] = '5'
headers['Tags'] = 'exclamation, stop_sign'
headers['Icon'] = '🚨'
elif subject == 'PPT/Workshop/Seminars etc':
headers['Priority'] = '2'
headers['Tags'] = 'popcorn'
headers['Icon'] = '📊'
elif subject == 'Result':
headers['Priority'] = '4'
headers['Tags'] = 'bowing_man,cup_with_straw'
headers['Icon'] = '✅'
elif subject == 'Schedule':
headers['Priority'] = '5'
headers['Tags'] = 'calendar'
headers['Icon'] = '📅'
elif subject == 'CV Submission':
headers['Priority'] = '5'
headers['Tags'] = 'briefcase'
headers['Icon'] = '📄'
elif subject == 'Shortlist':
headers['Priority'] = '5'
headers['Tags'] = 'chart_with_upwards_trend'
headers['Icon'] = '📋'
elif subject == 'Postponement':
headers['Priority'] = '5'
headers['Tags'] = 'alarm_clock,arrow_forward'
headers['Icon'] = 'ℹ️'
else:
headers['Priority'] = '5'
headers['Tags'] = 'arrow_forward'
headers['Icon'] = 'ℹ️'
return headers
def encode_rfc_2047_base64(input_string):
# Encode the input string to bytes using UTF-8 and then to Base64
base64_encoded = base64.b64encode(input_string.encode('utf-8')).decode('ascii')
# Format the RFC 2047 string
rfc2047_encoded = f'=?UTF-8?B?{base64_encoded}?='
return rfc2047_encoded
# def raise_error(message="An error occurred"):
# raise Exception(message)