ilang-ai commited on
Commit ·
57a8795
1
Parent(s): f194fc6
fix: webhook mode starts listening immediately, no AI test blocking
Browse filesCloud Run kills container if it doesn't listen on PORT within timeout.
AI startup test was blocking before run_webhook(). Now:
- Webhook mode: init_db → run_webhook immediately
- Polling mode: init_db → AI test → run_polling (unchanged)
bot.py
CHANGED
|
@@ -468,32 +468,13 @@ def main():
|
|
| 468 |
asyncio.set_event_loop(loop)
|
| 469 |
loop.run_until_complete(init_db())
|
| 470 |
|
| 471 |
-
# Startup test: verify Gemini API works
|
| 472 |
-
async def _test_ai():
|
| 473 |
-
try:
|
| 474 |
-
from modules.chat import model, _safe_text
|
| 475 |
-
r = await model.generate_content_async("Say hello in one word. JSON: {\"intent\":\"chat\",\"reply\":\"your word\"}", safety_settings=[
|
| 476 |
-
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
| 477 |
-
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
| 478 |
-
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
|
| 479 |
-
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
| 480 |
-
])
|
| 481 |
-
text = _safe_text(r)
|
| 482 |
-
if text:
|
| 483 |
-
logger.info("AI startup test OK: " + text[:100])
|
| 484 |
-
else:
|
| 485 |
-
logger.error("AI startup test FAILED: empty response")
|
| 486 |
-
except Exception as e:
|
| 487 |
-
logger.error("AI startup test EXCEPTION: " + str(e))
|
| 488 |
-
loop.run_until_complete(_test_ai())
|
| 489 |
-
|
| 490 |
# Detect mode: webhook (Cloud Run / Railway) or polling (VPS)
|
| 491 |
webhook_url = os.environ.get("WEBHOOK_URL", "")
|
| 492 |
port = int(os.environ.get("PORT", 8080))
|
| 493 |
|
| 494 |
if webhook_url:
|
| 495 |
-
# Webhook mode: Cloud Run
|
| 496 |
-
logger.info("I-Lang Guard starting (webhook
|
| 497 |
app.run_webhook(
|
| 498 |
listen="0.0.0.0",
|
| 499 |
port=port,
|
|
@@ -503,7 +484,25 @@ def main():
|
|
| 503 |
allowed_updates=["message", "callback_query", "my_chat_member"],
|
| 504 |
)
|
| 505 |
else:
|
| 506 |
-
# Polling mode:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 507 |
start_health_server()
|
| 508 |
logger.info("I-Lang Guard starting (polling mode)")
|
| 509 |
app.run_polling(
|
|
|
|
| 468 |
asyncio.set_event_loop(loop)
|
| 469 |
loop.run_until_complete(init_db())
|
| 470 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 471 |
# Detect mode: webhook (Cloud Run / Railway) or polling (VPS)
|
| 472 |
webhook_url = os.environ.get("WEBHOOK_URL", "")
|
| 473 |
port = int(os.environ.get("PORT", 8080))
|
| 474 |
|
| 475 |
if webhook_url:
|
| 476 |
+
# Webhook mode: start listening IMMEDIATELY (Cloud Run health check is strict)
|
| 477 |
+
logger.info("I-Lang Guard starting (webhook: " + webhook_url + ")")
|
| 478 |
app.run_webhook(
|
| 479 |
listen="0.0.0.0",
|
| 480 |
port=port,
|
|
|
|
| 484 |
allowed_updates=["message", "callback_query", "my_chat_member"],
|
| 485 |
)
|
| 486 |
else:
|
| 487 |
+
# Polling mode: run AI test first, then start
|
| 488 |
+
async def _test_ai():
|
| 489 |
+
try:
|
| 490 |
+
from modules.chat import model, _safe_text
|
| 491 |
+
r = await model.generate_content_async("Say hi in one word. JSON: {\"intent\":\"chat\",\"reply\":\"hi\"}", safety_settings=[
|
| 492 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
| 493 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
| 494 |
+
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
|
| 495 |
+
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
| 496 |
+
])
|
| 497 |
+
text = _safe_text(r)
|
| 498 |
+
if text:
|
| 499 |
+
logger.info("AI startup test OK: " + text[:100])
|
| 500 |
+
else:
|
| 501 |
+
logger.error("AI startup test FAILED: empty response")
|
| 502 |
+
except Exception as e:
|
| 503 |
+
logger.error("AI startup test EXCEPTION: " + str(e))
|
| 504 |
+
loop.run_until_complete(_test_ai())
|
| 505 |
+
|
| 506 |
start_health_server()
|
| 507 |
logger.info("I-Lang Guard starting (polling mode)")
|
| 508 |
app.run_polling(
|