Pick up changes for complete clean or harcode bot code
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
ea4eac3e30
commit
c48e191fc8
@ -6,7 +6,7 @@ from urllib.parse import urlparse
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
# Try to use requests; fallback to urllib
|
# Try to use requests; fallback to urllib d
|
||||||
try:
|
try:
|
||||||
import requests # type: ignore
|
import requests # type: ignore
|
||||||
_HAS_REQUESTS = True
|
_HAS_REQUESTS = True
|
||||||
|
@ -10,7 +10,7 @@ from django.core.cache import cache
|
|||||||
from asgiref.sync import sync_to_async
|
from asgiref.sync import sync_to_async
|
||||||
|
|
||||||
from .models import TelegramBot
|
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
|
from .renderer import render_spec
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
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)
|
await render_spec(bot=bot, chat_id=chat_id, spec=spec)
|
||||||
return JsonResponse({"status": "ok", "routed": True, "status_code": status})
|
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)
|
# Nothing matched → 204 (ack, but no action)
|
||||||
logger.warning("tg.no_route bot=%s trigger=%s cmd=%s", bot_name,
|
logger.warning(
|
||||||
(canon.get("command") or {}).get("trigger"),
|
"tg.no_route bot=%s trigger=%s cmd=%s",
|
||||||
(canon.get("command") or {}).get("name"))
|
bot_name,
|
||||||
|
(canon.get("command") or {}).get("trigger"),
|
||||||
|
(canon.get("command") or {}).get("name"),
|
||||||
|
)
|
||||||
return JsonResponse({"status": "no_route"}, status=204)
|
return JsonResponse({"status": "no_route"}, status=204)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user