-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
92 lines (78 loc) · 2.68 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
"""start the bot!"""
import os
import logging
import asyncio
import sys
from dotenv import load_dotenv
from telegram import *
from telegram.ext import *
from pollenbot.forecast import get_regions
from pollenbot.bothandlers.start import start_handler
from pollenbot.bothandlers.forecast import forecast_handler
from pollenbot.bothandlers.help import help_handler
from pollenbot.bothandlers.unknown import unknown_command_handler
from pollenbot.bothandlers.error import error_handler
from pollenbot.remind import queue_reminder
# I don't use this but you can use it to set commands with @botfather with /setcommands
_ = """
start - set up
forecast - upcoming pollen forecast
help - help, info & privacy information
"""
load_dotenv()
try:
API_KEY = os.environ["TELEGRAM_BOT_ACCESS_TOKEN"]
except KeyError as e:
raise ValueError(
"Please set the environment variable TELEGRAM_BOT_ACCESS_TOKEN"
) from e
if len(sys.argv) > 1 and sys.argv[1] == "start":
run_on_startup = True
else:
run_on_startup = False
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
def main():
"""start the bot!"""
this_dir = os.path.dirname(os.path.realpath(__file__))
persistent_data = PicklePersistence(
filepath=os.path.join(this_dir, "bot_data.pickle"),
store_data=PersistenceInput(user_data=True, bot_data=False),
)
loop = asyncio.new_event_loop()
all_user_data = loop.run_until_complete(persistent_data.get_user_data())
logger.info("Loaded user data: %s", all_user_data)
loop.close()
async def add_regions_to_application(application: Application) -> None:
application.bot_data["regions"] = get_regions()
application = (
Application.builder()
.token(API_KEY)
.persistence(persistent_data)
.post_init(add_regions_to_application)
.build()
)
application.add_handler(start_handler)
application.add_handler(forecast_handler)
application.add_handler(help_handler)
application.add_handler(unknown_command_handler)
application.add_error_handler(error_handler)
for user_id, user_data in all_user_data.items():
try:
reminders_on = user_data["reminders"]
except KeyError:
continue
if reminders_on:
if application.job_queue is None:
raise ValueError("Job queue not set in application")
queue_reminder(
application.job_queue,
user_id,
user_data=user_data,
send_now=run_on_startup,
)
application.run_polling()
if __name__ == "__main__":
main()