diff --git a/routes/openclaw_bot_routes.py b/routes/openclaw_bot_routes.py index 8aae02a..900bfe2 100644 --- a/routes/openclaw_bot_routes.py +++ b/routes/openclaw_bot_routes.py @@ -8684,6 +8684,9 @@ def _handle_event_ignore_callback(data: str, cq: dict, chat_id, message_id) -> N @openclaw_bot_bp.route('/bot/telegram/webhook', methods=['POST']) def telegram_webhook(): try: + # 每個 webhook request 先清空 ContextVar,避免同 worker thread 延用上一個 user。 + _CURRENT_USER_ID_CTX.set(None) + update = request.get_json(silent=True) if not update: return jsonify({'ok': True}) diff --git a/tests/test_openclaw_bot_routes_webhook.py b/tests/test_openclaw_bot_routes_webhook.py index f493d48..c51eb5d 100644 --- a/tests/test_openclaw_bot_routes_webhook.py +++ b/tests/test_openclaw_bot_routes_webhook.py @@ -56,6 +56,18 @@ def test_openclaw_answer_variants_are_menu_wakeup(): assert bot._looks_like_wakeup_prompt("你好") is True +def test_webhook_clears_stale_user_context_at_request_start(): + from routes import openclaw_bot_routes as bot + + bot._CURRENT_USER_ID_CTX.set(999) + + app = _build_request_app() + with app.test_request_context("/bot/telegram/webhook", method="POST", json=None): + bot.telegram_webhook() + + assert bot._CURRENT_USER_ID_CTX.get() is None + + def test_menu_command_returns_full_main_menu_keyboard(monkeypatch): from routes import openclaw_bot_routes as bot from services.openclaw_bot import menu_keyboards