Coding

WeChat MP Publish Toolkit

Try it

WeChat Official Account (微信公众号) draft push and auto-publish toolkit. This skill should be used when the user wants to push article drafts to a WeChat Officia...

What it does

WeChat Official Account (微信公众号) draft push and auto-publish toolkit. This skill should be used when the user wants to push article drafts to a WeChat Official Account draft box via API, publish drafts automatically on a schedule, or set up a daily publishing automation. Covers credential setup, IP whitelisting, draft creation, and freepublish/submit publishing. Triggers: push draft, publish to WeChat, auto-publish, 微信发布, 推送草稿, 自动发布, 公众号发布.

The skill document

WeChat MP Publisher

Overview

A generic toolkit for pushing and publishing articles to a WeChat Official Account (公众号) via the WeChat MP API. Supports two core operations:

  1. Push Draft — Upload article HTML + cover image to the draft box (draft/add)
  2. Publish Draft — Publish a draft from the draft box to all subscribers (freepublish/submit)

Plus a daily automation template for scheduled auto-publishing.

First-Time Setup

1. Configure Credentials

Copy assets/.env.example to a permanent location and fill in values:

cp assets/.env.example ~/.wechat-mp.env

Edit the file:

WECHAT_APPID=wx_your_appid_here
WECHAT_SECRET=your_secret_here
WECHAT_AUTHOR=你的公众号名称

Where to find credentials:

  • Login to https://mp.weixin.qq.com
  • Settings & Development → Development → Basic Configuration
  • AppID is visible directly; AppSecret click "Reset" to get (save immediately, only shown once)

2. IP Whitelist

The server IP calling the API must be whitelisted, otherwise errcode=40164.

To get the current server IP:

curl -s https://api.ipify.org

Add this IP at: mp.weixin.qq.com → Development → Basic Configuration → IP Whitelist (IP白名单).

3. API Permissions

  • draft/add (push draft): Available to all verified accounts
  • freepublish/submit (publish): Requires verified enterprise/organization account (已认证的企业/个体工商户主体). Permission activates ~24 hours after verification. If errcode=48001, the permission is still syncing — publish manually from the draft box.

Core Tasks

Task 1: Push Draft to Draft Box

Use scripts/push_draft.py to push an article as a draft.

python3 scripts/push_draft.py \
  --env ~/.wechat-mp.env \
  --title "文章标题" \
  --digest "文章摘要,不超过120字" \
  --html /path/to/article.html \
  --thumb MEDIA_ID_OR_LEAVE_BLANK

What the script does:

  1. Load credentials from the --env file
  2. Get access_token (handles 40164 IP whitelist error)
  3. If --thumb not provided and a cover image path is given, upload it to the material library first
  4. Push the draft via draft/add API
  5. Print the resulting media_id

Key constraints:

  • Title: max 64 bytes (~21 Chinese characters)
  • Digest (摘要): max 120 characters
  • Author: leave empty via API (Chinese names exceed 8-byte limit) — set manually in editor
  • Article images must use mmbiz.qpic.cn domains (upload to material library first)
  • JSON must use ensure_ascii=False to avoid garbled Chinese — the script handles this with curl --data-binary

Task 2: Publish a Draft

Use scripts/publish_draft.py to publish a draft that's already in the draft box.

python3 scripts/publish_draft.py \
  --env ~/.wechat-mp.env \
  --media-id MEDIA_ID_FROM_DRAFT

What the script does:

  1. Load credentials from the --env file
  2. Get access_token
  3. Call freepublish/submit with the media_id
  4. Report success (publish_id) or the specific error code

Error handling:

errcodeMeaningAction
0SuccessNotify user, provide publish_id
48001API permission not activeTell user to publish manually from mp.weixin.qq.com draft box
40007media_id invalid (already published or deleted)Tell user to check the draft box
-1System error (transient)Retry 2-3 times; if persistent, manual publish

Publishing is irreversible — once published, the article goes to all subscribers. Always verify the media_id is correct.

Task 3: Set Up Daily Auto-Publish Automation

Refer to references/automation-template.md for the complete automation prompt. The recommended setup:

  1. Draft generation automation — runs daily (e.g., 15:00) to generate article content and push to draft box
  2. Publish automation — runs daily (e.g., 20:00) to call freepublish/submit on the day's draft

Create automations using the automation_update tool with:

  • scheduleType: "recurring"
  • rrule: "FREQ=DAILY;BYHOUR=20;BYMINUTE=0" (for 20:00 daily)
  • The prompt from references/automation-template.md

Quick API Reference (Bash)

For ad-hoc operations, use scripts/wechat_api.sh:

source scripts/wechat_api.sh

# Set credentials
export WECHAT_APPID="wx_your_appid"
export WECHAT_SECRET="your_secret"

# Get access token
TOKEN=$(get_wechat_token)

# Upload cover image (returns media_id)
upload_wechat_image "$TOKEN" /path/to/cover.png

# Create draft from JSON file
create_draft "$TOKEN" /path/to/draft.json

# Publish draft
publish_draft "$TOKEN" "MEDIA_ID_HERE"

Resources

  • scripts/push_draft.py — Push article HTML to draft box (Python, uses curl for binary-safe JSON)
  • scripts/publish_draft.py — Publish a draft via freepublish/submit (Python)
  • scripts/wechat_api.sh — Bash helper functions for all WeChat MP API calls
  • references/api-guide.md — Detailed API documentation, error codes, encoding gotchas
  • references/automation-template.md — Ready-to-use automation prompt for daily publishing
  • assets/.env.example — Credential template file

Related skills

基于微信云托管 relay 的公众号草稿发布器:把现成的 markdown 文章 + 封面图推送到微信公众号草稿箱,本地无需 appid/secret、免 IP 白名单。触发:「推草稿」「发公众号草稿箱」「wechat draft publish」「send to wechat draft」。不触发:写文章、tr...

2 installs

Write and publish WeChat Official Account articles end-to-end with AI, or take an existing draft straight into the skill's built-in formatting + direct-send...

34 installs2 stars

Independently turn an article into WeChat Official Accounts compatible Markdown/HTML, present built-in theme choices, and publish to WeChat as a draft or for...

17 installs

微信公众号写作发布助手 / WeChat Official Account Writer. 用于写公众号文章、微信公众号推文、技术长文、AI 热点解读、个人观点文、产品文章、教程文章和商业内容;支持选题策划、爆款标题、文章大纲、正文成稿、润色改写、链接翻译/链接改写、AI 资讯整理、Markdown/微信 HTM...

18 installs