企业微信机器人API实战:企业微信客服机器人与企业微信机器人开发的协议接入指南
将企业微信机器人API接入现有的企业内部系统需要严格遵循企业微信开放平台的通信协议规范。本文提供一套标准化的协议接入与联调指南。一、 接入前置条件企业微信后台配置在“应用管理”或“微信客服”中创建应用获取CorpID、Secret。配置可信IP白名单防止凭证泄露后被恶意调用。配置“接收消息服务器URL”并生成Token与EncodingAESKey。二、 核心API接口清单接口名称请求方式目标URL示例功能说明获取TokenGET[https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpidIDcorpsecretSECRET](https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpidIDcorpsecretSECRET)获取调用凭证发送客服消息POST[https://qyapi.weixin.qq.com/cgi-bin/kf/send_msg?access_tokenACCESS_TOKEN](https://qyapi.weixin.qq.com/cgi-bin/kf/send_msg?access_tokenACCESS_TOKEN)向客服会话发送消息接收消息回调POST开发者自定义URL接收用户上行消息三、 代码实战获取 Access Token 与发送主动消息使用 Python 的requests库实现凭证获取与消息下发import requests import json CORPID YOUR_CORPID SECRET YOUR_SECRET def get_access_token(corpid, secret): url fhttps://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid{corpid}corpsecret{secret} response requests.get(url) data response.json() if access_token in data: return data[access_token] else: raise Exception(fFailed to get token: {data}) def send_kf_message(access_token, touser, open_kfid, content): url fhttps://qyapi.weixin.qq.com/cgi-bin/kf/send_msg?access_token{access_token} payload { touser: touser, open_kfid: open_kfid, msgtype: text, text: { content: content } } response requests.post(url, datajson.dumps(payload)) return response.json()