制作一个微信好友添加机器人:acceptRequest链路
·
好友添加是私域流量的入口。WTAPI通过Webhook回调好友请求事件,业务方调用 friend/acceptRequest 接受请求,之后还可以链式调用 friend/remark 改备注、friend/tag 打标签,实现"加好友→自动备注→打标签→发欢迎语"的完整链路。
api文档 WTAPI框架文档weiti.apifox.cn 。
一、好友请求事件回调
当有人向你的微信号发起好友请求时,WTAPI会通过Webhook推送好友请求事件。回调里包含请求人信息(如 fromWxid、content 验证消息等),具体字段以官方文档为准。
@app.route("/webhook", methods=["POST"])
def webhook():
data = request.json
event = data.get("event")
if event == "friend_request":
from_wxid = data.get("fromWxid")
verify_content = data.get("content", "")
instance_id = data.get("instanceId")
# 投递到好友处理队列
redis.lpush("friend_req_queue", json.dumps({
"fromWxid": from_wxid,
"content": verify_content,
"instanceId": instance_id,
}))
return {"code": "1000"}
二、自动接受请求
从队列取出请求,调 friend/acceptRequest 接受:
def accept_friend(from_wxid, instance_id):
resp = requests.post(
f"{BASE}/finder/v2/api/friend/acceptRequest",
headers=HEADERS,
json={
"appId": "<appId>",
"instanceId": instance_id,
"toWxid": from_wxid,
},
timeout=10,
).json()
return resp.get("code") == "1000"
三、通过后的链式自动化
好友通过后,立刻执行备注、打标签、发欢迎语三步:
def on_friend_accepted(from_wxid, instance_id, verify_content):
# 1. 根据验证消息生成备注名
remark = f"渠道-{verify_content[:10]}" if verify_content else "新好友"
requests.post(
f"{BASE}/finder/v2/api/friend/remark",
headers=HEADERS,
json={"appId": "<appId>", "instanceId": instance_id,
"toWxid": from_wxid, "remark": remark},
timeout=10,
)
# 2. 打标签(字段以官方文档为准)
requests.post(
f"{BASE}/finder/v2/api/friend/tag",
headers=HEADERS,
json={"appId": "<appId>", "instanceId": instance_id,
"toWxid": from_wxid, "tag": "新好友"},
timeout=10,
)
# 3. 发欢迎语
requests.post(
f"{BASE}/finder/v2/api/postText",
headers=HEADERS,
json={"appId": "<appId>", "instanceId": instance_id,
"toWxid": from_wxid, "content": "你好,欢迎添加!"},
timeout=10,
)
四、风控:不要全自动通过
全自动通过所有好友请求有两个风险:一是被恶意加好友刷量,二是加进来的号可能是营销号/举报号。工程上要加审核规则:
- 白名单自动通过:验证消息含指定关键词(如渠道码)才自动通过
- 黑名单拦截:已知恶意wxid直接拒绝
- 频率控制:单账号每分钟通过请求数设上限,超过转人工审核
- 人工兜底:不满足自动规则的请求进人工审核队列
五、状态持久化
每条好友请求的处理状态要落库:待处理/自动通过/人工通过/拒绝。这样可以统计渠道来源、分析加好友转化率,也方便后续跟进。
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐


所有评论(0)