Refactor Telegram bot login flow: clean UI with single editable message
- Consolidate login flow to use single message that edits in place - Auto-delete all user messages (email, code, password, /start command) - Remove all emojis from bot messages for cleaner interface - Fix "Retrimite Cod" bug - buttons now persist after resending code - Replace "Oracle" with "ROA" in all user-facing messages - Add clear instructions for each step (email input, code input, password) - Implement message tracking with context.user_data['login_message_id'] - Clean chat history - only menu message remains visible Files modified: - app/bot/email_handlers.py: Complete refactor of email login flow - app/bot/handlers.py: Update /start command to delete user message and edit existing message 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -54,24 +54,65 @@ async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
auth_code = args[0].upper()
|
||||
logger.info(f"Attempting to link user {telegram_user_id} with code {auth_code}")
|
||||
|
||||
# Show "linking..." message
|
||||
linking_msg = await update.message.reply_text(
|
||||
"Linking contul...\n"
|
||||
"Te rog asteapta..."
|
||||
)
|
||||
# ȘTERGE mesajul utilizatorului imediat (chat curat)
|
||||
try:
|
||||
await update.message.delete()
|
||||
except Exception as e:
|
||||
logger.warning(f"Could not delete /start message: {e}")
|
||||
|
||||
# Check dacă user-ul a apăsat pe "Login din Web App" înainte
|
||||
web_login_msg_id = context.user_data.get('web_login_message_id')
|
||||
|
||||
if web_login_msg_id:
|
||||
# EDITEAZĂ mesajul existent cu "Login din Web App"
|
||||
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
|
||||
)
|
||||
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)
|
||||
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(
|
||||
"Conectare cont...",
|
||||
parse_mode=ParseMode.MARKDOWN
|
||||
)
|
||||
|
||||
# Attempt linking
|
||||
result = await link_telegram_account(telegram_user, auth_code)
|
||||
|
||||
# Delete "linking..." message
|
||||
await linking_msg.delete()
|
||||
|
||||
if result:
|
||||
# Success!
|
||||
username = result['username']
|
||||
jwt_token = result['jwt_token']
|
||||
|
||||
# Show main menu with buttons for newly linked user
|
||||
# Get session and company info
|
||||
session_manager = get_session_manager()
|
||||
session = await session_manager.get_or_create_session(telegram_user_id)
|
||||
company = session.get_active_company()
|
||||
@@ -92,24 +133,16 @@ async def start_command(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, cache_enabled=cache_enabled)
|
||||
|
||||
# Single welcome message with menu
|
||||
# EDIT message to show menu with company
|
||||
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,
|
||||
await linking_msg.edit_text(
|
||||
menu_message,
|
||||
reply_markup=keyboard,
|
||||
parse_mode=ParseMode.MARKDOWN
|
||||
)
|
||||
@@ -117,12 +150,11 @@ async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
logger.info(f"User {telegram_user_id} successfully linked to {username}")
|
||||
|
||||
else:
|
||||
# Failed linking
|
||||
await update.message.reply_text(
|
||||
"**Cod invalid sau expirat**\n\n"
|
||||
# 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`\n\n"
|
||||
"Codul expiră în 15 minute.",
|
||||
"/start CODUL_TAU",
|
||||
parse_mode=ParseMode.MARKDOWN
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user