Skip to main content

WeCom WebSocket Channel

企业微信智能机器人 WebSocket 频道的独立 Python 实现。

✨ 特性

  • 🔌 独立运行 — 不依赖特定框架,可单独使用
  • 🌐 代理支持 — 支持 HTTP/HTTPS 代理,适配各种网络环境
  • 💬 图文混排 — 支持发送图文混排消息
  • 🔄 自动重连 — 断线自动重连,稳定可靠
  • 💓 心跳保活 — 自定义心跳机制,保持连接活跃
  • 🔐 消息加密 — 支持企业微信消息加解密

📦 安装

pip install wecom-ws-channel

或从源码安装:

git clone https://github.com/lcq225/wecom-ws-channel.git
cd wecom-ws-channel
pip install -e .

🚀 快速开始

基本用法

import asyncio
from wecom_ws_channel import WeComChannel

async def main():
    channel = WeComChannel(
        bot_id="your_bot_id",
        secret="your_secret",
    )
    
    # 设置消息处理回调
    channel.on_message = lambda msg: print(f"收到消息: {msg}")
    
    # 启动频道
    await channel.start()
    
    # 保持运行
    try:
        while True:
            await asyncio.sleep(1)
    except KeyboardInterrupt:
        await channel.stop()

asyncio.run(main())

使用代理

channel = WeComChannel(
    bot_id="your_bot_id",
    secret="your_secret",
    http_proxy="http://proxy.example.com:8080",
    http_proxy_auth="username:password",  # 可选
)

发送消息

# 发送文本
await channel.send_text(user_id, "Hello!")

# 发送图片
await channel.send_image(user_id, image_url)

# 发送文件
await channel.send_file(user_id, file_url, filename)

# 发送图文混排
await channel.send_mixed(user_id, [
    {"type": "text", "content": "这是一段文字"},
    {"type": "image", "url": "https://example.com/image.jpg"},
])

⚙️ 配置参数

参数 类型 必填 说明
bot_id str ✅ 智能机器人 BotID
secret str ✅ 智能机器人 Secret
http_proxy str ❌ HTTP 代理地址
http_proxy_auth str ❌ 代理认证 (username:password)
encoding_aes_key str ❌ 消息加密密钥
heartbeat_interval int ❌ 心跳间隔(秒),默认 10

📋 依赖

  • Python >= 3.9
  • aiohttp >= 3.8
  • pycryptodome >= 3.15

🔧 与 CoPaw 集成

本项目最初为 CoPaw 开发,可作为自定义频道使用:

# 放置到 .copaw/custom_channels/wecom/ 目录
# config.json 中配置:
{
    "channels": {
        "wecom": {
            "enabled": true,
            "bot_id": "your_bot_id",
            "secret": "your_secret"
        }
    }
}

📖 企业微信文档

🤝 贡献

欢迎提交 Issue 和 Pull Request!

📄 许可证

MIT License

🙏 致谢

  • 企业微信官方提供的 WebSocket 接口
  • CoPaw 项目框架参考

Metadata

Release files for wecom-ws-channel 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wecom-ws-channel 1.0.0
File Size Uploaded
wecom_ws_channel-1.0.0.tar.gz 10.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for wecom-ws-channel 1.0.0
File Interpreter ABI Platform
wecom_ws_channel-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 19.3 kB

Release files / wecom_ws_channel-1.0.0.tar.gz

Download URL wecom_ws_channel-1.0.0.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
3799e7b4173fb23a307ed709c38dd3b58592089b914362e71e3d6bc83de584f0
BLAKE2b-256 checksum
How to use checksums
c829391ea64f3b20bad00e0e4637b13b132a0c8fd18d7ca506c7949aaa7e7578
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.11

Release files / wecom_ws_channel-1.0.0-py3-none-any.whl

Download URL wecom_ws_channel-1.0.0-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9fdb7d9b2e09a8c29bdcf3b6bb96da6a107a5d59132f039d1d25d029952d87b8
BLAKE2b-256 checksum
How to use checksums
90f6c1b29a1f017aee56a3cbd4760016abd6b07d18392ac1770118766a0c41bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.11

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page