Pick up changes for complete clean or harcode bot code
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ekaropolus 2025-09-17 01:28:13 -06:00
parent ea4eac3e30
commit c48e191fc8
2 changed files with 8 additions and 28 deletions

View File

@ -6,7 +6,7 @@ from urllib.parse import urlparse
logger = logging.getLogger(__name__)
# Try to use requests; fallback to urllib
# Try to use requests; fallback to urllib d
try:
import requests # type: ignore
_HAS_REQUESTS = True

View File

@ -10,7 +10,7 @@ from django.core.cache import cache
from asgiref.sync import sync_to_async
from .models import TelegramBot
from .router import pick_db_route, pick_url, post_json
from .router import pick_db_route, post_json
from .renderer import render_spec
logger = logging.getLogger(__name__)
@ -226,33 +226,13 @@ async def telegram_webhook(request, bot_name: str):
await render_spec(bot=bot, chat_id=chat_id, spec=spec)
return JsonResponse({"status": "ok", "routed": True, "status_code": status})
# ----- In-memory fallback (optional; remove when all routes live in DB) -----
try:
route_url = pick_url(bot_name, canon)
except Exception:
route_url = None
if route_url:
status, body = await sync_to_async(post_json)(route_url, canon)
logger.info("tg.routed(mem) url=%s status=%s", route_url, status)
spec = None
if isinstance(body, dict) and ("messages" in body or body.get("schema_version") == "render.v1"):
spec = body
elif isinstance(body, dict) and "text" in body:
spec = {"schema_version": "render.v1",
"messages": [{"type": "text", "text": str(body["text"])}]}
chat_id = (canon.get("chat") or {}).get("id")
if spec and chat_id:
bot = Bot(token=bot_instance.token)
await render_spec(bot=bot, chat_id=chat_id, spec=spec)
return JsonResponse({"status": "ok", "routed": True, "status_code": status})
# Nothing matched → 204 (ack, but no action)
logger.warning("tg.no_route bot=%s trigger=%s cmd=%s", bot_name,
(canon.get("command") or {}).get("trigger"),
(canon.get("command") or {}).get("name"))
logger.warning(
"tg.no_route bot=%s trigger=%s cmd=%s",
bot_name,
(canon.get("command") or {}).get("trigger"),
(canon.get("command") or {}).get("name"),
)
return JsonResponse({"status": "no_route"}, status=204)
except Exception as e: