ilang-ai commited on
Commit ·
3e53fa5
1
Parent(s): 1a4abb9
v4.4: webhook mode for Cloud Run / Railway / Render
Browse files- Auto-detect: WEBHOOK_URL set → webhook mode, otherwise → polling mode
- Webhook: listens on PORT (default 8080), registers /webhook endpoint
- Polling: unchanged behavior for VPS / local dev
- Dockerfile: PORT=8080 for Cloud Run default
- Health check only starts in polling mode (webhook has its own HTTP)
- Dockerfile +3 -1
- bot.py +33 -14
Dockerfile
CHANGED
|
@@ -9,6 +9,8 @@ COPY . .
|
|
| 9 |
|
| 10 |
RUN mkdir -p data
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
| 14 |
CMD ["python", "bot.py"]
|
|
|
|
| 9 |
|
| 10 |
RUN mkdir -p data
|
| 11 |
|
| 12 |
+
# Cloud Run uses 8080, HF Space uses 7860
|
| 13 |
+
ENV PORT=8080
|
| 14 |
+
EXPOSE 8080
|
| 15 |
|
| 16 |
CMD ["python", "bot.py"]
|
bot.py
CHANGED
|
@@ -438,10 +438,10 @@ def start_health_server():
|
|
| 438 |
# ==================== Main ====================
|
| 439 |
|
| 440 |
def main():
|
| 441 |
-
# Health check for HF Space
|
| 442 |
-
start_health_server()
|
| 443 |
# Ensure data directory exists
|
| 444 |
-
os.
|
|
|
|
|
|
|
| 445 |
|
| 446 |
app = Application.builder().token(config.BOT_TOKEN).connect_timeout(30).read_timeout(30).write_timeout(30).pool_timeout(30).build()
|
| 447 |
|
|
@@ -472,26 +472,45 @@ def main():
|
|
| 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\"}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 476 |
text = _safe_text(r)
|
| 477 |
if text:
|
| 478 |
logger.info("AI startup test OK: " + text[:100])
|
| 479 |
else:
|
| 480 |
logger.error("AI startup test FAILED: empty response")
|
| 481 |
-
if hasattr(r, 'prompt_feedback'):
|
| 482 |
-
logger.error("Feedback: " + str(r.prompt_feedback))
|
| 483 |
-
if hasattr(r, 'candidates') and r.candidates:
|
| 484 |
-
logger.error("Candidates: " + str(r.candidates))
|
| 485 |
except Exception as e:
|
| 486 |
logger.error("AI startup test EXCEPTION: " + str(e))
|
| 487 |
loop.run_until_complete(_test_ai())
|
| 488 |
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 495 |
|
| 496 |
|
| 497 |
if __name__ == "__main__":
|
|
|
|
| 438 |
# ==================== Main ====================
|
| 439 |
|
| 440 |
def main():
|
|
|
|
|
|
|
| 441 |
# Ensure data directory exists
|
| 442 |
+
db_dir = os.path.dirname(config.DB_PATH)
|
| 443 |
+
if db_dir:
|
| 444 |
+
os.makedirs(db_dir, exist_ok=True)
|
| 445 |
|
| 446 |
app = Application.builder().token(config.BOT_TOKEN).connect_timeout(30).read_timeout(30).write_timeout(30).pool_timeout(30).build()
|
| 447 |
|
|
|
|
| 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, Railway, Render, etc.
|
| 496 |
+
logger.info("I-Lang Guard starting (webhook mode: " + webhook_url + ")")
|
| 497 |
+
app.run_webhook(
|
| 498 |
+
listen="0.0.0.0",
|
| 499 |
+
port=port,
|
| 500 |
+
url_path="webhook",
|
| 501 |
+
webhook_url=webhook_url + "/webhook",
|
| 502 |
+
drop_pending_updates=True,
|
| 503 |
+
allowed_updates=["message", "callback_query", "my_chat_member"],
|
| 504 |
+
)
|
| 505 |
+
else:
|
| 506 |
+
# Polling mode: VPS, local dev
|
| 507 |
+
start_health_server()
|
| 508 |
+
logger.info("I-Lang Guard starting (polling mode)")
|
| 509 |
+
app.run_polling(
|
| 510 |
+
drop_pending_updates=True,
|
| 511 |
+
allowed_updates=["message", "callback_query", "my_chat_member"],
|
| 512 |
+
bootstrap_retries=10
|
| 513 |
+
)
|
| 514 |
|
| 515 |
|
| 516 |
if __name__ == "__main__":
|