Refactor: Separate logic for abort_processing handler in TelegramHelper
This commit is contained in:
+12
-10
@@ -37,8 +37,8 @@ class TelegramHelper:
|
|||||||
|
|
||||||
async def clear(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: # Modified
|
async def clear(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: # Modified
|
||||||
user_id = update.effective_user.id
|
user_id = update.effective_user.id
|
||||||
response_message = await self._clear_logic(user_id) # Await was missing for async consistency, though _clear_logic is not async yet, it calls a sync method.
|
response_message = await self._clear_logic(user_id)
|
||||||
# For consistency with other _logic methods, making it async.
|
await update.message.reply_text(response_message)
|
||||||
|
|
||||||
# --- Status Command ---
|
# --- Status Command ---
|
||||||
async def _status_logic(self) -> str: # New logic method
|
async def _status_logic(self) -> str: # New logic method
|
||||||
@@ -100,13 +100,18 @@ class TelegramHelper:
|
|||||||
logging.error(f"An error occurred: {str(e)}")
|
logging.error(f"An error occurred: {str(e)}")
|
||||||
await update.message.reply_text("Sorry, an error occurred while processing your request.")
|
await update.message.reply_text("Sorry, an error occurred while processing your request.")
|
||||||
|
|
||||||
async def abort_processing(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
# --- Abort Processing (Callback) ---
|
||||||
|
async def _abort_processing_logic(self, user_id: int) -> str: # New logic method
|
||||||
|
return await self.bot.abort_processing(user_id)
|
||||||
|
|
||||||
|
async def abort_processing(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: # Modified
|
||||||
query = update.callback_query
|
query = update.callback_query
|
||||||
await query.answer()
|
await query.answer() # Telegram specific interaction
|
||||||
|
|
||||||
user_id = query.from_user.id
|
user_id = query.from_user.id
|
||||||
result = await self.bot.abort_processing(user_id)
|
response_text = await self._abort_processing_logic(user_id) # Call logic method
|
||||||
await query.edit_message_text(text=result)
|
|
||||||
|
await query.edit_message_text(text=response_text) # Telegram specific interaction
|
||||||
|
|
||||||
async def reboot(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
async def reboot(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
user_message = update.message.text.split()
|
user_message = update.message.text.split()
|
||||||
@@ -120,9 +125,6 @@ class TelegramHelper:
|
|||||||
reboot_f_path = self.reboot_file
|
reboot_f_path = self.reboot_file
|
||||||
if not os.path.exists(reboot_f_path):
|
if not os.path.exists(reboot_f_path):
|
||||||
with open(reboot_f_path, 'w') as f:
|
with open(reboot_f_path, 'w') as f:
|
||||||
# If update is None (e.g. called programmatically without a Telegram context for reboot),
|
|
||||||
# we should handle this. For now, assuming update is present if this handler is called by Telegram.
|
|
||||||
# Testability of this part needs care due to sys.exit()
|
|
||||||
chat_id_to_write = str(update.effective_chat.id) if update and update.effective_chat else ""
|
chat_id_to_write = str(update.effective_chat.id) if update and update.effective_chat else ""
|
||||||
f.write(chat_id_to_write)
|
f.write(chat_id_to_write)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
@@ -161,6 +163,6 @@ class TelegramHelper:
|
|||||||
if loop.is_running():
|
if loop.is_running():
|
||||||
loop.create_task(self.check_doreboot_file(application))
|
loop.create_task(self.check_doreboot_file(application))
|
||||||
else:
|
else:
|
||||||
asyncio.run(self.check_doreboot_file(application)) # Fallback if loop not running (e.g. tests)
|
asyncio.run(self.check_doreboot_file(application))
|
||||||
|
|
||||||
application.run_polling()
|
application.run_polling()
|
||||||
|
|||||||
Reference in New Issue
Block a user