Spaces:
Running

axeronis commited on
Commit
4b3688e
·
1 Parent(s): 79c4ca3

Fix: /start now sends welcome message (not ID) in tenant bot

Browse files
Files changed (1) hide show
  1. backend/app/api/telegram_webhook.py +4 -45
backend/app/api/telegram_webhook.py CHANGED
@@ -265,51 +265,10 @@ async def process_telegram_message(
265
 
266
  logger.info(f"✅ Tenant found, bot_token={'present' if bot_token else 'MISSING'}")
267
 
268
- # [NEW] Handle /start command for ID retrieval and Auto-Linking
269
- if text and text.strip().lower().startswith("/start"):
270
- # Extract payload: /start <payload>
271
- parts = text.strip().split()
272
- payload = parts[1] if len(parts) > 1 else None
273
-
274
- user_updated = False
275
- user_email = ""
276
-
277
- # Check if payload is a UUID (User ID)
278
- if payload:
279
- try:
280
- # Try to find user by ID across ALL tenants (since bot is global)
281
- # We use execute with select(User)
282
- from app.models.user import User
283
- from sqlmodel import select
284
-
285
- stmt = select(User).where(User.id == UUID(payload))
286
- result = await session.execute(stmt)
287
- user = result.scalar_one_or_none()
288
-
289
- if user:
290
- user.telegram_chat_id = chat_id
291
- session.add(user)
292
- await session.commit()
293
- user_updated = True
294
- user_email = user.email
295
- logger.info(f"✅ Auto-linked Telegram {chat_id} to User {user.id} ({user.email})")
296
- except Exception as e:
297
- logger.warning(f"Failed to auto-link user with payload {payload}: {e}")
298
-
299
- # Reply
300
- if user_updated:
301
- msg = f"✅ <b>Уведомления подключены!</b>\n\n👤 Пользователь: {user_email}\n🆔 ID Чата: <code>{chat_id}</code>"
302
- else:
303
- msg = f"✅ <b>Бот подключен!</b>\n\n🆔 Ваш User ID: <code>{chat_id}</code>\n\nСкопируйте этот ID в настройки уведомлений MicroFunnel."
304
-
305
- await telegram_service.send_message(
306
- token=bot_token,
307
- chat_id=chat_id,
308
- text=msg
309
- )
310
- logger.info(f"Sent response to /start (Linked: {user_updated})")
311
- return
312
-
313
  # 2. Process Media (Voice, Photo, Doc)
314
  is_voice = False
315
  media_url = None
 
265
 
266
  logger.info(f"✅ Tenant found, bot_token={'present' if bot_token else 'MISSING'}")
267
 
268
+ # NOTE: /start is handled in the AI processing flow (webhook.py)
269
+ # which sends the welcome message. We don't intercept it here.
270
+ # The ID message is only sent by the global notification bot (/tg-global).
271
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
272
  # 2. Process Media (Voice, Photo, Doc)
273
  is_voice = False
274
  media_url = None