-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot.py
791 lines (641 loc) · 32.7 KB
/
bot.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
import telegram
from telegram.ext import Updater, MessageHandler, Filters
from telegram.ext import CommandHandler
# Uncomment if you're using built-in logging.
#import logging
import hashlib
import requests
import os
import psycopg2 as pscg
from datetime import datetime as dt
from urllib.request import Request, urlopen
from pathlib import Path
from termcolor import colored
kraljevo_path = Path.cwd().joinpath('resources').joinpath('kraljevo.jpg')
# Credentials for database connection stored in system variable.
# Visit https://www3.ntu.edu.sg/home/ehchua/programming/howto/Environment_Variables.html for more info.
DATABASE_URL = os.environ['DATABASE_URL']
CREATOR_ID = os.environ['CREATOR_ID']
# Custom console logging.
def print_log(text, data = ""):
try:
with open(Path().resolve().joinpath("log").joinpath("log.txt"), 'a') as f:
f.write(str(dt.now().strftime("%I:%M:%S %p")) + ": " + str(text) + ". " + str(data) + "\n")
except IOError:
print(colored(dt.now().strftime("%I:%M:%S %p"), 'cyan'), " ", colored(text, 'magenta'), "can not open log file")
#print(colored(dt.now().strftime("%I:%M:%S %p"), 'cyan'), " ", colored(text, 'magenta'), data)
# Bypass anti-crawler systems by using browser's "identity".
# Reads an url content and returns a hash value.
def get_url_hash(url):
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
response = urlopen(req).read()
current_hash = hashlib.sha224(response).hexdigest()
return current_hash
def is_url_valid(url):
try:
requests.get(url)
return True
except Exception:
return False
def is_url_valid_length(url):
if len(url) < 1500:
return True
else:
return False
def start(update, context):
current_chat_id = update.effective_chat.id
context.bot.send_message(chat_id=update.effective_chat.id, text="Hello! I am a bot and I will notify you "
"whenever a site changes.\n"
"Welcome!\n\nYour chat id is: "
+ str(current_chat_id))
text = "Here\'s the list of my commands:\n\n" \
"/start - See the starting message\n" \
"/follow <link(s)> - Url(s) that you\'d like to keep track of\n" \
"/unfollow <link(s)> - Url(s) that you no longer want to keep track of\n" \
"/unfollow_all - Delete all your urls from database\n" \
"/list - Info on what you follow\n" \
"/help - Bot manual\n" \
"/comment <comment> - Leave a comment for me! For safety reasons, max 30 comments per user is permitted\n" \
"/list_comments - See all your comments\n" \
"/end - Wipe all your data\n"
context.bot.send_message(chat_id=update.effective_chat.id, text=text)
def end(update, context):
current_chat_id = update.effective_chat.id
cursor = None
conn = None
# DB connection
try:
conn = pscg.connect(DATABASE_URL, sslmode='require')
print_log("Successfully connected to database!")
cursor = conn.cursor()
delete_query1 = "delete from public.user_data where chat_id = %s"
delete_query2 = "delete from public.user_comments where chat_id = %s"
try:
cursor.execute(delete_query1, [current_chat_id])
conn.commit()
context.bot.send_message(chat_id=update.effective_chat.id, text="Url data wiped.")
except pscg.Error as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="Database issues. Failed to "
"wipe url data.")
print()
print_log(e.pgcode, ": Failed to execute query.")
print_log("Query in question: ", delete_query1)
print_log("Chat_id: ", current_chat_id)
print_log("Fail message: ", e.pgerror)
print()
try:
cursor.execute(delete_query2, [current_chat_id])
conn.commit()
context.bot.send_message(chat_id=update.effective_chat.id, text="Comments data wiped.")
except pscg.Error as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="Database issues. Failed to "
"wipe comments data.")
print()
print_log(e.pgcode, ": Failed to execute query.")
print_log("Query in question: ", delete_query2)
print_log("Chat_id: ", current_chat_id)
print_log("Fail message: ", e.pgerror)
print()
except pscg.Error as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="Database issues. Failed to connect...:\n")
print_log("Failed to connect to PostgreSQL database: ", e.pgcode)
print_log("Fail message: ", e.pgerror)
finally:
if conn:
cursor.close()
conn.close()
print_log("PostgreSQL connection is closed.")
def follow(update, context):
current_chat_id = update.effective_chat.id
print_log("Follow command invoked for chat_id: ", str(current_chat_id))
# There should be at least 1 url sent together with command
if len(context.args) < 1:
context.bot.send_message(chat_id=update.effective_chat.id, text="That command requires "
"at least 1 argument.")
return
urls = context.args
conn = None
cursor = None
for url in urls:
# Check if url is in valid format and if it exists
if not is_url_valid(url):
context.bot.send_message(chat_id=update.effective_chat.id, text="Url not valid:\n" + url + "\n")
continue
# Check the length of url (1500 is upper max defined in database)
if not is_url_valid_length(url):
context.bot.send_message(chat_id=update.effective_chat.id, text="Url must be less than 1500 characters:\n"
+ url + "\n")
continue
# DB connection
try:
conn = pscg.connect(DATABASE_URL, sslmode='require')
print_log("Successfully connected to database!")
cursor = conn.cursor()
check_query = "select * from public.user_data where chat_id = %s and url = %s"
# Check if the entry already exists
try:
cursor.execute(check_query, (current_chat_id, url))
conn.commit()
# Add if not
if cursor.fetchone() is None:
insert_query = "insert into public.user_data (chat_id, url, hash) values (%s, %s, %s)"
values = (current_chat_id, url, get_url_hash(url))
try:
cursor.execute(insert_query, values)
conn.commit()
context.bot.send_message(chat_id=update.effective_chat.id, text="Successfully followed:\n"
+ url)
print_log(str(current_chat_id) + ": followed ", url)
except pscg.Error as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="Database issues. Failed "
"to make an entry for url:\n"
+ url)
print()
print_log(e.pgcode, ": Failed to execute query.")
print_log("Query in question: ", insert_query)
print_log("Chat_id: ", current_chat_id)
print_log("Fail message: ", e.pgerror)
print()
# Send a message if it does
else:
context.bot.send_message(chat_id=update.effective_chat.id, text="Entry already exists:\n"
+ url)
print_log(str(current_chat_id) + ": entry already exists ", url)
except pscg.Error as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="Database issues. Failed "
"to check if there's an entry:\n"
+ url)
print()
print_log(e.pgcode, ": Failed to execute query.")
print_log("Query in question: ", check_query)
print_log("Chat_id: ", current_chat_id)
print_log("Fail message: ", e.pgerror)
print()
except pscg.Error as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="Database issues. Failed to connect...:\n")
print_log("Failed to connect to PostgreSQL database: ", e.pgcode)
print_log("Fail message: ", e.pgerror)
finally:
if conn:
cursor.close()
conn.close()
print_log("PostgreSQL connection is closed.")
def unfollow(update, context):
current_chat_id = update.effective_chat.id
if len(context.args) < 1:
context.bot.send_message(chat_id=update.effective_chat.id, text='That command requires '
'at least 1 argument.')
return
urls = context.args
conn = None
cursor = None
for url in urls:
# Check if url is in valid format and if it exists
if not is_url_valid(url):
context.bot.send_message(chat_id=update.effective_chat.id, text="Url not valid:\n" + url + "\n")
continue
# Check the length of url (1500 is upper max defined in database)
if not is_url_valid_length(url):
context.bot.send_message(chat_id=update.effective_chat.id, text="Url must be less than 1500 characters:\n"
+ url + "\n")
continue
# DB connection
try:
conn = pscg.connect(DATABASE_URL, sslmode='require')
print_log("Successfully connected to database!")
cursor = conn.cursor()
check_query = "select * from public.user_data where chat_id = %s and url = %s"
# Check if the entry already exists
try:
cursor.execute(check_query, (current_chat_id, url))
conn.commit()
# Delete if yes
if cursor.fetchone() is not None:
delete_query = "delete from public.user_data where chat_id = %s and url = %s"
values = (current_chat_id, url)
try:
cursor.execute(delete_query, values)
conn.commit()
context.bot.send_message(chat_id=update.effective_chat.id, text="Successfully unfollowed:\n"
+ url)
print_log(str(current_chat_id) + ": unfollowed ", url)
except pscg.Error as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="Database issues. Failed "
"to delete an entry for url:\n"
+ url)
print()
print_log(e.pgcode, ": Failed to execute query.")
print_log("Query in question: ", delete_query)
print_log("Chat_id: ", current_chat_id)
print_log("Fail message: ", e.pgerror)
print()
# Send a message if it does not
else:
context.bot.send_message(chat_id=update.effective_chat.id, text="Entry does not exist:\n"
+ url)
print_log(str(current_chat_id) + ": entry does not exist ", url)
except pscg.Error as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="Database issues. Failed "
"to check if there's an entry:\n"
+ url)
print()
print_log(e.pgcode, ": Failed to execute query.")
print_log("Query in question: ", check_query)
print_log("Chat_id: ", current_chat_id)
print_log("Fail message: ", e.pgerror)
print()
except pscg.Error as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="Database issues. Failed to connect...:\n")
print_log("Failed to connect to PostgreSQL database: ", e.pgcode)
print_log("Fail message: ", e.pgerror)
finally:
if conn:
cursor.close()
conn.close()
print_log("PostgreSQL connection is closed.")
def unfollow_all(update, context):
current_chat_id = update.effective_chat.id
conn = None
cursor = None
# DB connection
try:
conn = pscg.connect(DATABASE_URL, sslmode='require')
print_log("Successfully connected to database!")
cursor = conn.cursor()
delete_query = "delete from public.user_data where chat_id = %s"
try:
cursor.execute(delete_query, [current_chat_id])
conn.commit()
context.bot.send_message(chat_id=update.effective_chat.id, text="Your list of urls is now empty.")
except pscg.Error as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="Database issues. Failed to "
"delete your data.")
print()
print_log(e.pgcode, ": Failed to execute query.")
print_log("Query in question: ", delete_query)
print_log("Chat_id: ", current_chat_id)
print_log("Fail message: ", e.pgerror)
print()
except pscg.Error as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="Database issues. Failed to connect...:\n")
print_log("Failed to connect to PostgreSQL database: ", e.pgcode)
print_log("Fail message: ", e.pgerror)
finally:
if conn:
cursor.close()
conn.close()
print_log("PostgreSQL connection is closed.")
def list_all(update, context):
current_chat_id = update.effective_chat.id
cursor = None
conn = None
# DB connection
try:
conn = pscg.connect(DATABASE_URL, sslmode='require')
print_log("Successfully connected to database!")
# Selecting all urls from current users and reporting
cursor = conn.cursor()
lookup_query = "select url from public.user_data where chat_id = %s"
try:
cursor.execute(lookup_query, [current_chat_id])
data = cursor.fetchall()
if data:
text = "Urls you follow:\n"
for url in data:
url_txt = str(url[0])
text = text + "- " + url_txt + "\n"
context.bot.send_message(chat_id=update.effective_chat.id, text=text)
else:
context.bot.send_message(chat_id=update.effective_chat.id, text="Currently you do not follow "
"anything.")
except pscg.Error as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="Database issues. Failed to "
"list your data.")
print()
print_log(e.pgcode, ": Failed to execute query.")
print_log("Query in question: ", lookup_query)
print_log("Chat_id: ", current_chat_id)
print_log("Fail message: ", e.pgerror)
print()
except pscg.Error as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="Database issues. Failed to connect...:\n")
print_log("Failed to connect to PostgreSQL database: ", e.pgcode)
print_log("Fail message: ", e.pgerror)
finally:
if conn:
cursor.close()
conn.close()
print_log("PostgreSQL connection is closed.")
def show_help(update, context):
text = "Here\'s the list of my commands:\n\n" \
"/start - See the starting message\n" \
"/follow <link(s)> - Url(s) that you\'d like to keep track of\n" \
"/unfollow <link(s)> - Url(s) that you no longer want to keep track of\n" \
"/unfollow_all - Delete all your urls from database\n" \
"/list - Info on what you follow\n" \
"/help - Bot manual\n" \
"/comment <comment> - Leave a comment for me! For safety reasons, max 30 comments per user is permitted\n" \
"/list_comments - See all your comments\n" \
"/end - Wipe all your data\n"
context.bot.send_message(chat_id=update.effective_chat.id, text=text)
def comment(update, context):
current_chat_id = update.effective_chat.id
conn = None
cursor = None
# If text of comment is empty no point in sending anything
if len(context.args) < 1:
context.bot.send_message(chat_id=update.effective_chat.id, text='That command requires '
'at least 1 argument.')
return
# Send creator a message; creator chat id is a system variable
comment_text = " ".join(context.args)
context.bot.send_message(chat_id=CREATOR_ID, text="New comment arrived:\n\n" + comment_text)
context.bot.send_message(chat_id=update.effective_chat.id, text="Successfully sent a comment to the creator.")
# If user did not flood the database, make an entry for his comment,
# and if possible, update the name and username
try:
conn = pscg.connect(DATABASE_URL, sslmode='require')
print_log("Successfully connected to database!")
cursor = conn.cursor()
check_query = "select * from public.user_comments where chat_id = %s"
try:
cursor.execute(check_query, [current_chat_id])
conn.commit()
data = cursor.fetchall()
if len(data) < 30:
insert_query = "insert into public.user_comments (chat_id, comment_text) values (%s, %s)"
values = [current_chat_id, comment_text]
try:
cursor.execute(insert_query, values)
conn.commit()
print_log(str(current_chat_id) + ": User sent a comment.")
username_query = "update public.user_comments set username = %s " \
"where chat_id = %s and comment_text = %s"
name_query = "update public.user_comments set first_name = %s " \
"where chat_id = %s and comment_text = %s"
username = update.effective_chat.username
name = update.effective_chat.first_name
if username:
try:
cursor.execute(username_query, [username, current_chat_id, comment_text])
conn.commit()
print_log(str(current_chat_id) + ": Username updated.")
except pscg.Error as e:
print()
print_log(e.pgcode, ": Failed to execute query.")
print_log("Query in question: ", username_query)
print_log("Chat_id: ", current_chat_id)
print_log("Fail message: ", e.pgerror)
print()
if name:
try:
cursor.execute(name_query, [name, current_chat_id, comment_text])
conn.commit()
print_log(str(current_chat_id) + ": Name updated.")
except pscg.Error as e:
print()
print_log(e.pgcode, ": Failed to execute query.")
print_log("Query in question: ", name_query)
print_log("Chat_id: ", current_chat_id)
print_log("Fail message: ", e.pgerror)
print()
except pscg.Error as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="Database issues. Failed "
"to save a comment... "
"But the creator still got "
"it as a message, don't worry.\n")
print()
print_log(e.pgcode, ": Failed to execute query.")
print_log("Query in question: ", insert_query)
print_log("Chat_id: ", current_chat_id)
print_log("Fail message: ", e.pgerror)
print()
else:
context.bot.send_message(chat_id=update.effective_chat.id, text="You've sent too many comments "
"already... Wait for the "
"automatic cleanup, it might take a "
"few days.")
print_log(str(current_chat_id) + ": user maximized number of comments.")
except pscg.Error as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="Database issues. Failed "
"to do a checkup...")
print()
print_log(e.pgcode, ": Failed to execute query.")
print_log("Query in question: ", check_query)
print_log("Chat_id: ", current_chat_id)
print_log("Fail message: ", e.pgerror)
print()
except pscg.Error as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="Database issues. Failed to connect...:\n")
print_log("Failed to connect to PostgreSQL database: ", e.pgcode)
print_log("Fail message: ", e.pgerror)
finally:
if conn:
cursor.close()
conn.close()
print_log("PostgreSQL connection is closed.")
def list_comments(update, context):
current_chat_id = update.effective_chat.id
cursor = None
conn = None
# DB connection
try:
conn = pscg.connect(DATABASE_URL, sslmode='require')
print_log("Successfully connected to database!")
# Selecting all urls from current users and reporting
cursor = conn.cursor()
lookup_query = "select comment_id, comment_text from public.user_comments where chat_id = %s"
try:
cursor.execute(lookup_query, [current_chat_id])
data = cursor.fetchall()
if data:
text = "Your comments:\n\n-----\n"
for comment_id, comment_text in data:
text = text + "Comment id: " + str(comment_id) + "\n" +\
"Comment text:\n" + str(comment_text) + "\n-----\n"
context.bot.send_message(chat_id=update.effective_chat.id, text=text)
else:
context.bot.send_message(chat_id=update.effective_chat.id, text="You've got no comments.")
except pscg.Error as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="Database issues. Failed to "
"list your data.")
print()
print_log(e.pgcode, ": Failed to execute query.")
print_log("Query in question: ", lookup_query)
print_log("Chat_id: ", current_chat_id)
print_log("Fail message: ", e.pgerror)
print()
except pscg.Error as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="Database issues. Failed to connect...:\n")
print_log("Failed to connect to PostgreSQL database: ", e.pgcode)
print_log("Fail message: ", e.pgerror)
finally:
if conn:
cursor.close()
conn.close()
print_log("PostgreSQL connection is closed.")
def kraljevo(update, context):
global kraljevo_path
context.bot.send_photo(chat_id=update.effective_chat.id, photo=open(kraljevo_path, 'rb'))
def send_a_message_to_users(update, context):
sender_chat_id = chat_id=update.effective_chat.id
cursor = None
conn = None
if sender_chat_id != CREATOR_ID:
context.bot.send_message(chat_id=update.effective_chat.id, text='You are not allowed to use that command. :)')
return
# Message has to have a text
if len(context.args) < 1:
context.bot.send_message(chat_id=update.effective_chat.id, text='That command requires '
'at least 1 argument.')
return
# Send creator a message; creator chat id is a system variable
message_text = context.args.join(" ")
try:
conn = pscg.connect(DATABASE_URL, sslmode='require')
print_log("Successfully connected to database!")
# Selecting all chat ids from database
cursor = conn.cursor()
lookup_query = "select distinct chat_id from public.user_data"
try:
cursor.execute(lookup_query)
conn.commit()
data = cursor.fetchall()
if data:
for chat_id in data:
if chat_id != CREATOR_ID:
print_log("Sending a message to: ", chat_id)
context.bot.send_message(chat_id=chat_id, text=message_text)
except pscg.Error as e:
print()
print_log(e.pgcode, ": Failed to execute query.")
print_log("Query in question: ", lookup_query)
print_log("Chat_id: ", chat_id)
print_log("Fail message: ", e.pgerror)
print()
except pscg.Error as e:
print_log("Failed to connect to PostgreSQL database: ", e.pgcode)
print_log("Fail message: ", e.pgerror)
finally:
if conn:
cursor.close()
conn.close()
print_log("PostgreSQL connection is closed.")
def unknown(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="Unknown command!\n")
def callback_minute(context: telegram.ext.CallbackContext):
cursor = None
conn = None
try:
conn = pscg.connect(DATABASE_URL, sslmode='require')
print_log("Successfully connected to database!")
# Selecting all urls from current users and report if hash has changed
# since the last callback
cursor = conn.cursor()
lookup_query = "select chat_id, url, hash from public.user_data"
try:
cursor.execute(lookup_query)
conn.commit()
data = cursor.fetchall()
update_query = "update public.user_Data set hash = %s where chat_id = %s and url = %s"
if data:
for chat_id, url, hash_code in data:
print_log("Monitoring url for chat: ", chat_id)
print_log("url: ", url)
hash_new = get_url_hash(url)
if hash_code != hash_new:
try:
cursor.execute(update_query, [hash_new, chat_id, url])
conn.commit()
print_log("Change noted for user %s: " % str(chat_id), url)
text = "The content of the following site has changed:\n" + url
context.bot.send_message(chat_id=chat_id, text=text)
except pscg.Error as e:
print()
print_log(e.pgcode, ": Failed to execute query.")
print_log("Query in question: ", update_query)
print_log("Chat_id: ", chat_id)
print_log("Fail message: ", e.pgerror)
print()
except pscg.Error as e:
print()
print_log(e.pgcode, ": Failed to execute query.")
print_log("Query in question: ", lookup_query)
print_log("Fail message: ", e.pgerror)
print()
except pscg.Error as e:
context.bot.send_message(chat_id=CREATOR_ID, text="Database issues. Failed to connect...:\n")
print_log("Failed to connect to PostgreSQL database: ", e.pgcode)
print_log("Fail message: ", e.pgerror)
finally:
if conn:
cursor.close()
conn.close()
print_log("PostgreSQL connection is closed.")
def callback_10_days(context: telegram.ext.CallbackContext):
cursor = None
conn = None
try:
conn = pscg.connect(DATABASE_URL, sslmode='require')
print_log("Successfully connected to database!")
# Delete all comments
cursor = conn.cursor()
refresh_query = "delete from public.user_comments"
try:
cursor.execute(refresh_query)
conn.commit()
context.bot.send_message(chat_id=CREATOR_ID, text="I have refreshed the comments database.")
print_log("Comments database refreshed.")
except pscg.Error as e:
print()
context.bot.send_message(chat_id=CREATOR_ID, text="Failed to refresh the comments database...")
print_log(e.pgcode, ": Failed to execute query.")
print_log("Query in question: ", refresh_query)
print_log("Fail message: ", e.pgerror)
print()
except pscg.Error as e:
context.bot.send_message(chat_id=CREATOR_ID, text="Database issues. Failed to connect...:\n")
print_log("Failed to connect to PostgreSQL database: ", e.pgcode)
print_log("Fail message: ", e.pgerror)
finally:
if conn:
cursor.close()
conn.close()
print_log("PostgreSQL connection is closed.")
TOKEN = os.environ['TOKEN']
PORT = int(os.environ.get('PORT', '8443'))
updater = Updater(token=TOKEN, use_context=True)
job_queuer = updater.job_queue
updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=TOKEN)
updater.bot.setWebhook('https://avro-bot.herokuapp.com/' + TOKEN)
dispatcher = updater.dispatcher
# Uncomment this if you'd like integrated logging
# logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
start_handler = CommandHandler('start', start, run_async=True)
follow_handler = CommandHandler('follow', follow, run_async=True)
unfollow_handler = CommandHandler('unfollow', unfollow, run_async=True)
unfollow_all_handler = CommandHandler('unfollow_all', unfollow_all, run_async=True)
list_handler = CommandHandler('list', list_all)
help_handler = CommandHandler('help', show_help)
kraljevo_handler = CommandHandler('kraljevo', kraljevo)
comment_handler = CommandHandler('comment', comment)
list_comments_handler = CommandHandler('list_comments', list_comments)
send_a_message_to_users_handler = CommandHandler('send_a_message_to_users', send_a_message_to_users)
end_handler = CommandHandler('end', end, run_async=True)
unknown_handler = MessageHandler(Filters.command, unknown)
dispatcher.add_handler(kraljevo_handler)
dispatcher.add_handler(start_handler)
dispatcher.add_handler(follow_handler)
dispatcher.add_handler(unfollow_handler)
dispatcher.add_handler(unfollow_all_handler)
dispatcher.add_handler(list_handler)
dispatcher.add_handler(help_handler)
dispatcher.add_handler(end_handler)
dispatcher.add_handler(comment_handler)
dispatcher.add_handler(list_comments_handler)
dispatcher.add_handler(send_a_message_to_users_handler)
dispatcher.add_handler(unknown_handler)
job_queuer.run_repeating(callback_minute, interval=30, first=0)
job_queuer.run_repeating(callback_10_days, interval=432000, first=432000)
updater.start_polling()
updater.idle()