企业微信接口怎么接入智能体平台?API机器人与企业业务系统的连接方式
最近做的企微二开,公司选了个智能体平台(Dify/Coze 这类),要把企微机器人接到智能体平台上——企微负责消息收发,智能体平台负责对话理解和工具调用。和之前聊的 OpenClaw 接入不同,那篇重点是一个 AI 引擎的对接,这篇重点是智能体平台这种更完整的能力——它有自己的工作流编排、知识库、工具市场,企微只是它的一个消息通道。把怎么对接记下来。 Eyun 平台开放的企微 API,统一 POST+JSON,鉴权用 App Token 加 appid(Authorization: Bearer eyk_xxxx),路径 {BASE_URL}/wx-api/api/<模块>/<动作>,响应封套 {code, data, detail, message, time},code 为 0 成功。
智能体平台和企微的分工
先明确分工:
-
企微 API:消息收发、客户管理、群管理
-
智能体平台:对话理解、工作流编排、知识库检索、工具调用
企微不碰 AI 逻辑,智能体平台不碰企微接口。中间做个桥接层——企微消息转发给智能体平台,智能体平台回复转发回企微。
消息桥接:企微到智能体平台
客户在企微发消息,Webhook 回调进来,桥接层转发给智能体平台:
from flask import Flask, request
import requests
app = Flask(__name__)
BASE = "https://api.eyun.com"
HEADERS = {"Authorization": "Bearer eyk_xxxx", "Content-Type": "application/json"}
AGENT_API = "https://agent.example.com/chat" # 智能体平台API
@app.route("/wx-api/webhook/", methods=["POST"])
def webhook():
event = request.headers.get("X-Eyun-Event")
if event != "message":
return "ok"
payload = request.json["data"]
appid = payload["appid"]
from_uin = payload["fromUin"]
content = payload["content"]
# 1. 转发给智能体平台
reply = call_agent(appid, from_uin, content)
# 2. 调企微接口发回复
requests.post(
f"{BASE}/wx-api/api/message/sendText",
headers=HEADERS,
json={"appid": appid, "to": from_uin, "content": reply}
)
return "ok"
桥接层只做转发,不做 AI 处理。智能体平台返回文本,桥接层调 sendText 发给客户。
智能体平台调用
调智能体平台的对话接口,带上会话 ID 保证上下文连续:
def call_agent(appid, from_uin, content):
session_id = f"wecom:{appid}:{from_uin}"
resp = requests.post(
AGENT_API,
json={
"session_id": session_id,
"message": content,
"user_id": from_uin
},
timeout=30
)
return resp.json()["reply"]
session_id 用 appid 加 uin 保证唯一——同一客户在不同 appid 下对话不串。智能体平台内部维护对话历史和上下文,桥接层不用管。
工具调用协议转换
智能体平台可能要调企微接口(查客户、发消息),但智能体平台不懂企微接口格式。桥接层做协议转换:
def call_agent_with_tools(appid, from_uin, content):
session_id = f"wecom:{appid}:{from_uin}"
while True:
resp = requests.post(
AGENT_API,
json={"session_id": session_id, "message": content},
timeout=30
).json()
if resp.get("tool_call"):
# 智能体要调工具,桥接层执行企微接口
tool_result = execute_wecom_tool(appid, resp["tool_call"])
# 把结果给智能体继续
content = {"tool_result": tool_result, "tool_call_id": resp["tool_call"]["id"]}
continue
else:
return resp["reply"]
def execute_wecom_tool(appid, tool_call):
name = tool_call["name"]
args = tool_call["arguments"]
if name == "query_customer":
return query_customer(appid, args["keyword"])
elif name == "send_message":
return send_message(appid, args["to_uin"], args["content"])
智能体平台输出工具调用请求,桥接层映射到企微接口执行,结果返回给智能体。智能体平台不直接碰企微接口,所有接口调用在桥接层控制。
客户查询工具执行
智能体要查客户,桥接层调企微联系人接口:
def query_customer(appid, keyword):
if keyword.isdigit() and len(keyword) >= 11:
resp = requests.post(
f"{BASE}/wx-api/api/contact/phoneNumberSearch",
headers=HEADERS,
json={"appid": appid, "phone": keyword}
)
else:
resp = requests.post(
f"{BASE}/wx-api/api/contact/search",
headers=HEADERS,
json={"appid": appid, "keyword": keyword}
)
data = resp.json()["data"]
return {"name": data.get("nickName", ""), "phone": data.get("mobile", "")}
返回结构化数据给智能体平台,智能体基于数据生成自然语言回复。接口路径在开发文档。
状态同步
智能体平台和企微之间要同步状态——客户加好友、离职、换群,智能体平台要知道:
def sync_contact_event(appid, from_uin, event_type):
# 客户加好友,通知智能体平台初始化会话
if event_type == "external_contact_add":
requests.post(
f"{AGENT_API}/init_session",
json={"session_id": f"wecom:{appid}:{from_uin}", "user_id": from_uin}
)
加好友时初始化智能体会话,智能体平台知道这个客户来了。不做同步,智能体平台不知道客户状态变化,上下文对不上。
写在最后
接入智能体平台这套东西,本质是做个桥接层——企微 Webhook 收消息转发给智能体平台、智能体回复调 sendText 发回去、智能体的工具调用映射到企微接口执行、状态变更同步。企微和智能体平台互不直接耦合,桥接层控制所有接口调用。把桥接做对、协议转换做准、状态同步做实,智能体平台真正通过企微服务客户。
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐


所有评论(0)