diff options
author | clarkzjw <[email protected]> | 2023-02-24 21:08:59 -0800 |
---|---|---|
committer | clarkzjw <[email protected]> | 2023-02-28 15:58:02 -0800 |
commit | 353b83d415061bff2881cbe324273409740be64c (patch) | |
tree | 420fa402e7eda3425fd69b24959cffb0b2cc5039 /bot.py | |
parent | 0041eb4f9893687565e444be9d50648f49aa4d91 (diff) | |
download | swarm2fediverse-353b83d415061bff2881cbe324273409740be64c.tar.gz |
bot: implement delayed checkin, (kind of), some TODO leftfeature/delayed_checkin
Diffstat (limited to 'bot.py')
-rw-r--r-- | bot.py | 31 |
1 files changed, 27 insertions, 4 deletions
@@ -11,6 +11,7 @@ from starlette.requests import Request | |||
11 | from starlette.responses import PlainTextResponse, Response, JSONResponse | 11 | from starlette.responses import PlainTextResponse, Response, JSONResponse |
12 | from starlette.routing import Route | 12 | from starlette.routing import Route |
13 | from telegram import Update | 13 | from telegram import Update |
14 | from telegram.error import BadRequest | ||
14 | from telegram.ext import ( | 15 | from telegram.ext import ( |
15 | Application, | 16 | Application, |
16 | CallbackContext, | 17 | CallbackContext, |
@@ -33,7 +34,8 @@ from callback import ( | |||
33 | callback_skip_location_keyword_search, | 34 | callback_skip_location_keyword_search, |
34 | callback_add_comment, | 35 | callback_add_comment, |
35 | callback_skip_comment, | 36 | callback_skip_comment, |
36 | callback_add_media | 37 | callback_add_media, |
38 | callback_delayed_checkin | ||
37 | ) | 39 | ) |
38 | from command import ( | 40 | from command import ( |
39 | start_command, | 41 | start_command, |
@@ -44,7 +46,8 @@ from command import ( | |||
44 | toggle_visibility_command, | 46 | toggle_visibility_command, |
45 | callback_toggle_visibility, | 47 | callback_toggle_visibility, |
46 | logout_command, | 48 | logout_command, |
47 | list_command | 49 | list_command, |
50 | delayed_checkin_command, | ||
48 | ) | 51 | ) |
49 | from config import ( | 52 | from config import ( |
50 | FEDI_LOGIN, | 53 | FEDI_LOGIN, |
@@ -57,7 +60,8 @@ from config import ( | |||
57 | BOT_TOKEN, | 60 | BOT_TOKEN, |
58 | BOT_SCOPE, | 61 | BOT_SCOPE, |
59 | MAIN_MENU, | 62 | MAIN_MENU, |
60 | WAIT_VISIBILITY | 63 | WAIT_VISIBILITY, |
64 | DELAYED_CHECKIN, | ||
61 | ) | 65 | ) |
62 | 66 | ||
63 | from prompt.string import PROMPT_CHOOSE_ACTION | 67 | from prompt.string import PROMPT_CHOOSE_ACTION |
@@ -114,10 +118,14 @@ async def process_oauth_login_callback(update: FediLoginCallbackUpdate, context: | |||
114 | user.access_key = encrypt(access_token, ENCRYPT_KEY) | 118 | user.access_key = encrypt(access_token, ENCRYPT_KEY) |
115 | user.save() | 119 | user.save() |
116 | 120 | ||
117 | text = "You have successfully logged in to your Mastodon account!" | 121 | text = "You have successfully logged in to your Mastodon account!" |
122 | try: | ||
118 | await context.bot.delete_message(chat_id=user.telegram_user_id, message_id=context.user_data[PROMPT_FEDI_LOGIN]) | 123 | await context.bot.delete_message(chat_id=user.telegram_user_id, message_id=context.user_data[PROMPT_FEDI_LOGIN]) |
119 | await context.bot.send_message(chat_id=user.telegram_user_id, text=text) | 124 | await context.bot.send_message(chat_id=user.telegram_user_id, text=text) |
120 | await context.bot.send_message(chat_id=user.telegram_user_id, text=PROMPT_CHOOSE_ACTION, reply_markup=MAIN_MENU) | 125 | await context.bot.send_message(chat_id=user.telegram_user_id, text=PROMPT_CHOOSE_ACTION, reply_markup=MAIN_MENU) |
126 | except BadRequest as e: | ||
127 | if "not found" in str(e.message): | ||
128 | pass | ||
121 | 129 | ||
122 | 130 | ||
123 | async def main() -> None: | 131 | async def main() -> None: |
@@ -176,11 +184,26 @@ async def main() -> None: | |||
176 | allow_reentry=True, | 184 | allow_reentry=True, |
177 | ) | 185 | ) |
178 | 186 | ||
187 | delayed_checkin_handler = ConversationHandler( | ||
188 | entry_points=[ | ||
189 | CommandHandler("delay", delayed_checkin_command), | ||
190 | ], | ||
191 | states={ | ||
192 | DELAYED_CHECKIN: [ | ||
193 | MessageHandler(filters.TEXT & ~filters.COMMAND, callback_delayed_checkin), | ||
194 | ], | ||
195 | }, | ||
196 | fallbacks=[CommandHandler("cancel", cancel_command)], | ||
197 | per_message=False, | ||
198 | allow_reentry=True, | ||
199 | ) | ||
200 | |||
179 | application.add_handler(CommandHandler("logout", logout_command)) | 201 | application.add_handler(CommandHandler("logout", logout_command)) |
180 | application.add_handler(CommandHandler("list", list_command)) | 202 | application.add_handler(CommandHandler("list", list_command)) |
181 | application.add_handler(CommandHandler("Help", help_command)) | 203 | application.add_handler(CommandHandler("Help", help_command)) |
182 | application.add_handler(TypeHandler(type=FediLoginCallbackUpdate, callback=process_oauth_login_callback)) | 204 | application.add_handler(TypeHandler(type=FediLoginCallbackUpdate, callback=process_oauth_login_callback)) |
183 | 205 | ||
206 | application.add_handler(delayed_checkin_handler, 3) | ||
184 | application.add_handler(visibility_conversation_handler, 2) | 207 | application.add_handler(visibility_conversation_handler, 2) |
185 | application.add_handler(checkin_handler, 1) | 208 | application.add_handler(checkin_handler, 1) |
186 | 209 | ||