diff --git a/tg-bot/main.py b/tg-bot/main.py index 5e58595..e6a9f5c 100644 --- a/tg-bot/main.py +++ b/tg-bot/main.py @@ -152,9 +152,14 @@ async def poll_updates(): return print("[POLLING] Starting update polling") + retry_count = 0 + max_retries = 5 + while True: try: - updates = await bot.get_updates(offset=update_offset, timeout=30) + retry_count = 0 # Reset on successful request + print("[POLLING] Calling get_updates...") + updates = await bot.get_updates(offset=update_offset, timeout=30, allowed_updates=["callback_query"]) if updates: print(f"[POLLING] Received {len(updates)} updates") @@ -172,14 +177,25 @@ async def poll_updates(): ) await bot.answer_callback_query(query.id) print(f"[POLLING] Callback processed successfully") + else: + print(f"[POLLING] Update ID {update.update_id} is not a callback_query") + except asyncio.CancelledError: print("[POLLING] Polling task cancelled") break except Exception as e: - print(f"[POLLING] Error in polling: {e}") + retry_count += 1 + print(f"[POLLING] Error in polling (attempt {retry_count}/{max_retries}): {type(e).__name__}: {e}") import traceback traceback.print_exc() - await asyncio.sleep(5) + + if "Conflict" in str(e) or "terminated by other getUpdates" in str(e): + print("[POLLING] CONFLICT: Another bot instance is polling the same token!") + print("[POLLING] Check if another tg-bot container/process is running") + print("[POLLING] Waiting 10 seconds before retry...") + await asyncio.sleep(10) + else: + await asyncio.sleep(5) class MessageRequest(BaseModel): message: str @@ -267,11 +283,16 @@ async def health_check(): @app.post("/send", dependencies=[Depends(verify_token)]) async def send_message(request: MessageRequest): """Send a message to telegram chat with optional service control buttons""" + print(f"[SEND] Received message request: {request.message[:50]}...") + print(f"[SEND] service_id={request.service_id}, service_name={request.service_name}") + if not bot: + print("[SEND] ERROR: Bot not configured") raise HTTPException(status_code=503, detail="Telegram bot not configured") chat_id = request.chat_id or TELEGRAM_CHAT_ID if not chat_id: + print("[SEND] ERROR: No chat_id provided") raise HTTPException(status_code=400, detail="No chat_id provided and no default configured") try: @@ -285,6 +306,7 @@ async def send_message(request: MessageRequest): if request.service_id or request.service_name: # Use service_id if available, otherwise use service_name prefixed with 'name_' identifier = str(request.service_id) if request.service_id else f"name_{request.service_name}" + print(f"[SEND] Adding buttons with identifier: {identifier}") keyboard = [ [ InlineKeyboardButton("▶️ Start", callback_data=f"service_start_{identifier}"), @@ -293,17 +315,24 @@ async def send_message(request: MessageRequest): ] ] kwargs["reply_markup"] = InlineKeyboardMarkup(keyboard) + print(f"[SEND] Buttons added to message") + else: + print(f"[SEND] No buttons - no service_id or service_name provided") + print(f"[SEND] Sending message to chat {chat_id}") message = await bot.send_message(**kwargs) await log_message(int(chat_id), request.message, True) + print(f"[SEND] Message sent successfully, message_id={message.message_id}") return { "status": "sent", "message_id": message.message_id, - "chat_id": chat_id + "chat_id": chat_id, + "has_buttons": bool(request.service_id or request.service_name) } except TelegramError as e: + print(f"[SEND] TelegramError: {str(e)}") await log_message(int(chat_id), request.message, False, str(e)) raise HTTPException(status_code=500, detail=f"Failed to send message: {str(e)}")