WeCom AI Bot Python SDK
raw JSON → 1.0.2 verified Sat May 09 auth: no python
企业微信智能机器人 Python SDK,基于 WebSocket 长连接通道,提供消息收发、流式回复、模板卡片、事件回调、文件下载解密等核心能力。当前版本 1.0.2,要求 Python >=3.8。
pip install wecom-aibot-python-sdk Common errors
error ModuleNotFoundError: No module named 'wecom_aibot_python_sdk' ↓
cause 包名不是连字符,应为下划线 wecom_aibot。
fix
使用 from wecom_aibot import WeComBot
error ConnectionRefusedError: ... ↓
cause WebSocket 连接地址错误或网络不通,或企微后台未启用机器人。
fix
检查 app_id 是否正确,确认企微后台已开启机器人并配置了 IP 白名单。
error TypeError: __init__() missing 4 required positional arguments: 'app_id', 'app_secret', 'token', 'encoding_aes_key' ↓
cause 实例化 WeComBot 未传入必要参数。
fix
确保传入 app_id, app_secret, token, encoding_aes_key 四个参数。
Warnings
breaking v1.0.0 至 v1.0.2 之间回调注册方式变更,旧版使用 @bot.on_text 现已改为 @bot.on_message()。 ↓
fix 升级后使用 @bot.on_message() 替代 @bot.on_text
gotcha token 和 encoding_aes_key 需从企业微信后台获取,如果填错会导致连接失败且无明确错误提示。 ↓
fix 请确保在企微后台的『机器人』配置中复制正确的 Token 和 EncodingAESKey。
deprecated 旧版导入路径 from wecom_aibot_python_sdk import ... 在 v1.0.0 后不再支持。 ↓
fix 改为 from wecom_aibot import ...
Imports
- WeComBot wrong
from wecom_aibot_python_sdk import WeComBotcorrectfrom wecom_aibot import WeComBot - TextMsg
from wecom_aibot import TextMsg - MarkdownMsg
from wecom_aibot import MarkdownMsg
Quickstart
import os
from wecom_aibot import WeComBot
bot = WeComBot(
app_id=os.environ.get('WECOM_APP_ID', ''),
app_secret=os.environ.get('WECOM_APP_SECRET', ''),
token=os.environ.get('WECOM_TOKEN', ''),
encoding_aes_key=os.environ.get('WECOM_ENCODING_AES_KEY', '')
)
@bot.on_message()
def echo(msg):
return msg.reply_text('Hello from WeCom Bot!')
bot.run()