agentleFS
shaoxuehengshaoxueheng/wechat-article-publisher/SKILL.md

wechat-article-publisher

Skill0 starsChanged 8 months agoMITView on GitHub

Publish Markdown/HTML articles directly to WeChat Official Account (微信公众号) draft box via WeChat's official API. Auto-generates cover image from article content.

  • Reads credentials
  • Installs packages

3 other projects use a version of this.

Adopted by

Most recently taken up by

  • Other versions3 projects use a version of this.

    Word for word · 0

    None found.

    Edited · 0

    None found.

    Wrote their own version · 3

    1. iamzifei/wechat-article-publisher-skill★ 157same name
    2. ZhaoYis/My-Skills★ 2same name
    3. tomlen045/media-agent-skills★ 0same name

    How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.

The file

Files
SKILL.md

WeChat Article Publisher

将 Markdown 或 HTML 文章直接发布到微信公众号草稿箱。

  • 自动根据文章内容生成封面图(主题色自动匹配)
  • 直接调用微信官方 API,无需第三方服务

Prerequisites

  • Python 3.9+
  • pip install Pillow markdown
  • .env 文件中设置 WECHAT_APP_ID 和 WECHAT_APP_SECRET

Configuration

在项目目录或家目录创建 .env 文件(脚本会自动向上查找):

# ~/.env 或 /your/project/.env
WECHAT_APP_ID=wx你的AppID
WECHAT_APP_SECRET=你的AppSecret

获取凭据:微信公众平台 → 设置与开发 → 基本配置 → AppID / AppSecret

IP白名单:基本配置 → IP白名单 → 添加当前机器 IP(否则报 40164 错误)

Usage

SCRIPT=~/.claude/skills/wechat-article-publisher/scripts/wechat_api.py

# 验证凭据
python $SCRIPT get-token

# 发布 Markdown(自动生成封面)
python $SCRIPT add-draft --markdown /path/to/article.md

# 发布 HTML(自动生成封面)
python $SCRIPT add-draft --html /path/to/article.html

# 指定本地图片作封面(自动上传)
python $SCRIPT add-draft --markdown /path/to/article.md --cover-image /path/to/cover.jpg

# 使用已有 WeChat media_id 作封面
python $SCRIPT add-draft --markdown /path/to/article.md --thumb-media-id MEDIA_ID

# 不带封面
python $SCRIPT add-draft --markdown /path/to/article.md --no-cover

# 完整选项
python $SCRIPT add-draft \
  --markdown /path/to/article.md \
  --title "自定义标题" \
  --author "作者名" \
  --summary "文章摘要" \
  --source-url https://example.com/original

Workflow

每次执行 add-draft 的完整流程:

  1. 从 .env 加载 WECHAT_APP_ID / WECHAT_APP_SECRET
  2. 获取 access_token(缓存于 ~/.wechat_token_cache.json,2小时有效)
  3. 解析文件:.md → 转 HTML,.html → 提取 body
  4. 根据文章关键词自动选择配色方案,生成 900×500 封面图
  5. 上传封面到微信永久素材,获取 thumb_media_id
  6. 调用 POST /cgi-bin/draft/add 写入草稿箱
  7. 返回 media_id,提示用户去公众平台预览发布

Cover Image Generation

封面由 Pillow 本地生成,根据文章关键词自动选色:

主题配色
AI / 机器学习 / 大模型深蓝紫渐变
科技 / 编程 / 开发深蓝青渐变
商业 / 创业 / 营销深蓝金渐变
健康 / 医疗 / 运动深绿渐变
金融 / 投资 / 理财深金渐变
文化 / 艺术 / 教育深紫粉渐变
其他默认深蓝紫

Step-by-Step Guide

Step 1: 确认 .env 配置

cat ~/.env | grep WECHAT_APP

如未配置,提示用户填写 AppID 和 AppSecret,参考 .env.example。

Step 2: 验证凭据

python ~/.claude/skills/wechat-article-publisher/scripts/wechat_api.py get-token

成功输出:

{"status": "ok", "token_preview": "abcd1234..."}

Step 3: 发布文章

python ~/.claude/skills/wechat-article-publisher/scripts/wechat_api.py add-draft \
  --markdown /path/to/article.md

成功输出:

{
  "success": true,
  "media_id": "xxxxxxxxxxxxxxxx",
  "message": "文章已成功发布到公众号草稿箱"
}

Step 4: 提示用户

  • 告知草稿已创建成功
  • 提示登录 https://mp.weixin.qq.com → 草稿箱 → 预览并发布
  • 提供 media_id 供参考

API Reference

Get Access Token

GET https://api.weixin.qq.com/cgi-bin/token
    ?grant_type=client_credential&appid=APPID&secret=APPSECRET

Upload Material (Cover Image)

POST https://api.weixin.qq.com/cgi-bin/material/add_material
     ?access_token=TOKEN&type=image
Content-Type: multipart/form-data

Add Draft

POST https://api.weixin.qq.com/cgi-bin/draft/add?access_token=TOKEN
{
  "articles": [{
    "title": "标题",
    "content": "<p>HTML内容</p>",
    "digest": "摘要",
    "thumb_media_id": "封面media_id",
    "author": "作者",
    "content_source_url": "原文链接",
    "need_open_comment": 0,
    "only_fans_can_comment": 0
  }]
}

Error Codes

Code含义解决
40001access_token 无效删除 ~/.wechat_token_cache.json,重新 get-token
40013AppID 无效检查 .env 中 WECHAT_APP_ID
40125AppSecret 无效检查 .env 中 WECHAT_APP_SECRET
40164IP 不在白名单微信后台 → 基本配置 → IP白名单添加当前 IP
45009调用频率超限等待后重试
48001API 未授权微信后台 → 接口权限 → 开启草稿接口

Critical Rules

  1. 绝不自动发布 — 只写草稿,用户在公众平台手动发布
  2. 先验证凭据 — get-token 失败立即报错,不继续
  3. token 有缓存 — ~/.wechat_token_cache.json 2小时内复用
  4. 内容必须是 HTML — Markdown 在提交前自动转换

Example Flow

用户:把 ~/articles/ai-trends.md 发到微信草稿箱

1. 检查 .env 配置
2. python ~/.claude/skills/wechat-article-publisher/scripts/wechat_api.py get-token
3. python ~/.claude/skills/wechat-article-publisher/scripts/wechat_api.py add-draft \
     --markdown ~/articles/ai-trends.md
4. 输出成功信息 + media_id
5. 提示用户去 mp.weixin.qq.com 草稿箱预览发布

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.