Skip to main content

微信公众号发布服务 Python SDK

Project description

WeChat Publish SDK

微信公众号发布服务的 Python SDK,提供稳定、易用的 API 接口。

特性

  • API 版本化 - 通过 api_version 参数支持多个版本
  • 自动签名 - 无需手动计算 HMAC-SHA256 签名
  • 类型安全 - 完整的类型注解,IDE 提示友好
  • 错误处理 - 统一的异常体系
  • 简洁易用 - 一个客户端解决所有操作
  • 默认账号 - 设置 default_account 后无需每次传 account

安装

pip install wechat-publish-sdk

快速开始

初始化客户端

import os
from wechat_publish_sdk import WeChatClient

client = WeChatClient(
    base_url="http://localhost:3000",
    signing_key=os.getenv("SIGNING_KEY"),
    default_account="mingdeng",  # 可选:设置默认账号
    api_version="v1"  # 可选:默认 "v1"
)

发布文章

from wechat_publish_sdk import WeChatClient, PublishRequest

# 基本发布
result = client.publish_article(
    PublishRequest(
        title="测试文章",
        content="# 这是测试内容"
    )
)

if result.success:
    print(f"发布成功,draft_id: {result.draft_id}")
else:
    print(f"发布失败: {result.message}")

发布带封面的文章

# 先上传封面
upload_result = client.upload_image(
    UploadRequest(
        account="mingdeng",
        file_path="path/to/cover.jpg"
    )
)

if upload_result.success:
    media_id = upload_result.media_id

    # 发布文章时使用封面
    result = client.publish_article(
        PublishRequest(
            title="带封面的文章",
            content="# 文章内容",
            thumb_media_id=media_id,
            show_cover_pic=1,
            author="作者名",
            digest="文章摘要"
        )
    )

批量发布

articles = [
    {"title": "文章1", "content": "内容1"},
    {"title": "文章2", "content": "内容2"},
    {"title": "文章3", "content": "内容3"},
]

for article in articles:
    result = client.publish_article(
        PublishRequest(**article)
    )
    print(f"{article['title']}: {result.message}")

查询素材列表

result = client.list_materials(
    account="mingdeng",
    material_type="image",
    offset=0,
    count=20
)

if result.success:
    print(f"共 {result.total_count} 个素材")
    for item in result.items:
        print(f"  - {item.name}: {item.url}")

Markdown 渲染

from wechat_publish_sdk import RenderRequest

result = client.render_markdown(
    RenderRequest(
        content="# 标题\n\n内容",
        theme="orange"
    )
)

if result.success:
    print(result.html)

API 版本控制

SDK 支持多版本 API,通过初始化时指定 api_version 参数:

# 使用 v1 稳定版
client_v1 = WeChatClient(
    base_url="http://localhost:3000",
    signing_key="key",
    api_version="v1"
)

# 使用 dev 开发版
client_dev = WeChatClient(
    base_url="http://localhost:3000",
    signing_key="key",
    api_version="dev"
)

异常处理

from wechat_publish_sdk import (
    WeChatClient, PublishRequest,
    ValidationError, AccountNotFoundError, PublishFailedError
)

try:
    result = client.publish_article(PublishRequest(...))
except ValidationError as e:
    print(f"参数错误: {e}")
except AccountNotFoundError as e:
    print(f"账号不存在: {e}")
except PublishFailedError as e:
    print(f"发布失败: {e}")
except WeChatPublishError as e:
    print(f"其他错误: {e}")

配置说明

参数 类型 必填 说明
base_url str 后端服务地址
signing_key str HMAC 签名密钥(十六进制)
default_account str 默认账号
api_version str API 版本,默认 "v1"
timeout int 请求超时(秒),默认 30

开发

# 克隆项目
git clone https://github.com/tangcheng/wechat-publish-sdk.git

# 安装开发依赖
pip install -e ".[dev]"

# 运行测试
pytest

# 代码格式化
black wechat_publish_sdk/

# 类型检查
mypy wechat_publish_sdk/

License

MIT License

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

wechat_publish_sdk-1.0.0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

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

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: wechat_publish_sdk-1.0.0.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for wechat_publish_sdk-1.0.0.tar.gz
Algorithm Hash digest
SHA256 bd3a5befc7d460c8c43b186821f31b7456e5d8236cba1760d5c31a752605db93
MD5 61209518ac905d1a99f506890180261f
BLAKE2b-256 2c7b05d0cf75a7c5b5a50348f3aba74db071fe851f1429e7816ded6547323852

See more details on using hashes here.

Provenance

The following attestation bundles were made for wechat_publish_sdk-1.0.0.tar.gz:

Publisher: publish.yml on helloworldtang/wechat-publish-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for wechat_publish_sdk-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 002de1aa9845399117f26716b2b05d7c6078bccf7148d480122940dda865ef08
MD5 cb2aba98627c76651571a36572517d64
BLAKE2b-256 ac5c7f509b394b8f1a91e52955fea06721f1b68293602e79c96b34ecf3a13d5b

See more details on using hashes here.

Provenance

The following attestation bundles were made for wechat_publish_sdk-1.0.0-py3-none-any.whl:

Publisher: publish.yml on helloworldtang/wechat-publish-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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