-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
1556 lines (1122 loc) · 52.4 KB
/
main.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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Telegram bible_verses_bot
# References:
# Pyrogram's documentation: https://docs.pyrogram.org/
# Pyrogram's pdf documentation: https://buildmedia.readthedocs.org/media/pdf/pyrogram/stable/pyrogram.pdf
# Conversations in pyrogram: https://nippycodes.com/coding/conversations-in-pyrogram-no-extra-package-needed/
import asyncio
import datetime
import logging
import os
import re
import threading
import time
import traceback
from typing import Callable, Dict, List, Optional, Tuple, Union
import httpx
from bs4 import BeautifulSoup
from telebot import TeleBot, types
from telebot.apihelper import ApiTelegramException
from verse_match import VerseMatch
import keep_alive
import regexes
import utils
from bible_versions import (apocrypha_supported, bible_version_set,
bible_version_tuple, version_map)
from firebase_wrapper import Database
from message_match import MessageMatch
from next_step_handler import NextStepHandler
from request_sess import s
# The set of errors to stop sending the message when encountered
ERRORS_TO_BREAK_ON = {
"Bad Request: chat not found",
"Bad Request: replied message not found",
"Bad Request: message to reply not found",
"Bad Request: message to be replied not found",
"Bad Request: group chat was upgraded to a supergroup chat",
"Bad Request: need administrator rights in the channel chat",
"Bad Request: CHAT_RESTRICTED",
"Bad Request: TOPIC_CLOSED",
"Bad Request: message thread not found",
"Bad Request: not enough rights to send text messages to the chat",
"Forbidden: bot was kicked from the supergroup chat",
"Forbidden: bot was kicked from the group chat",
"Forbidden: bot was blocked by the user",
"Forbidden: user is deactivated",
"Forbidden: bot was kicked from the channel chat",
"Forbidden: bot is not a member of the channel chat",
"Forbidden: the group chat was deleted",
}
# Sets the timezone to Singapore's timezone
# The default timezone on Replit is UTC+0
os.environ["TZ"] = "Asia/Singapore"
time.tzset()
# Logging configuration
logging.basicConfig(
format="%(levelname)s: %(asctime)s - %(message)s", level=logging.DEBUG
)
# logging.disable("CRITICAL")
# Disable the logging for httpcore
logging.getLogger("httpcore").setLevel(logging.CRITICAL)
# Starts the server
keep_alive.keep_alive()
# Bot API key
API_KEY = os.environ["API_KEY"]
# Initialise the bot
bot = TeleBot(API_KEY)
# Initialise the next step handler
handler = NextStepHandler(max_step=1)
# Initialise the database
db = Database("/bible_verses_bot")
# The time to send out the verse of the day message in 24 hours
# It should be set to (12, 0) for 12:00pm
verse_of_the_day_time: Tuple[int, int] = (12, 0)
class TimeCheck(threading.Thread):
"""
A class that creates a thread to check the time of the day.
This is so that the bot can still run while sending the verse
of the day daily at the correct time.
"""
# Have empty slots so the a dictionary isn't created for this class
__slots__: list = []
# List of TimeCheck instances
instances: list = []
def __init__(self) -> None:
threading.Thread.__init__(self)
self.daemon = True
TimeCheck.instances.append(self)
# Function to start the thread for the user to receive the verse of the day
def run(self) -> None:
# Loop to ensure that the function runs continuously
while True:
# Gets the current time
current_time = datetime.datetime.now()
# For testing purposes
# if current_time.minute == 0:
# Checks if the time of the day is the time to send out the verse of the day and the previous sent time is a day ago
if (current_time.hour, current_time.minute) == (
verse_of_the_day_time[0],
verse_of_the_day_time[1],
) and current_time.day != datetime.datetime.fromisoformat(
db["previous_sent_time"]
).day:
# Calls the function to send the verse of the day message
asyncio.run(send_verse_of_the_day())
# Set the previous sent time to the current time
db["previous_sent_time"] = datetime.datetime.now().isoformat()
# Pauses the execution of the thread for the required amount of time until the next day's 12pm
else:
# Gets the current time
current_time = datetime.datetime.now()
# Checks if the previous sent time was one day ago and the current time is after 12pm
if (
datetime.datetime.fromisoformat(
db["previous_sent_time"]
).day
!= current_time.day
and current_time.hour >= verse_of_the_day_time[0]
):
# Pauses the program for 10 seconds to avoid errors
time.sleep(10)
# Sends the verse of the day
asyncio.run(send_verse_of_the_day())
# Set the previous sent time to the current time
db["previous_sent_time"] = (
datetime.datetime.now().isoformat()
)
# The time now
now = datetime.datetime.now()
# The time that the verse of the day is supposed to be sent
next_time = datetime.datetime(
year=now.year,
month=now.month,
day=now.day,
hour=verse_of_the_day_time[0],
minute=verse_of_the_day_time[1],
)
# For testing purposes
# next_time = datetime.datetime(year=now.year, month=now.month, day=now.day, hour=now.hour)
logging.info(f"{next_time}")
# Checks if the next_time is later than the current time
if next_time > now:
# Pauses the execution until the current day's 12pm
trusty_sleep((next_time - now).seconds)
# Pauses the program for one second so that it doesn't miss the timing to send the verse of the day
time.sleep(1)
# If next_time is before or equal to the current time
else:
# Set the next time to be the next day's 12pm
next_time = next_time + datetime.timedelta(days=1)
# For testing purposes
# next_time = next_time + datetime.timedelta(hours=1)
logging.info(f"Edited next_time is {next_time}")
# Pauses the execution until the next day's 12pm
trusty_sleep((next_time - now).seconds)
# Pauses the program for one second so that it doesn't miss the timing to send the verse of the day
time.sleep(1)
# The function to debounce the inline query
def debounce_inline_query(duration: float) -> Callable:
# The decorator within to decorate the inline query handler with the debounce
def decorator(
inline_query_handler: Callable[[types.InlineQuery], None]
) -> Callable[[types.InlineQuery], None]:
# The dictionary of inline queries from different users
inline_query_dict: Dict[Union[int, str], threading.Timer] = {}
# The actual debounce function that pauses the inline query handler until the wait duration has passed
def actual_debounce(inline_query: types.InlineQuery) -> None:
# Grabs the inline query dictionary from the decorator
nonlocal inline_query_dict
# Gets the user id from the inline query
user_id: int = inline_query.from_user.id
# The function to wrap the inline query handler so it can be called in a thread
def call_handler(user_id: int) -> None:
# Remove the inline query handler for the user from the dictionary
inline_query_dict.pop(user_id, None)
# Calls the inline query handler
inline_query_handler(inline_query)
# Tries to cancel the timer on the current calling inline query handler
try:
inline_query_dict[user_id].cancel()
# If the current inline query is not found in the dictionary, skip cancelling the timer
except KeyError:
pass
# Add the handler for this inline query to the dictionary
inline_query_dict[user_id] = threading.Timer(
duration, call_handler, [user_id]
)
# Starts the timer
inline_query_dict[user_id].start()
# Returns the actual debounce function
return actual_debounce
# Returns the decorator
return decorator
# The function to call when there is an inline query
@bot.inline_handler(lambda query: quick_check(query.query))
@debounce_inline_query(1)
def inline_handler(inline_query: types.InlineQuery) -> None:
# Calls the answer function using a thread
threading.Thread(target=inline_answer, args=(inline_query,)).start()
# Function to respond to the inline query
def inline_answer(inline_query: types.InlineQuery) -> None:
# Get the verse using the search_verse function
verse = search_verse(inline_query=inline_query, inline=True)
# Checks if the verse is too long
if len(verse) > 4096:
# Answers the inline query with an error message
bot.answer_inline_query(
inline_query.id,
cache_time=0,
results=[
types.InlineQueryResultArticle(
"Message is too long, try searching for a shorter verse.",
"Message is too long, try searching for a shorter verse.",
types.InputTextMessageContent(
"Sorry, the requested verse was too long to be sent."
),
)
],
)
# If the verse is not too long
elif verse:
# Gets the title of the verse
verse_title = verse.split("\n")[0]
# Answers the inline query
bot.answer_inline_query(
inline_query.id,
cache_time=0,
results=[
types.InlineQueryResultArticle(
verse_title,
verse_title,
types.InputTextMessageContent(verse, parse_mode="markdown"),
)
],
)
# Handles the /start command
@bot.channel_post_handler(commands=["start"])
@bot.message_handler(commands=["start"])
def start_handler(message: types.Message) -> None:
start_msg = """Hello! Add this bot to your Christian group chats and any bible verses shared to the group will be automatically detected by the bot and displayed as a reply to the message with the bible verse.
You can also subscribe to the verse of the day in your group chat or through the messaging the bot with the /verseoftheday command.
To unsubscribe from the verse of the day, use the /stopverseoftheday command.
Use inline mode by tagging the bot, @bible_verses_bot, and then typing your desired verse to send the entire verse to your chat.
To see more information about the bot, use the /help command.
Hopefully this bot will help you in your journey with God!"""
send_message(start_msg, message)
# Handles the /help command
@bot.channel_post_handler(commands=["help"])
@bot.message_handler(commands=["help"])
def help_handler(message: types.Message) -> None:
help_msg = """To use this bot, add the bot to a group and any bible verses shared to the group will be automatically detected and displayed as a separate message.
The default bible version used is the New International Version (NIV).
The default bible version used to search the Apocrypha is the New Revised Standard Version Updated Edition (NRSVUE).
All searches using the New Revised Standard Version (NRSV) as the bible version will be automatically changed to use the New Revised Standard Version Updated Edition (NRSVUE) as the bible version instead.
The bot will only detect bible verses written in these formats:
- 1 John 1:9 NIV
- Luke 9:1-10
- Deuteronomy 1:3,5-7,10,4:2-4,6,8-10 KJV
- Ezra 3:1, 3-7, 5:3, 6:3-7, 10
- Ruth 1:10-11; 4:2-6, 10,21
- 2 Samuel 3:4;5:13-14, 20
- Titus 1 NKJV
- Hebrews 3, 5-7, 10
- 2 Chronicles Chapter 2 Verse 6 NCV
- 3 John Chapter 1 Verses 1-13
- Genesis Chapter 1 Verse 9 to 11 NASB
- Job Chapter 1 Verse 1,4-8,11
- Daniel Chapter 5 Verses 4 to 6, 10-13, 17
- Nehemiah Chapter 10 ESV
- Hosea Chapters 3-4,8
You can use the bot's inline mode by tagging the bot, @bible_verses_bot, and then searching for the verse that you want to send to the chat.
You can also use the /verse command to find the verse you're looking for.
You can use the /version command to see the default bible version the bot is set to.
Use the /setversion command to change the default bible version.
You can also use the /listversions command to see the list of bible versions you can change to.
The bot can also send you the verse of the day by using the /verseoftheday or the /votd command.
The verse of the day would be sent at 12:00pm daily and it will be in the bible version that you have set for the chat. If the bible version you have set for your chat doesn't have the verse of the day, the bot will send the NIV version instead.
You can unsubscribe from the verse of the day using the /stopverseoftheday or the /svotd command.
For any bug reports, enquiries or feedback, please contact @hankertrix.
Hopefully you'll find this bot useful!"""
send_message(help_msg, message)
# A function to get the version from the database and returns NIV if there is no default version found
def get_version(message: types.Message) -> str:
return db["chats_version"].get(str(message.chat.id), "NIV")
# Handles the /version command
@bot.channel_post_handler(commands=["version"])
@bot.message_handler(commands=["version"])
def display_version(message: types.Message) -> None:
# Gets the version using the get_version function
version = get_version(message)
# The message to send to the chat
version_message = f"The current bible version is {version}."
# Sends the message
send_message(version_message, message)
# Handles the /setversion command
@bot.channel_post_handler(commands=["setversion"])
@bot.message_handler(commands=["setversion"])
def handle_version(message: types.Message) -> None:
# The content of the message behind the /setversion command
msg_ctx = (
re.sub("/setversion ?", "", message.text.lower())
if message.text
else ""
)
# Checks if there is nothing behind the /setversion command
if msg_ctx == "":
# Sends the message to the user
send_message("Please enter your bible version.", message)
# Register the next step handler
handler.register_next_step_handler("setversion", message)
# If there is text written behind the /setversion command
else:
# Calls the set_version function
set_version(message, msg_ctx)
# The function to read the user's message and save the new bible version given if it's accepted
@bot.channel_post_handler(
func=lambda message: handler.check_step("setversion", message, 1)
)
@bot.message_handler(
func=lambda message: handler.check_step("setversion", message, 1)
)
def set_version(message: types.Message, ctx: str = "") -> None:
# Checks if the context is not given
# And if the message text is given (it should always be given)
if ctx == "" and message.text:
# Sets the version to the entire message given by the user
version_given = message.text.upper().strip()
# If the context is given
else:
# Sets the version to be the text behind the /setversion command
version_given = ctx.upper().strip()
# Gets the version from the version mapping
version_given = version_map.get(version_given, version_given)
# Gets the version dictionary from the database
version_dict = db["chats_version"]
# Checks if the bible version given is an accepted one
if version_given in bible_version_set:
# Gets the message ID as a string
message_id = str(message.chat.id)
# Removes the saved version if found
if message_id in version_dict:
del version_dict[message_id]
# Checks if the version is not NIV
if version_given != "NIV":
# Saves the new version given to the database only when it's not NIV to save space
version_dict[message_id] = version_given
# The message to notifiy the chat that the version has changed
version_changed_msg = (
f"The current bible version has changed to {version_given}."
)
# Sends the message
send_message(version_changed_msg, message)
# If the bible version given is not in the list
else:
# The message to send to the chat
invalid_msg = f"You have given me an invalid bible version. \n{get_version(message)} remains as the current bible version.\nUse the /setversion command to try again."
# Sends the message
reply_to(message, invalid_msg)
# Removes the next step handler
handler.clear_step_handler("setversion", message)
# Handles the /listversions command
@bot.channel_post_handler(commands=["listversions", "listversion"])
@bot.message_handler(commands=["listversions", "listversion"])
def list_bible_versions(message: types.Message) -> None:
# Gets the list of accepted English bible versions and joins them with a line break
english_versions = "\n".join(bible_version_tuple[:-13])
# Gets the list of accepted Chinese bible versions and joins them with a line break
chinese_versions = "\n".join(bible_version_tuple[-13:])
# Gets the list of bible versions that support apocrypha and joins them with a line break
apocrypha_versions = "\n".join(apocrypha_supported)
# The message to be sent to the chat
list_version_msg = f"Accepted bible versions: \n\n\nEnglish versions: \n\n{english_versions} \n\n\nChinese versions: \n\n{chinese_versions} \n\n\nBible versions that support Apocrypha (English only): \n\n{apocrypha_versions}"
# Sends the message
send_message(list_version_msg, message)
# Gets the specific message id from the database
def get_id(message_id: int) -> List[int]:
return [chat_id for chat_id in db["subbed"] if chat_id == message_id]
# Handles the /verseoftheday command
@bot.channel_post_handler(commands=["verseoftheday", "votd"])
@bot.message_handler(commands=["verseoftheday", "votd"])
def verse_start(message: types.Message) -> None:
# Gets the saved version
saved_version = db["chats_version"].get(str(message.chat.id), "NIV")
# Gets the verse message
verse_msg = asyncio.run(get_verse_of_the_day(saved_version))
# Gets the list of subscribers to the verse of the day message
sub_list = db["subbed"]
# Appends the user's chat id to the database
sub_list.append(message.chat.id)
# Sets the data in the database to the new list and remove all duplicate entries
db["subbed"] = list(dict.fromkeys(sub_list))
# Message to be sent to the user
sub_msg = f"You are now subscribed to the verse of the day! \n\nYou will now receive the verse of the day at 12:00pm daily. \n\nToday's verse is: \n\n{verse_msg}"
# Sends the message
send_message(sub_msg, message, parse_mode="markdown")
# Handles the /stopverseoftheday command
@bot.channel_post_handler(commands=["stopverseoftheday", "svotd"])
@bot.message_handler(commands=["stopverseoftheday", "svotd"])
def verse_stop(message: types.Message) -> None:
# Gets list of chat ids for the user (should be 0 or 1 in length)
chat_id_list = get_id(message.chat.id)
# Checks if the user has subscribed before
if len(chat_id_list) != 0:
# Gets their chat id from the database and removes it
db["subbed"] = [sub for sub in db["subbed"] if sub != chat_id_list[0]]
# Message to acknowledge that they have been unsubscribed
stop_msg = "You will no longer receive the verse of the day daily. To re-enable, use the /verseoftheday or /votd command."
send_message(stop_msg, message)
# Sends them a message to tell them to subscribe first
else:
not_subbed_msg = "You haven't subscribed to receive the verse of the day. Please subscribe first using the /verseoftheday or the /votd command."
send_message(not_subbed_msg, message)
# More reliable time.sleep() because I'm pausing the verse of the day thread execution for a long time
def trusty_sleep(sleeptime: int) -> None:
start = time.time()
while time.time() - start < sleeptime:
time.sleep(sleeptime - (time.time() - start))
# Function to get the verse of the day
async def get_verse_of_the_day(version="NIV") -> str:
# Initialise the verse message
verse_msg = ""
# While the verse message is nothing
while verse_msg == "":
# Using the httpx async client
async with httpx.AsyncClient() as session:
# Gets the verse of the day page from bible gateway
verse_of_the_day_page = await session.get(
f"https://www.biblegateway.com/reading-plans/verse-of-the-day/next?version={version}"
)
# Add line breaks to the end of every heading
text = re.sub(
r"</h[1-6]>",
lambda match: f"\n{match.group()}",
verse_of_the_day_page.text,
)
# Replace all the backticks in the text with a different backtick so that it will not mess with the backtick used for markdown formatting in the message sent to the user
text = text.replace("`", "\u02cb")
# Initialise the beautiful soup parser with lxml
soup = BeautifulSoup(text, "lxml")
# Delete the footnotes and the cross references from the HTML
for unwanted_tag in soup.select(
".passage-other-trans, .footnote, .footnotes, .crossreference, .crossrefs"
):
unwanted_tag.decompose()
# Convert all of the <br> tags to a span tag containing a newline character
for tag in soup.select("br"):
tag.name = "span"
tag.string = "\n"
# Convert all of the text in <sup> tags to superscript
for tag in soup.select("sup"):
tag.string = utils.to_sups(tag.text.strip())
# Selects all of the verses
verses_soup = soup.select(".rp-passage")
# The list to store the verse message
verse_msg_list = []
# Iterate over all of the verses
for verse_soup in verses_soup:
# Gets the verse name from the HTML
verse_name = (
verse_soup.find("div", class_="rp-passage-display")
.get_text()
.strip()
)
# Gets the text of the verse from the HTML
verse_text = verse_soup.find(
"div", class_="rp-passage-text"
).get_text()
# Adds a left to right mark in front of the verse to force all text to be displayed left to right (stops the Hebrew symbols from appearing at the end of the line instead of the start of the line in Psalms 119)
# Also removes all the spaces after a new line character
verse_text = "\u200e" + re.sub("\n +", "\n", verse_text.strip())
# Creates the verse and adds it to the list
verse_msg_list.append(f"{verse_name} {version}\n{verse_text}")
# The verse message containing the verse name and the verse of the day
verse_msg = "\n\n".join(verse_msg_list).strip()
# Remove random characters from the verse message, like the asterisks in the MSG version of the bible
verse_msg = re.sub("[¶*] *", "", verse_msg)
# If the verse message is nothing, change the version to NIV
if verse_msg == "":
version = "NIV"
# Returns the verse message
return verse_msg
# Function to send the verse of the day
async def send_verse_of_the_day() -> None:
# asyncio.sleep(15)
# Gets the list of people who have subscribed to the verse of the day
subbed_list = db["subbed"]
# Gets the stored version for each chat
chats_version = db["chats_version"]
# An empty dictionary to add the version and their respective messages to
# It will always contain NIV
verse_of_the_day_msg_dict = {"NIV": ""}
# Iterates over the list of chat IDs that have subscribed to the verse of the day
for chat_id in subbed_list:
# Gets the saved version for the chat ID
saved_version = chats_version.get(str(chat_id))
# Checks if the chat has saved a version
if saved_version is not None:
# Adds the version to the dictionary
verse_of_the_day_msg_dict[saved_version] = ""
# The list of tasks
tasks = []
# The list of versions in the dictionary
versions = list(verse_of_the_day_msg_dict.keys())
# Iterates over all of the versions in the dictionary
for version in versions:
# Adds the task to get the verse of the day to the list of tasks
tasks.append(get_verse_of_the_day(version))
# Gathers all of the verses of the day and collects the exceptions as well
verses_of_the_day = await asyncio.gather(*tasks, return_exceptions=True)
# Iterates over the verses of the day list and retries any exceptions found
for index, item in enumerate(verses_of_the_day):
# If the item is not an exception, continue the loop
if type(item) is not Exception:
continue
# Keep trying to get the verse of the day until it is successful
while True:
try:
# Try getting the verse of the day for the version again
new_verse_of_the_day = await get_verse_of_the_day(
versions[index]
)
# Breaks the loop if getting the verse of the day is successful
break
# Logs the exception
except Exception as e:
logging.error(e)
# Put the verse of the day into the list
verses_of_the_day[index] = new_verse_of_the_day
# Iterates over the versions in the dictionary
for index, version in enumerate(versions):
# Sets the version in the dictionary to the verse of the day message
verse_of_the_day_msg_dict[version] = (
f"Today's verse is: \n\n{verses_of_the_day[index]}"
)
# Iterates the list of chat ids that have subscribed to the verse of the day
for chat_id in subbed_list:
# Gets the saved version for the chat ID and default to NIV if it isn't set
saved_version = chats_version.get(str(chat_id), "NIV")
# The verse message to send to the person
verse_msg = verse_of_the_day_msg_dict.get(saved_version)
# Sends the verse of the day message to all of the chats using a thread
threading.Thread(
target=send_message,
args=(verse_msg,),
kwargs={"chat_id": chat_id, "parse_mode": "markdown"},
).start()
# Function to get the webpage asynchronously
async def get_webpages(match_obj_list: List[VerseMatch]) -> List[str]:
# Use the httpx async client to get the webpages
async with httpx.AsyncClient() as session:
# List of tasks
tasks = []
# Iterates the list of match objects
for obj in match_obj_list:
# Calls the match object's get url function
url = obj.get_url()
# Appends the task to get the webpage asynchronously to the tasks list
tasks.append(session.get(url))
# Gets all the response objects
responses = await asyncio.gather(*tasks, return_exceptions=True)
# Iterates over the list of response objects
for index, item in enumerate(responses):
# Skips the item if it is not an exception
if not isinstance(item, Exception):
continue
# Otherwise, try getting the url again
# Infinite loop so that the bot keeps trying
while True:
try:
# Gets the url from the object
url = match_obj_list[index].get_url()
# Gets the new response object
new_response = session.get(url)
# Sets the response object in the list to the new one
responses[index] = new_response
# Breaks the loop if it's successful
break
# Otherwise, catch any error and logs them
except Exception as e:
logging.error(e)
# Returns the list of htmls returned by the httpx module
return [response.text for response in responses]
# Function to determine the version to be passed to the VerseMatch object
def choose_version(default_version: str, bible_version: str) -> str:
# Returns the bible version if it's not empty, otherwise, return the default version
return bible_version if bible_version != "" else default_version
# A function to search the bible verse and return the verse message
# so I don't have to write the same thing twice for the two threading classes
# to search the verse
def search_verse(
message: Union[types.Message, str] = "",
inline_query: Union[types.InlineQuery, str] = "",
inline: Optional[bool] = False,
) -> str:
# For measuring performance
# start_time = time.perf_counter()
# Checks if the function is called from the inline handler
if inline:
# Sets the default version to NIV
default_version = "NIV"
# Gets the inline query and makes it lowercase
msg = inline_query.query.lower()
# If the function is called from a message
else:
# Gets the default version from the chat
default_version = get_version(message)
# Rips the message content and makes the message lowercase
msg = message.text.lower()
# Create a message match object to store data
msg_obj = MessageMatch(msg=msg)
# Calls the message match object function to simplify and organise the data
msg_obj.make_dic()
dic = msg_obj.dic
# List of match objects
match_obj_list = []
# Iterates through the dictionary created by the message match object
for [book, chapter, start_verse, end_verse, version] in dic.values():
# Create a match object for every match in the dictionary
# The match object grabs the data from the message match object
match_obj = VerseMatch(
msg=msg,
book=book,
match=[chapter, start_verse, end_verse],
version=choose_version(default_version, version),
)
# Appends the match object to a list
match_obj_list.append(match_obj)
# List of htmls returned by the get webpages function
htmls = asyncio.run(get_webpages(match_obj_list))
# For measuring performance
verse_match_start_time = time.perf_counter()
# Iterates the match object list
for index, obj in enumerate(match_obj_list):
# Assigns the match object's res property to the html response
obj.res = htmls[index]
# Calls the match object function to parse the message and return the bible verse
obj.get_verses()
# Gets the resulting string from the objects if the object is valid
verse_msg = "\n\n\n\n\n".join(
dict.fromkeys(obj.verses for obj in match_obj_list if obj.valid)
)
# For measuring performance
# with open("time.txt", "a") as f:
# f.write(f"{time.perf_counter() - verse_match_start_time}\n")
logging.debug(
f"VerseMatch time taken: {time.perf_counter() - verse_match_start_time}"
)
# logging.debug(f"Total time taken: {time.perf_counter() - start_time}")
# Returns the verse message
return verse_msg
# A class to handle the get_verse() function
class GetVerse(threading.Thread):
# Slots to save memory
__slots__ = ["message"]
def __init__(self, message: types.Message) -> None:
threading.Thread.__init__(self)
self.message = message
self.daemon = False
# Overriding the thread's run function
def run(self) -> None:
# Calls the search_verse function to get the verse message to be sent
verse_msg = search_verse(message=self.message)
# Checks if the verse message is empty
if verse_msg != "":
# Sends a reply if it's not empty
split_message(self.message, verse_msg, parse_mode="markdown")
# Asks the user to try again
else:
invalid_msg = """You have given me an invalid bible verse. Check your spelling and try again by using the /verse command."""
reply_to(self.message, invalid_msg)
# Handles the command /verse
@bot.channel_post_handler(commands=["verse"])
@bot.message_handler(commands=["verse"])
def verse_handler(message: types.Message) -> None:
# Removes the command from the message
msg = (
message.text.lower().replace(r"/verse", "").strip()
if message.text
else ""
)
# Checks if the msg still has other words
if msg:
# Initialise a GetVerse thread
thread = GetVerse(message)
# Starts the thread
thread.start()
# If the message is empty
else:
# Sends the message to the user
send_message("Please enter your bible verses.", message)
# Registers the next step handler
handler.register_next_step_handler("verse", message)
# Searches for the bible verse given previously through the command /verse
@bot.channel_post_handler(
func=lambda message: handler.check_step("verse", message, 1)
)
@bot.message_handler(
func=lambda message: handler.check_step("verse", message, 1)
)
def get_verse(message: types.Message) -> None:
# Initialises a thread from the GetVerse class
thread = GetVerse(message)
# Starts the thread
thread.start()
# Removes the next step handler
handler.clear_step_handler("verse", message)
# A function to quickly check if a message contains a bible verse
def quick_check(message: str) -> bool:
# Makes the message lower case
msg = message.lower()
# Runs through all the different checks
if check_num_portion(msg):
return True
elif check_chapter_portion(msg):
return True
elif check_full_num_chapter(msg):
return True
elif check_full_chapt_chapter(msg):
return True
else: