Skip to main content

企业微信群机器人 Python SDK,支持所有消息类型、自动重试、异步发送、消息模板

Project description

gwozai-wechat-bot

企业微信群机器人 Python SDK,简单易用,功能完善。

PyPI version Python License: MIT

特性

  • ✅ 支持所有消息类型(文本、Markdown、图片、图文、文件、卡片)
  • ✅ 自动重试机制
  • ✅ 异步发送支持
  • ✅ 内置消息模板(告警、构建、部署、任务、日报)
  • ✅ 批量发送(自动频率控制)
  • ✅ 完善的类型提示
  • ✅ 日志记录

安装

pip install gwozai-wechat-bot

如需支持 .env 文件配置:

pip install gwozai-wechat-bot[dotenv]

快速开始

from gwozai_wechat_bot import WeChatBot

# 创建机器人
bot = WeChatBot(key="your-webhook-key")

# 发送文本
bot.text("Hello World")

# 发送 Markdown
bot.markdown("# 标题\n**加粗**")

# 发送图片
bot.image("/path/to/image.png")

# 发送文件
bot.file("/path/to/file.pdf")

环境变量配置

创建 .env 文件:

WECHAT_WEBHOOK_KEY=your-webhook-key

然后直接使用:

from gwozai_wechat_bot import WeChatBot

bot = WeChatBot()  # 自动读取环境变量
bot.text("Hello!")

消息类型

文本消息

bot.text("Hello World")
bot.text("紧急通知!", at_all=True)
bot.text("提醒", mentioned=["zhangsan", "lisi"])

Markdown 消息

bot.markdown("""
# 标题
> 引用

**加粗** 普通文字

<font color="info">绿色</font>
<font color="warning">橙色</font>
""")

图片消息

bot.image("/path/to/image.png")       # 本地文件
bot.image("https://example.com/a.png") # URL
bot.image(image_bytes)                 # 字节数据

图文消息

bot.news_single(
    title="文章标题",
    url="https://example.com",
    description="文章描述",
    picurl="https://example.com/cover.png"
)

文件消息

bot.file("/path/to/report.pdf")

卡片消息

bot.card(
    title="审批通知",
    desc="您有待处理的审批",
    emphasis=("3", "待审批"),
    fields=[("申请人", "张三"), ("类型", "请假")],
    buttons=[("查看详情", "https://example.com")]
)

消息模板

# 告警通知
bot.alert("服务器宕机", level="critical", source="订单服务")

# 构建通知
bot.build_notify(project="my-app", status="success", branch="main")

# 部署通知
bot.deploy_notify(project="my-app", env="production", version="v1.0.0")

# 任务通知
bot.task_notify(title="完成文档", assignee="张三", priority="high")

# 日报
bot.daily_report(title="系统日报", metrics={"请求量": "100万"})

异步发送

import asyncio

async def main():
    bot = WeChatBot(key="your-key")
    await bot.async_text("异步消息")
    await bot.async_markdown("# 异步 Markdown")

asyncio.run(main())

批量发送

bot.batch([
    ("text", "消息1"),
    ("text", "消息2"),
    ("markdown", "# 标题"),
])

便捷函数

from gwozai_wechat_bot import send_text, send_markdown, send_alert

send_text("Hello")
send_markdown("# 标题")
send_alert("告警", level="error")

License

MIT © gwozai

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

gwozai_wechat_bot-2.0.0.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

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

gwozai_wechat_bot-2.0.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file gwozai_wechat_bot-2.0.0.tar.gz.

File metadata

  • Download URL: gwozai_wechat_bot-2.0.0.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for gwozai_wechat_bot-2.0.0.tar.gz
Algorithm Hash digest
SHA256 e24de154dccaafd4d8b7fd9ef0532a70f67a93fad547b9f3c1f9ddd0bf35ea93
MD5 3f24c66bb66a08691b902d6da9c3aced
BLAKE2b-256 b414eeeb872dfa07d0c364960a61d0064bd7f9374f8220f6668ae1d4662a260d

See more details on using hashes here.

File details

Details for the file gwozai_wechat_bot-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for gwozai_wechat_bot-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7a84b3a33e287a7bd38740eaa22d9d2a3a240a8a169d132ecead64403cdb8010
MD5 b9fb80ab236c2fe6aa8f95bdae98f381
BLAKE2b-256 4539063b16c65a031374cb5851433aa55d8eb7c0e2343bdf9fb61c56102c3837

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