Update main.py
This commit is contained in:
@@ -152,9 +152,14 @@ async def poll_updates():
|
|||||||
return
|
return
|
||||||
|
|
||||||
print("[POLLING] Starting update polling")
|
print("[POLLING] Starting update polling")
|
||||||
|
retry_count = 0
|
||||||
|
max_retries = 5
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
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:
|
if updates:
|
||||||
print(f"[POLLING] Received {len(updates)} updates")
|
print(f"[POLLING] Received {len(updates)} updates")
|
||||||
@@ -172,13 +177,24 @@ async def poll_updates():
|
|||||||
)
|
)
|
||||||
await bot.answer_callback_query(query.id)
|
await bot.answer_callback_query(query.id)
|
||||||
print(f"[POLLING] Callback processed successfully")
|
print(f"[POLLING] Callback processed successfully")
|
||||||
|
else:
|
||||||
|
print(f"[POLLING] Update ID {update.update_id} is not a callback_query")
|
||||||
|
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
print("[POLLING] Polling task cancelled")
|
print("[POLLING] Polling task cancelled")
|
||||||
break
|
break
|
||||||
except Exception as e:
|
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
|
import traceback
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
|
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)
|
await asyncio.sleep(5)
|
||||||
|
|
||||||
class MessageRequest(BaseModel):
|
class MessageRequest(BaseModel):
|
||||||
@@ -267,11 +283,16 @@ async def health_check():
|
|||||||
@app.post("/send", dependencies=[Depends(verify_token)])
|
@app.post("/send", dependencies=[Depends(verify_token)])
|
||||||
async def send_message(request: MessageRequest):
|
async def send_message(request: MessageRequest):
|
||||||
"""Send a message to telegram chat with optional service control buttons"""
|
"""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:
|
if not bot:
|
||||||
|
print("[SEND] ERROR: Bot not configured")
|
||||||
raise HTTPException(status_code=503, detail="Telegram bot not configured")
|
raise HTTPException(status_code=503, detail="Telegram bot not configured")
|
||||||
|
|
||||||
chat_id = request.chat_id or TELEGRAM_CHAT_ID
|
chat_id = request.chat_id or TELEGRAM_CHAT_ID
|
||||||
if not 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")
|
raise HTTPException(status_code=400, detail="No chat_id provided and no default configured")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -285,6 +306,7 @@ async def send_message(request: MessageRequest):
|
|||||||
if request.service_id or request.service_name:
|
if request.service_id or request.service_name:
|
||||||
# Use service_id if available, otherwise use service_name prefixed with '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}"
|
identifier = str(request.service_id) if request.service_id else f"name_{request.service_name}"
|
||||||
|
print(f"[SEND] Adding buttons with identifier: {identifier}")
|
||||||
keyboard = [
|
keyboard = [
|
||||||
[
|
[
|
||||||
InlineKeyboardButton("▶️ Start", callback_data=f"service_start_{identifier}"),
|
InlineKeyboardButton("▶️ Start", callback_data=f"service_start_{identifier}"),
|
||||||
@@ -293,17 +315,24 @@ async def send_message(request: MessageRequest):
|
|||||||
]
|
]
|
||||||
]
|
]
|
||||||
kwargs["reply_markup"] = InlineKeyboardMarkup(keyboard)
|
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)
|
message = await bot.send_message(**kwargs)
|
||||||
await log_message(int(chat_id), request.message, True)
|
await log_message(int(chat_id), request.message, True)
|
||||||
|
print(f"[SEND] Message sent successfully, message_id={message.message_id}")
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"status": "sent",
|
"status": "sent",
|
||||||
"message_id": message.message_id,
|
"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:
|
except TelegramError as e:
|
||||||
|
print(f"[SEND] TelegramError: {str(e)}")
|
||||||
await log_message(int(chat_id), request.message, False, 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)}")
|
raise HTTPException(status_code=500, detail=f"Failed to send message: {str(e)}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user