This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaimon.py
executable file
·126 lines (108 loc) · 4.18 KB
/
paimon.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
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
# Copyright (c) 2021-2024 scmanjarrez. All rights reserved.
# This work is licensed under the terms of the MIT license.
import logging
import os
import paimon_cli as cli
import paimon_gui as gui
import utils as ut
from telegram import Update
from telegram.ext import (
ApplicationBuilder,
CallbackQueryHandler,
CommandHandler,
filters,
)
async def button_handler(update: Update, context: ut.Context):
uid = ut.uid(update)
query = update.callback_query
if cli.allowed(uid):
if query.data == "main_menu":
await gui.main_menu(update)
elif query.data == "notes_menu":
await gui.notes_menu(update, context)
elif query.data == "diary_month_menu":
await gui.diary_month_menu(update)
elif query.data.startswith("diary_menu"):
args = query.data.split("_")
await gui.diary_menu(update, int(args[-1]))
elif query.data == "abyss_seasons_menu":
await gui.abyss_seasons_menu(update)
elif query.data.startswith("abyss_floors_menu"):
args = query.data.split("_")
await gui.abyss_floors_menu(update, args[-1] == "previous")
elif query.data.startswith("abyss_menu"):
args = query.data.split("_")
await gui.abyss_menu(update, args[-2] == "previous", args[-1])
elif query.data == "notifications_menu":
await gui.notifications_menu(update)
elif query.data.startswith("notification_toggle"):
args = query.data.split("_")
await gui.notification_toggle(update, args[-1])
def setup_handlers(application: ApplicationBuilder):
start_handler = CommandHandler(
"start", cli.bot_help, filters=~filters.UpdateType.EDITED_MESSAGE
)
application.add_handler(start_handler)
help_handler = CommandHandler(
"help", cli.bot_help, filters=~filters.UpdateType.EDITED_MESSAGE
)
application.add_handler(help_handler)
menu_handler = CommandHandler(
"menu", cli.menu, filters=~filters.UpdateType.EDITED_MESSAGE
)
application.add_handler(menu_handler)
redeem_handler = CommandHandler(
"redeem", cli.redeem, filters=~filters.UpdateType.EDITED_MESSAGE
)
application.add_handler(redeem_handler)
set_handler = CommandHandler(
"set", cli.set_value, filters=~filters.UpdateType.EDITED_MESSAGE
)
application.add_handler(set_handler)
get_handler = CommandHandler(
"get", cli.get_value, filters=~filters.UpdateType.EDITED_MESSAGE
)
application.add_handler(get_handler)
application.add_handler(CallbackQueryHandler(button_handler))
if __name__ == "__main__":
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
logging.getLogger("apscheduler.executors.default").addFilter(ut.NoLog())
logging.getLogger("apscheduler.scheduler").addFilter(ut.NoLog())
logging.getLogger("httpx").addFilter(ut.NoLog())
if os.path.isfile(ut.CONF_FILE):
ut.set_up()
application = ApplicationBuilder().token(ut.setting("token")).build()
application.job_queue.run_once(
ut.update_db, 5, name="Starting DB update"
)
# application.job_queue.run_once(
# ut.daily_checkin, 10, name="Starting daily claiming"
# )
setup_handlers(application)
try:
if ut.setting("webhook"):
application.run_webhook(
listen=ut.setting("listen"),
port=ut.setting("port"),
url_path=ut.setting("token"),
cert=ut.setting("cert"),
webhook_url=(
f"https://"
f"{ut.setting('ip')}/"
f"{ut.setting('token')}"
),
)
else:
application.run_polling()
except KeyError:
logging.error(
f"New setting 'webhook' required "
f"in {ut.CONF_FILE}. Check README for more info."
)
else:
logging.error(f"File {ut.CONF_FILE} not found.")