企业微信接口开发实战:如何让AI机器人读取企业微信文档并执行任务
最近做的企微二开,要求 AI 机器人能读取企微里的聊天记录、客户档案、群消息,基于这些信息执行任务——客户问"上周聊了什么",机器人从历史消息里找;销售问"这个客户什么标签",机器人从客户档案读。这篇重点讲 AI 读取企微数据后怎么执行任务——调哪些接口读数据、数据怎么给 AI、AI 怎么基于数据执行。把调了哪些接口记下来。
Eyun 平台开放的企微 API,统一 POST+JSON,鉴权用 App Token 加 appid(Authorization: Bearer eyk_xxxx),路径 {BASE_URL}/wx-api/api/<模块>/<动作>,响应封套 {code, data, detail, message, time},code 为 0 成功。
读取历史消息:调消息接口
AI 要读对话历史,调消息模块拉历史消息:
import requests
BASE = "https://api.eyun.com"
HEADERS = {"Authorization": "Bearer eyk_xxxx", "Content-Type": "application/json"}
def get_history(appid, conversation_id, limit=20):
resp = requests.post(
f"{BASE}/wx-api/api/message/getHistoryMessageList",
headers=HEADERS,
json={"appid": appid, "conversationId": conversation_id, "limit": limit}
)
msgs = resp.json()["data"]["list"]
return [{"from": m["fromUin"], "content": m["content"], "time": m["time"]}
for m in msgs]
conversationId 单聊是客户 uin,群聊是 roomId,必须是 number。返回的消息按时间倒序,要反转成正序给 AI。limit 控制拉多少条,太多 token 爆炸。
读取客户档案:调联系人接口
AI 要读客户信息,调联系人模块:
def get_customer_profile(appid, uin):
resp = requests.post(
f"{BASE}/wx-api/api/contact/getUserProfileDetail",
headers=HEADERS,
json={"appid": appid, "uin": uin}
)
data = resp.json()["data"]
return {
"name": data.get("nickName", ""),
"phone": data.get("mobile", ""),
"labels": [t["name"] for t in data.get("labelList", [])],
"corp": data.get("corpName", "")
}
客户档案是 AI 理解客户的基础——知道客户是谁、什么标签、哪个公司,才能针对性回答。
读取群成员:调群接口
AI 在群场景要读群成员,调群模块:
def get_group_members(appid, room_id):
resp = requests.post(
f"{BASE}/wx-api/api/group/getGroupMemberList",
headers=HEADERS,
json={"appid": appid, "roomId": room_id}
)
return resp.json()["data"]["members"]
群成员列表让 AI 知道群里有谁,回答"群里谁负责售后"这类问题。
AI 基于数据执行任务
读到的数据给 AI,AI 生成回复或执行操作。以"客户问上周聊了什么"为例:
def handle_query(appid, from_uin, question):
# 1. 读历史消息
history = get_history(appid, from_uin, limit=50)
# 2. 读客户档案
profile = get_customer_profile(appid, from_uin)
# 3. 把数据组织成AI上下文
context = f"""
客户:{profile['name']},标签:{', '.join(profile['labels'])}
最近对话:
{format_history(history)}
"""
# 4. AI基于上下文回答
reply = llm.chat(question, context=context)
# 5. 调企微接口发回复
requests.post(
f"{BASE}/wx-api/api/message/sendText",
headers=HEADERS,
json={"appid": appid, "to": from_uin, "content": reply}
)
AI 不直接调企微接口读数据——所有读取在后端控制,AI 只拿组织好的文本。这样能控制 AI 读什么不读什么,敏感数据可以脱敏。
执行操作:AI 输出意图后调接口
AI 基于读到的数据可能要执行操作——客户说"帮我打标签",AI 识别意图后调标签接口:
def handle_action(appid, from_uin, question, context):
# AI判断要执行什么操作
action = llm.extract_action(question, context)
if action["type"] == "update_label":
# 执行打标签
resp = requests.post(
f"{BASE}/wx-api/api/label/updateLabel",
headers=HEADERS,
json={"appid": appid, "uin": from_uin, "labels": action["labels"]}
)
result = "已打标签" if resp.json()["code"] == 0 else "打标签失败"
# 回复客户
requests.post(
f"{BASE}/wx-api/api/message/sendText",
headers=HEADERS,
json={"appid": appid, "to": from_uin, "content": result}
)
AI 只输出操作意图和参数,实际接口调用在后端执行——能加权限校验、日志、频率控制。接口路径和参数在Eyun 开发文档。
数据脱敏:敏感信息不给 AI
读到的数据给 AI 前要脱敏——手机号、身份证、金额不能明文给 AI:
def mask_sensitive(data):
if "phone" in data and data["phone"]:
data["phone"] = data["phone"][:3] + "****" + data["phone"][-4:]
if "amount" in data:
data["amount"] = "***"
return data
脱敏后 AI 不会在回复里泄露敏感信息。不脱敏,AI 可能把客户手机号发给群里,合规出事。关于数据保护和接口权限,在Eyun 企业微信 API 平台有说明。
写在最后
AI 读取企微文档执行任务这套东西,本质是把企微的消息接口、联系人接口、群接口、标签接口串起来——getHistoryMessageList 读历史、getUserProfileDetail 读客户档案、getGroupMemberList 读群成员、updateLabel 执行操作、sendText 发回复。AI 只负责理解和决策,所有接口调用在后端控制,能加脱敏和权限。把读取做对、脱敏做严、执行可控,AI 机器人真正能基于企微数据办事。
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐



所有评论(0)