diff --git a/reports-app/telegram-bot/app/bot/handlers.py b/reports-app/telegram-bot/app/bot/handlers.py index b300470..a514f87 100644 --- a/reports-app/telegram-bot/app/bot/handlers.py +++ b/reports-app/telegram-bot/app/bot/handlers.py @@ -72,37 +72,22 @@ async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE): text="Conectare cont...", parse_mode=ParseMode.MARKDOWN ) - linking_msg = await context.bot.get_updates() # Dummy, ne interesează doar message_id - # Simulăm un obiect Message pentru a putea folosi .edit_text() mai jos - class FakeMessage: - def __init__(self, chat_id, message_id, bot): - self.chat_id = chat_id - self.message_id = message_id - self._bot = bot - async def edit_text(self, text, reply_markup=None, parse_mode=None): - await self._bot.edit_message_text( - chat_id=self.chat_id, - message_id=self.message_id, - text=text, - reply_markup=reply_markup, - parse_mode=parse_mode - ) - linking_msg = FakeMessage(update.effective_chat.id, web_login_msg_id, context.bot) - # Clear message_id din context - context.user_data.pop('web_login_message_id', None) + # IMPORTANT: NU ștergem message_id - îl păstrăm pentru editări ulterioare except Exception as e: logger.warning(f"Could not edit web_login message: {e}") - # Fallback: creează mesaj nou - linking_msg = await update.effective_chat.send_message( - "Conectare cont...", - parse_mode=ParseMode.MARKDOWN - ) - else: - # Nu există mesaj anterior - creează mesaj nou - linking_msg = await update.effective_chat.send_message( + # Fallback: creează mesaj nou dacă editarea a eșuat + web_login_msg_id = None + + # Dacă nu avem mesaj de editat, creăm unul nou + if not web_login_msg_id: + # Creează mesaj nou și salvează ID-ul în context pentru editări ulterioare + new_msg = await update.effective_chat.send_message( "Conectare cont...", parse_mode=ParseMode.MARKDOWN ) + web_login_msg_id = new_msg.message_id + # Salvează în context pentru consistență + context.user_data['web_login_message_id'] = web_login_msg_id # Attempt linking result = await link_telegram_account(telegram_user, auth_code) @@ -141,8 +126,11 @@ async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE): menu_message = pad_message_for_wide_buttons(menu_text) - await linking_msg.edit_text( - menu_message, + # EDIT the login message (same message throughout the flow) + await context.bot.edit_message_text( + chat_id=update.effective_chat.id, + message_id=web_login_msg_id, + text=menu_message, reply_markup=keyboard, parse_mode=ParseMode.MARKDOWN ) @@ -151,10 +139,12 @@ async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE): else: # Failed linking - EDIT message to show error - await linking_msg.edit_text( - "Cod invalid sau expirat\n\n" - "Generează un cod nou din aplicația web și trimite:\n" - "/start CODUL_TAU", + await context.bot.edit_message_text( + chat_id=update.effective_chat.id, + message_id=web_login_msg_id, + text="Cod invalid sau expirat\n\n" + "Generează un cod nou din aplicația web și trimite:\n" + "/start CODUL_TAU", parse_mode=ParseMode.MARKDOWN ) @@ -1306,18 +1296,40 @@ async def handle_text_message(update: Update, context: ContextTypes.DEFAULT_TYPE if len(text) == 8 and text.isalnum(): logger.info(f"Detected potential linking code: {text} from user {telegram_user_id}") - # Show "linking..." message - linking_msg = await update.message.reply_text( - "Linking contul...\n" - "Te rog asteapta..." - ) + # ȘTERGE mesajul utilizatorului cu codul (chat curat) + try: + await update.message.delete() + except Exception as e: + logger.warning(f"Could not delete code message: {e}") + + # Check dacă există mesaj de editat din "Login din Web App" + web_login_msg_id = context.user_data.get('web_login_message_id') + + if web_login_msg_id: + # EDITEAZĂ mesajul existent cu instrucțiunile + try: + await context.bot.edit_message_text( + chat_id=update.effective_chat.id, + message_id=web_login_msg_id, + text="Conectare cont...", + parse_mode=ParseMode.MARKDOWN + ) + except Exception as e: + logger.warning(f"Could not edit web_login message: {e}") + # Fallback: creează mesaj nou + web_login_msg_id = None + + # Dacă nu există mesaj de editat, creează unul nou + if not web_login_msg_id: + linking_msg = await update.effective_chat.send_message( + "Conectare cont...", + parse_mode=ParseMode.MARKDOWN + ) + web_login_msg_id = linking_msg.message_id # Attempt linking result = await link_telegram_account(telegram_user, text) - # Delete "linking..." message - await linking_msg.delete() - if result: # Success! username = result['username'] @@ -1331,35 +1343,32 @@ async def handle_text_message(update: Update, context: ContextTypes.DEFAULT_TYPE from app.bot.menus import create_main_menu, pad_message_for_wide_buttons keyboard = create_main_menu(company_name, company_cui, is_authenticated=True) - # Single welcome message with menu + # Create menu text if company_name: - welcome_text = ( - f"**Cont conectat cu succes**\n\n" - f"Bun venit, **{username}**!\n\n" - f"{company_name}" - ) + menu_text = f"{company_name}" else: - welcome_text = ( - f"**Cont conectat cu succes**\n\n" - f"Bun venit, **{username}**!\n\n" - f"Selectează o companie pentru a continua" - ) + menu_text = "Selectează o companie pentru a continua" - welcome_message = pad_message_for_wide_buttons(welcome_text) + menu_message = pad_message_for_wide_buttons(menu_text) - await update.message.reply_text( - welcome_message, + # EDIT the same message to show menu (no new messages!) + await context.bot.edit_message_text( + chat_id=update.effective_chat.id, + message_id=web_login_msg_id, + text=menu_message, reply_markup=keyboard, parse_mode=ParseMode.MARKDOWN ) logger.info(f"User {telegram_user_id} successfully linked to {username} via direct code input") else: - # Failed linking - await update.message.reply_text( - "**Cod invalid sau expirat**\n\n" - "Genereaza un cod nou din aplicatia web si trimite-l direct.\n\n" - "Codul expira in 15 minute.", + # Failed linking - EDIT the same message to show error + await context.bot.edit_message_text( + chat_id=update.effective_chat.id, + message_id=web_login_msg_id, + text="Cod invalid sau expirat\n\n" + "Generează un cod nou din aplicația web și trimite-l direct.\n\n" + "Codul expiră în 15 minute.", parse_mode=ParseMode.MARKDOWN )