-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
89 lines (76 loc) · 2.71 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
"""Telegram bot to log daily spending"""
import os
from dotenv import load_dotenv
import logging
import pandas
from telegram.ext import *
import telegram.ext.filters as filters
from telegram import *
from budgeter.bothandlers.help import help_handler
from budgeter.bothandlers.start import start_handler
from budgeter.bothandlers.stats import stats_handler
from budgeter.bothandlers.spend import spend_handler
from budgeter.bothandlers.privacy import privacy_handler
from budgeter.bothandlers.spreadsheet import spreadsheet_handler
from budgeter.bothandlers.errorHandler import error_handler
from budgeter.bothandlers.remind import remind_handler
from budgeter.bothandlers.unknown import unknown_command_handler
import asyncio
from budgeter.remind import queue_reminder
import gspread
load_dotenv()
try:
API_KEY = os.environ["TELEGRAM_BOT_ACCESS_TOKEN"]
except KeyError as e:
raise ValueError(
"Please set the TELEGRAM_BOT_ACCESS_TOKEN environment variable."
) from e
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
def main():
# user data
persistent_data = PicklePersistence(
filepath="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())
loop.close()
# spreadsheet authentication
CREDENTIALS_PATH = "google_credentials.json"
spreadsheet_client = gspread.service_account(filename=CREDENTIALS_PATH)
async def add_client_to_application(application: Application) -> None:
application.bot_data["spreadsheet_client"] = spreadsheet_client
# application
application = (
Application.builder()
.token(API_KEY)
.persistence(persistent_data)
.post_init(add_client_to_application)
.build()
)
application.add_handler(help_handler)
application.add_handler(start_handler)
application.add_handler(stats_handler)
application.add_handler(spreadsheet_handler)
application.add_handler(remind_handler)
application.add_handler(spend_handler)
application.add_handler(privacy_handler)
application.add_handler(unknown_command_handler)
application.add_error_handler(error_handler)
for user_id, user_data in all_user_data.items():
try:
sheet_url = user_data["spreadsheet_url"]
reminders_on = user_data["reminders"]
except KeyError:
continue
if reminders_on:
queue_reminder(application.job_queue, user_id)
application.run_polling()
if __name__ == "__main__":
main()