Skip to main content

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

Project description

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 项目框架参考

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wecom_ws_channel-1.0.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wecom_ws_channel-1.0.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file wecom_ws_channel-1.0.0.tar.gz.

File metadata

  • Download URL: wecom_ws_channel-1.0.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for wecom_ws_channel-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3799e7b4173fb23a307ed709c38dd3b58592089b914362e71e3d6bc83de584f0
MD5 2c93eb0205828951c4dd6e7df00268ca
BLAKE2b-256 c829391ea64f3b20bad00e0e4637b13b132a0c8fd18d7ca506c7949aaa7e7578

See more details on using hashes here.

File details

Details for the file wecom_ws_channel-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for wecom_ws_channel-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9fdb7d9b2e09a8c29bdcf3b6bb96da6a107a5d59132f039d1d25d029952d87b8
MD5 d8019992c4b4ed9aaaa8dcb7d2e57460
BLAKE2b-256 90f6c1b29a1f017aee56a3cbd4760016abd6b07d18392ac1770118766a0c41bc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page