ForcePilot/backend/package/yuxi/channel/extensions/wecom/events.py
Kris 7215418610 feat(channel): 添加企业微信、微博、WhatsApp 和 Workplace 渠道扩展
新增企业微信、微博、WhatsApp、Workplace 四个渠道扩展。

企业微信渠道扩展主要模块:config, gateway, webhook, webhook_bot, outbound, streaming, pairing, security, crypto, dedupe, persistent_dedupe, card, directory, events, externalcontact, media, mentions, menu, message, oauth, status

微博渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, passive_reply, broadcast, message, menu, media, subscription, template, status

WhatsApp 渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, actions, monitor, status

Workplace 渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, actions, challenge, groups, media, mentions, menu, monitor, persona, quick_reply, signature, subscriptions, template, threading, users, status
2026-05-21 12:01:56 +08:00

90 lines
3.1 KiB
Python

import logging
from yuxi.channel.extensions.wecom.types import InboundWeComMessage
logger = logging.getLogger(__name__)
async def handle_event(msg: InboundWeComMessage) -> str | None:
event = msg.event
event_key = msg.event_key
if event == "click":
return await _handle_click(msg, event_key)
if event == "view":
logger.info("WeCom view event: key=%s user=%s", event_key, msg.from_user)
return None
if event == "scancode_push":
return await _handle_scancode(msg, event_key)
if event == "scancode_waitmsg":
return await _handle_scancode(msg, event_key)
if event in ("subscribe", "enter_agent"):
return await _handle_enter(msg)
if event == "unsubscribe":
logger.info("WeCom unsubscribe: user=%s", msg.from_user)
return None
if event == "location_select":
return await _handle_location_select(msg)
if event == "change_external_contact":
return await _handle_external_contact_change(msg)
if event == "change_external_chat":
return await _handle_external_chat_change(msg)
if event in ("pic_sysphoto", "pic_photo_or_album", "pic_weixin"):
logger.info("WeCom pic event: %s user=%s", event, msg.from_user)
return None
logger.debug("WeCom unhandled event: %s user=%s", event, msg.from_user)
return None
async def _handle_click(msg: InboundWeComMessage, event_key: str) -> str | None:
logger.info("WeCom click event: key=%s user=%s", event_key, msg.from_user)
return f"菜单点击: {event_key}"
async def _handle_enter(msg: InboundWeComMessage) -> str | None:
logger.info("WeCom enter event: user=%s", msg.from_user)
return "你好!我是 AI 助手,有什么可以帮你的?"
async def _handle_scancode(msg: InboundWeComMessage, event_key: str) -> str | None:
logger.info("WeCom scancode event: key=%s user=%s", event_key, msg.from_user)
return f"扫码事件: {event_key}"
async def _handle_location_select(msg: InboundWeComMessage) -> str | None:
logger.info(
"WeCom location_select event: user=%s content=%s",
msg.from_user,
msg.content,
)
return None
async def _handle_external_contact_change(msg: InboundWeComMessage) -> str | None:
change_type = msg.raw_xml.get("ChangeType", "")
external_user_id = msg.raw_xml.get("ExternalUserID", "")
if change_type == "add_external_contact":
logger.info(
"New external contact added: user=%s external=%s",
msg.from_user,
external_user_id,
)
return "你好!欢迎添加我为联系人,有什么可以帮你的?"
if change_type == "del_external_contact":
logger.info(
"External contact removed: user=%s external=%s",
msg.from_user,
external_user_id,
)
return None
async def _handle_external_chat_change(msg: InboundWeComMessage) -> str | None:
change_type = msg.raw_xml.get("ChangeType", "")
chat_id = msg.raw_xml.get("ChatId", "")
logger.info("External chat change: type=%s chat=%s", change_type, chat_id)
return None