一、 为什么选择 Python?
Python 凭借其极简的语法和丰富的生态,成为了开发机器人的首选语言。今天我们将抛弃复杂的底层轮子,直接对接 E云管家 的标准 RESTful API,用最少的代码实现一个微信自动收发系统。

二、 核心代码实现
1. 依赖库安装
我们只需要两个库:requests(用于发送指令)和 FastAPI(用于高速接收消息回调)。
pip install requests fastapi uvicorn2. 完整极简实战代码
新建一个 robot.py 文件,写入以下代码:
import requests from fastapi import FastAPI, Request app = FastAPI() API_URL = "http://api.eyunguanjia.com/v1" INSTANCE_ID = "你的实例ID" # 在E云管家后台获取 def send_message(to_user, text): """封装发送消息接口""" url = f"{API_URL}/message/send" data = { "instance_id": INSTANCE_ID, "to_user": to_user, "msg_type": "text", "content": text } try: requests.post(url, json=data) except Exception as e: print(f"发送失败: {e}") @app.post("/webhook") async def receive_webhook(request: Request): """接收E云管家推送的消息""" json_data = await request.json() # 过滤出文本消息 if json_data.get("event") == "FriendMessage" and json_data.get("msg_type") == "text": sender = json_data.get("from_user") text_content = json_data.get("content") # 极简关键字匹配 if text_content == "菜单": send_message(sender, "1. 每日天气\n2. 智能畅聊\n3. 联系人工") elif text_content == "1": send_message(sender, "今日北京天气:晴,15°C~28°C。") return {"status": "ok"} if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)三、 代码深度解析
1. 接口解耦:E云管家充当了中间件的角色。当微信收到消息,E云管家将其封装为标准的 JSON,推送到 http://你的IP:8000/webhook。
2. 异步高并发:这里采用了 FastAPI 异步框架,能够承受高并发的消息推送。如果你的机器人加入了多个大群,群内消息刷屏时,异步架构能保证消息不丢失、不阻塞。

四、 运行与测试
运行代码后,将 http://你的公网IP:8000/webhook 填写到 E云管家 后台。尝试用另一个微信号给机器人发送“菜单”,你将在 1 秒内收到机器人的精准回复。API 文档参考