Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .coverage
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
__pycache__/
*.pyc
bot_activity.log
.coverage
36 changes: 35 additions & 1 deletion src/handlers/common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import logging
import functools
from telegram import Update
from telegram.ext import CallbackContext
from telegram.ext import CallbackContext, ConversationHandler
from config import TIMEOUT_NAME, TIMEOUT_FIRE_NAME


Expand All @@ -22,6 +23,39 @@ async def wrapper(update: Update, context: CallbackContext, *args, **kwargs):
return wrapper


def cb_auth_required(func):
"""Decorator to restrict callback query handler access to allowed users only.
Reads the whitelist from context.bot_data['allowed_user_ids'].
Returns ConversationHandler.END when access is denied so the conversation
is cleanly terminated regardless of the current state.
"""
@functools.wraps(func)
async def wrapper(update: Update, context: CallbackContext, *args, **kwargs):
query = update.callback_query
user_id = str(query.from_user.id)
allowed_user_ids = context.bot_data.get('allowed_user_ids', set())
if user_id not in allowed_user_ids:
await query.answer("Access denied.", show_alert=True)
return ConversationHandler.END
return await func(update, context, *args, **kwargs)
return wrapper


def safe_delete_file(path: str) -> None:
"""Delete a temporary file safely, logging errors without raising.

:param path: Absolute or relative path to the file to delete.
"""
if not path:
return
try:
if os.path.exists(path):
os.remove(path)
logging.info("%s has been deleted.", path)
except Exception as e:
logging.error("Error deleting image file %s: %s", path, e)


async def handle_api_error(update: Update, error_data: dict) -> bool:
"""Handle API error responses uniformly.
Sends an appropriate error photo depending on error type.
Expand Down
Loading
Loading