-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_receiver.py
360 lines (321 loc) · 18.1 KB
/
message_receiver.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
from os import getenv
from calendar import month_name
from multiprocessing import Process
import re
import sqlite3
import emails
import pytz
from dotenv import load_dotenv
from telethon import TelegramClient, events
import datetime as dt
# get app credentials
load_dotenv()
api_id = int(getenv('API_ID'))
api_hash = getenv('API_HASH')
SMTP_USER = getenv("SMTP_USER")
SMTP_PASS = getenv("SMTP_PASS")
# connect to database
conn = sqlite3.connect("database.db", detect_types=sqlite3.PARSE_DECLTYPES)
cursor = conn.cursor()
cursor.execute(
'''CREATE TABLE IF NOT EXISTS alarms (id INTEGER PRIMARY KEY, email TEXT UNIQUE, notif_date DATE)''')
cursor.execute(
'''CREATE TABLE IF NOT EXISTS data (id INTEGER PRIMARY KEY, slot_date DATE, updated DATETIME)''')
# define constant variables
months = {}
for m in month_name[1:]:
if len(m) > 5:
months[m] = [m.lower(), m[:3].lower()]
else:
months[m] = [m.lower()]
WORDS_TO_EXCLUDE = [
'super',
'nah',
'nai',
'not',
'grateful',
'thanks',
'gone',
'alhamdulilah',
'alhamdulillah',
'got',
"ns",
"no slot",
"er"
]
def send_mail(email, slot_date):
"""
Function for sending mails to the subscribers
"""
message = emails.html(
html=f"""<!DOCTYPE html>
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--[if mso]><xml><o:OfficeDocumentSettings><o:PixelsPerInch>96</o:PixelsPerInch><o:AllowPNG/></o:OfficeDocumentSettings></xml><![endif]-->
<style>
* {{
box-sizing: border-box;
}}
body {{
margin: 0;
padding: 0;
}}
a[x-apple-data-detectors] {{
color: inherit !important;
text-decoration: inherit !important;
}}
#MessageViewBody a {{
color: inherit;
text-decoration: none;
}}
p {{
line-height: inherit
}}
.desktop_hide,
.desktop_hide table {{
mso-hide: all;
display: none;
max-height: 0px;
overflow: hidden;
}}
.image_block img+div {{
display: none;
}}
@media (max-width:720px) {{
.social_block.desktop_hide .social-table {{
display: inline-block !important;
}}
.image_block img.big,
.row-content {{
width: 100% !important;
}}
.mobile_hide {{
display: none;
}}
.stack .column {{
width: 100%;
display: block;
}}
.mobile_hide {{
min-height: 0;
max-height: 0;
max-width: 0;
overflow: hidden;
font-size: 0px;
}}
.desktop_hide,
.desktop_hide table {{
display: table !important;
max-height: none !important;
}}
}}
</style>
</head>
<body style="background-color: #ffffff; margin: 0; padding: 0; -webkit-text-size-adjust: none; text-size-adjust: none;">
<table class="nl-container" width="100%" border="0" cellpadding="0" cellspacing="0" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; background-color: #ffffff;">
<tbody>
<tr>
<td>
<table class="row row-1" align="center" width="100%" border="0" cellpadding="0" cellspacing="0" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; background-color: #7787b5;">
<tbody>
<tr>
<td>
<table class="row-content stack" align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; color: #000000; width: 700px;" width="700">
<tbody>
<tr>
<td class="column column-1" width="100%" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; font-weight: 400; text-align: left; vertical-align: top; border-top: 0px; border-right: 0px; border-bottom: 0px; border-left: 0px;">
<div class="spacer_block" style="height:10px;line-height:10px;font-size:1px;"> </div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<table class="row row-2" align="center" width="100%" border="0" cellpadding="0" cellspacing="0" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; background-color: #e9e9f6; background-image: url('https://d1oco4z2z1fhwp.cloudfront.net/templates/default/1016/Patternvirus.png'); background-position: center top; background-repeat: repeat;">
<tbody>
<tr>
<td>
<table class="row-content stack" align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; color: #000000; width: 700px;" width="700">
<tbody>
<tr>
<td class="column column-1" width="100%" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; font-weight: 400; text-align: left; padding-bottom: 45px; padding-left: 15px; padding-right: 15px; padding-top: 35px; vertical-align: top; border-top: 0px; border-right: 0px; border-bottom: 0px; border-left: 0px;">
<table class="text_block block-1" width="100%" border="0" cellpadding="10" cellspacing="0" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; word-break: break-word;">
<tr>
<td class="pad">
<div style="font-family: sans-serif">
<div class style="font-size: 12px; font-family: Roboto, Tahoma, Verdana, Segoe, sans-serif; mso-line-height-alt: 14.399999999999999px; color: #50639c; line-height: 1.2;">
<p style="margin: 0; font-size: 12px; text-align: center; mso-line-height-alt: 14.399999999999999px;"><strong><span style="font-size:38px;">SLOT UPDATE!</span></strong></p>
</div>
</div>
</td>
</tr>
</table>
<table class="image_block block-2" width="100%" border="0" cellpadding="0" cellspacing="0" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt;">
<tr>
<td class="pad" style="width:100%;padding-right:0px;padding-left:0px;">
<div class="alignment" align="center" style="line-height:10px"><a href="https://slotbot.zubayer.one/" target="_blank" style="outline:none" tabindex="-1"><img class="big" src="https://live.staticflickr.com/65535/48937388902_8c37fac212_b.jpg" style="display: block; height: auto; border: 0; width: 670px; max-width: 100%;" width="670" alt="Image" title="Image"></a></div>
</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<table class="row row-3" align="center" width="100%" border="0" cellpadding="0" cellspacing="0" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt;">
<tbody>
<tr>
<td>
<table class="row-content stack" align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; color: #000000; width: 700px;" width="700">
<tbody>
<tr>
<td class="column column-1" width="100%" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; font-weight: 400; text-align: left; padding-bottom: 55px; padding-top: 30px; vertical-align: top; border-top: 0px; border-right: 0px; border-bottom: 0px; border-left: 0px;">
<table class="heading_block block-1" width="100%" border="0" cellpadding="0" cellspacing="0" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt;">
<tr>
<td class="pad" style="text-align:center;width:100%;">
<h1 style="margin: 0; color: #555555; direction: ltr; font-family: Roboto, Tahoma, Verdana, Segoe, sans-serif; font-size: 28px; font-weight: 700; letter-spacing: normal; line-height: 120%; text-align: center; margin-top: 0; margin-bottom: 0;"><em><span class="tinyMce-placeholder">SLOT IS AVAILABLE ON {slot_date.strftime('%d %B')}!</span></em></h1>
</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<table class="row row-4" align="center" width="100%" border="0" cellpadding="0" cellspacing="0" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; background-color: #f7f7ff;">
<tbody>
<tr>
<td>
<table class="row-content stack" align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; color: #000000; width: 700px;" width="700">
<tbody>
<tr>
<td class="column column-1" width="100%" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; font-weight: 400; text-align: left; padding-bottom: 5px; padding-left: 10px; padding-right: 10px; padding-top: 25px; vertical-align: top; border-top: 0px; border-right: 0px; border-bottom: 0px; border-left: 0px;">
<table class="social_block mobile_hide block-1" width="100%" border="0" cellpadding="10" cellspacing="0" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt;">
<tr>
<td class="pad">
<div class="alignment" align="center">
<table class="social-table" width="138.66666666666666px" border="0" cellpadding="0" cellspacing="0" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; display: inline-block;">
<tr>
<td style="padding:0 7px 0 7px;"><a href="https://instagram.com/zubayer204" target="_blank"><img src="https://app-rsrc.getbee.io/public/resources/social-networks-icon-sets/circle-blue/[email protected]" width="32" height="32" alt="Instagram" title="Instagram" style="display: block; height: auto; border: 0;"></a></td>
<td style="padding:0 7px 0 7px;"><a href="https://github.com/Zubayer204" target="_blank"><img src="https://ff5e0e4847.imgdist.com/public/users/BeeFree/beefree-964jj0dqkah/github-mark.svg" width="32.666666666666664" height="32" alt="Github" title="Github" style="display: block; height: auto; border: 0;"></a></td>
<td style="padding:0 7px 0 7px;"><a href="https://www.linkedin.com/in/zuba-the-coder/" target="_blank"><img src="https://app-rsrc.getbee.io/public/resources/social-networks-icon-sets/circle-blue/[email protected]" width="32" height="32" alt="LinkedIn" title="LinkedIn" style="display: block; height: auto; border: 0;"></a></td>
</tr>
</table>
</div>
</td>
</tr>
</table>
<div class="spacer_block" style="height:30px;line-height:30px;font-size:1px;"> </div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table><!-- End -->
</body>
</html>""",
subject=f"Slot available on {slot_date.strftime('%d %B')}",
mail_from=("US F1 SLOT BOT", "[email protected]"))
message.set_headers({'reply-to': '[email protected]', 'in-reply-to': '[email protected]'})
message.send(
to=email,
smtp={"host": "email-smtp.us-east-1.amazonaws.com",
"port": 587,
"user": SMTP_USER,
"password": SMTP_PASS,
"tls": True}
)
print("Sent mail to:", email)
def save_and_notify(slot_date, updated):
"""
Save the slot date to file and check for alarms in database
"""
# save the date in database
cursor.execute(
'''INSERT OR REPLACE INTO data (id, slot_date, updated) VALUES (?, ?, ?)''',
(1, slot_date, updated)
)
conn.commit()
# skip if the date is in next year actually
if slot_date < dt.datetime.now().date():
return
cursor.execute(
'''SELECT * FROM alarms WHERE notif_date >= ?''', (slot_date,))
results = cursor.fetchall()
proccesses = []
for row in results:
print(row)
process = Process(target=send_mail, args=(row[1], slot_date))
process.start()
proccesses.append(process)
for process in proccesses:
process.join()
def check_closeness(dates: list, month_names: list, text_string: str) -> int:
"""
Check if the date is close to the month name
"""
for date in dates:
date_ind = text_string.find(date)
if any(m in text_string[max(0, date_ind-10):date_ind+10] for m in month_names):
return int(date)
return 0
def get_date(msg):
"""
Get slot date from message
"""
text = msg.text.lower()
words_in_text = text.split(' ')
# if the message is lenghty, it's probably not a slot update
if len(words_in_text) > 20:
return False
updated = msg.date.astimezone(pytz.timezone('Asia/Dhaka'))
current_year = dt.datetime.now().year
if not (any(word in words_in_text for word in WORDS_TO_EXCLUDE) or text.endswith('?')):
for big_name, m in months.items():
if any(each in text for each in m):
dates = re.findall(r"\d{1,2}", text)
if dates:
# check if the date is close to the monthname in string
result_date = check_closeness(dates, m, text)
if result_date:
slot_datetime = dt.datetime.strptime(
f"{current_year} {result_date} {big_name}", "%Y %d %B")
save_and_notify(slot_datetime.date(), updated)
return {
"slot_date": slot_datetime.date(),
"updated": updated
}
return False
# initialize telegram client
client = TelegramClient("slot_checker", api_id, api_hash)
client.start()
# listen for new messages
@client.on(events.NewMessage(-1001403807365))
async def handle_new_message(event):
"""
Handler for new messages
"""
print(event.message.text, event.message.date)
result = get_date(event.message)
if result:
print(f"Available slot date: {result['slot_date'].strftime('%d %B')}")
print(f"Last updated at: {result['updated']}")
print("\n\n")
client.run_until_disconnected()