diff --git a/app/routers/wechat_callback.py b/app/routers/wechat_callback.py index 55cf8a6..119f691 100644 --- a/app/routers/wechat_callback.py +++ b/app/routers/wechat_callback.py @@ -70,11 +70,19 @@ async def wechat_agent_message( asyncio.create_task(_process_agent_message(user_id, content)) return PlainTextResponse(content="success") + # ---- 图片消息 → Agent 图片分析 ---- + if msg_type == "image": + user_id = msg.get("FromUserName", "") + media_id = msg.get("MediaId", "") + logger.info(f"收到企微图片: user={user_id}, media_id={media_id}") + asyncio.create_task(_process_agent_image(user_id, media_id)) + return PlainTextResponse(content="success") + return PlainTextResponse(content="success") async def _process_agent_message(user_id: str, content: str): - """异步处理消息并主动回复""" + """异步处理文字消息并主动回复""" try: from app.services.agent_dispatcher import get_agent_dispatcher from app.services.wechat_service import get_wechat_service @@ -88,6 +96,30 @@ async def _process_agent_message(user_id: str, content: str): logger.error(f"Agent消息处理失败: user={user_id}, error={e}", exc_info=True) +async def _process_agent_image(user_id: str, media_id: str): + """异步处理图片消息:先回复"正在分析",再调 VLM 分析""" + try: + from app.services.agent_dispatcher import get_agent_dispatcher + from app.services.wechat_service import get_wechat_service + + wechat = get_wechat_service() + # 先回复提示,避免用户等太久 + await wechat.send_text_message(user_id, "正在分析图片,请稍候...") + + dispatcher = get_agent_dispatcher() + reply = await dispatcher.handle_image(user_id, media_id) + + await wechat.send_text_message(user_id, reply) + except Exception as e: + logger.error(f"Agent图片处理失败: user={user_id}, error={e}", exc_info=True) + try: + from app.services.wechat_service import get_wechat_service + wechat = get_wechat_service() + await wechat.send_text_message(user_id, "图片处理失败,请稍后重试。") + except Exception: + pass + + async def _process_card_button_click(msg: dict): """ 处理模板卡片按钮点击事件(两步状态机)