关于 baoyu-post-to-wechat
baoyu-post-to-wechat 是面向公众号运营的发布技能,可通过 API 或浏览器发布文章和多图图文,并支持主题、元数据及多账号配置。
主要功能
- 01通过 API 或浏览器发布文章
- 02发布最多 9 张图片的图文
- 03Markdown 转换为公众号格式
- 04支持主题、颜色与元数据设置
- 05管理多个公众号账号
适用场景
- 01从 Markdown 发布公众号文章
- 02将多张图片整理为图文内容
- 03使用浏览器会话发布文章
- 04通过远程 API 访问白名单 IP
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ jimliu/ baoyu-skills --skill baoyu-post-to-wechat安装后在 Claude Code 中输入/baoyu-post-to-wechat确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/api-setup.mdarticle-posting.mdimage-text-posting.mdmulti-account.mdreferences/config/first-time-setup.mdscripts/bun.lockcdp.tscheck-permissions.tscopy-to-clipboard-mac.test.tscopy-to-clipboard.tsmd-to-wechat.tspackage.jsonpaste-from-clipboard.tswechat-agent-browser.tswechat-api.tswechat-article-focus.test.tswechat-article.tswechat-browser.tswechat-extend-config.test.tswechat-extend-config.tswechat-http.test.tswechat-http.tswechat-image-loader.tswechat-image-processor.tswechat-remote-publish.test.tswechat-remote-publish.tswechat-socks-http.test.tswechat-socks-http.ts详细介绍
发布到微信公众号
用户输入工具
当此技能向用户提问时,请遵循以下工具选择规则(按优先级排序):
- 优先使用当前 Agent 运行时提供的内置用户输入工具,例如
AskUserQuestion、request_user_input、clarify、ask_user或任何等效工具。 - 回退方案:如果没有此类工具,则输出带编号的纯文本消息,并请用户针对每个问题回复所选编号/答案。
- 批量提问:如果工具支持每次调用提出多个问题,则将所有适用问题合并到一次调用中;如果每次只能提出一个问题,则按优先级逐个提问。
以下具体的 AskUserQuestion 引用仅供参考——在其他运行时中,请替换为本地等效工具。
语言
使用用户的语言回复。如果用户使用中文,则用中文回复;如果使用英文,则用英文回复。技术标记(路径、标志、字段名)保持英文。
脚本目录
{baseDir} = 此 SKILL.md 所在目录。解析 ${BUN_X}:优先使用 bun;否则使用 npx -y bun;否则建议执行 brew install oven-sh/bun/bun。
| 脚本 | 用途 |
|---|---|
scripts/wechat-browser.ts | 图文推送 |
scripts/wechat-article.ts | 通过浏览器发布文章 |
scripts/wechat-api.ts | 通过 API 发布文章 |
scripts/md-to-wechat.ts | 将 Markdown 转换为适用于微信的 HTML,并附带图片占位符 |
scripts/check-permissions.ts | 验证环境和权限 |
偏好设置(EXTEND.md)
按顺序检查以下路径;以最先找到的文件为准:
| 路径 | 范围 |
|---|---|
.baoyu-skills/baoyu-post-to-wechat/EXTEND.md | 项目 |
${XDG_CONFIG_HOME:-$HOME/.config}/baoyu-skills/baoyu-post-to-wechat/EXTEND.md | XDG |
$HOME/.baoyu-skills/baoyu-post-to-wechat/EXTEND.md | 用户主目录 |
找到 → 读取、解析并应用。未找到 → 先运行首次设置(references/config/first-time-setup.md),再执行其他操作。
最低必需键(不区分大小写,接受 1/0 或 true/false):
| 键 | 默认值 | 映射 |
|---|---|---|
default_author | 空 | CLI/frontmatter 未提供时,作为 author 的备用值 |
need_open_comment | 1 | draft/add 中的 articles[].need_open_comment |
only_fans_can_comment | 0 | draft/add 中的 articles[].only_fans_can_comment |
推荐的 EXTEND.md:
default_theme: default
default_color: blue
default_publish_method: browser
default_author: 宝玉
need_open_comment: 1
only_fans_can_comment: 0
chrome_profile_path: /path/to/chrome/profile
# Remote API publishing (optional) — only set if WeChat's IP allowlist
# excludes your local machine. See "Remote API Method" below.
# remote_publish_host: server.example.com
# remote_publish_user: deploy
# remote_publish_port: 22
# remote_publish_identity_file: ~/.ssh/id_ed25519
# remote_publish_known_hosts_file: ~/.ssh/known_hosts
# remote_publish_strict_host_key_checking: accept-new
# remote_publish_connect_timeout: 10
# remote_publish_proxy_jump: bastion.example.com
刻意不支持原始 ssh/scp 选项;仅接受上述带类型的键。仅支持 SSH 密钥认证(不支持密码)。
主题选项:default、grace、simple、modern。颜色预设:blue、green、vermilion、yellow、purple、sky、rose、olive、black、gray、pink、red、orange(或十六进制值)。
取值优先级:CLI 参数 → frontmatter → EXTEND.md(账号级 → 全局)→ 技能默认值。
多账号支持
EXTEND.md 支持使用 accounts: 区块管理多个公众号。配置 2 个或更多条目时,工作流会插入步骤 0.5,提示用户选择账号(或根据 default: true 或 --account <alias> 自动选择)。
完整详情——兼容性规则、每个账号的键、凭据解析、每个账号的 Chrome 配置文件、CLI 用法——请参阅 references/multi-account.md。
使用前检查(可选)
首次使用前,建议进行环境检查(用户可以跳过):
${BUN_X} {baseDir}/scripts/check-permissions.ts
检查项:Chrome、配置文件隔离、Bun、辅助功能、剪贴板、粘贴快捷键、API 凭据、Chrome 冲突。
| 检查失败项 | 修复方法 |
|---|---|
| Chrome | 安装 Chrome,或设置 WECHAT_BROWSER_CHROME_PATH |
| 配置文件目录 | 使用位于 baoyu-skills/chrome-profile 的共享配置文件 |
| Bun 运行时 | 执行 brew install oven-sh/bun/bun 或 npm install -g bun |
| 辅助功能(macOS) | 系统设置 → 隐私与安全性 → 辅助功能 → 启用终端应用 |
| 复制到剪贴板 | 确保已安装 Swift/AppKit(macOS:xcode-select --install) |
| 粘贴快捷键(Linux) | 安装 xdotool(X11)或 ydotool(Wayland) |
| API 凭据 | 按照步骤 2 中的引导完成设置,或在 .baoyu-skills/.env 中设置 |
图文发布(图文)
包含多张图片的短帖(最多 9 张):
${BUN_X} {baseDir}/scripts/wechat-browser.ts --markdown article.md --images ./images/
${BUN_X} {baseDir}/scripts/wechat-browser.ts --title "标题" --content "内容" --image img.png --submit
详情:references/image-text-posting.md。
文章发布工作流(文章)
- [ ] Step 0: Load preferences (EXTEND.md)
- [ ] Step 0.5: Resolve account (multi-account only — see references/multi-account.md)
- [ ] Step 1: Determine input type
- [ ] Step 2: Select method and configure credentials
- [ ] Step 3: Resolve theme/color and validate metadata
- [ ] Step 4: Publish to WeChat
- [ ] Step 5: Report completion
步骤 0:加载偏好设置
检查并加载 EXTEND.md(参见上文“偏好设置”)。如果未找到,请先完成首次设置,再处理其他问题。解析并缓存以下设置,供后续步骤使用:default_theme、default_color、default_author、need_open_comment、only_fans_can_comment。
步骤 1:确定输入类型
| 输入 | 判定方式 | 下一步 |
|---|---|---|
| HTML 文件 | 路径以 .html 结尾,且文件存在 | 跳至步骤 3 |
| Markdown 文件 | 路径以 .md 结尾,且文件存在 | 步骤 2 |
| 纯文本 | 不是文件路径,或文件不存在 | 保存为 Markdown,然后执行步骤 2 |
纯文本处理:
- 生成 slug(取前 2-4 个有意义的词,使用 kebab-case;将中文翻译成英文作为 slug)。
- 保存到
post-to-wechat/YYYY-MM-DD/<slug>.md(如有需要,创建目录)。 - 继续按 Markdown 文件处理。
步骤 2:选择发布方式并进行配置
除非 EXTEND.md 或 CLI 中已指定,否则询问发布方式:
| 方式 | 速度 | 要求 |
|---|---|---|
api(推荐) | 快 | API 凭据(本地 IP 已加入允许列表) |
browser | 慢 | Chrome + 已登录的会话 |
remote-api | 快 | API 凭据 + 一台可通过 SSH 访问且 IP 已加入微信允许列表的服务器 |
已选择 API + 缺少凭据 → 按照 references/api-setup.md 运行引导式设置(写入 .baoyu-skills/.env)。
remote-api 方式:微信的“公众号设置 → IP 白名单”通常会将 API 访问限制在一到两个固定 IP。如果本机 IP 不在该列表中,但云服务器的 IP 在列表中,则使用 remote-api:所有 Markdown 渲染、图片处理、草稿组装和 HTML 改写仍在本地完成,只有发往 api.weixin.qq.com 的 HTTPS 请求(token、uploadimg、add_material、draft/add)会通过 SSH SOCKS5 动态端口转发(ssh -N -D)进行隧道传输,因此微信看到的来源 IP 是远程服务器。不会有任何文件写入远程主机;AppSecret 不会离开本地进程。远程主机只需运行 sshd 并能访问外网——无需 Python,也无需 Agent 进程。参见下文“远程 API 方式”。
步骤 3:确定主题/颜色并验证元数据
- 主题:CLI
--theme→ EXTEND.mddefault_theme→default(采用第一个匹配项;如果已确定,则不要询问)。 - 颜色:CLI
--color→ EXTEND.mddefault_color→ 省略(采用主题默认值)。 - 验证元数据(Markdown 的 frontmatter,HTML 的 meta 标签):
| 字段 | 缺失时 → |
|---|---|
| 标题 | 询问,或按 Enter 键根据内容自动生成 |
| 摘要 | Frontmatter description → summary → 询问或自动生成 |
| 作者 | CLI --author → frontmatter author → EXTEND.md default_author |
| 来源 URL | CLI --source-url → frontmatter sourceUrl/contentSourceUrl/content_source_url |
自动生成:标题 = 第一个 H1/H2 或第一句话;摘要 = 第一段,截取至 120 个字符。
- 封面图片(API 的
article_type=news必填):CLI--cover→ frontmatter(coverImage/featureImage/cover/image)→imgs/cover.png→ 第一张行内图片 → 如果仍缺失,则停止并请求提供一张封面图片。
步骤 4:发布
重要 — 切勿预先将 Markdown 转换为 HTML。 发布脚本会在内部处理转换,而且两种方法对图片的渲染方式不同:API 会渲染用于上传的 <img> 标签,浏览器会使用用于粘贴并替换的占位符。传入预先转换的 HTML 会导致其中一种方法出错。
Markdown 引用默认行为:对于 Markdown 输入,普通外部链接默认会转换为文末引用。只有在用户明确要求保留行内链接时,才使用 --no-cite。现有 HTML 输入保持原样。
API 方法(接受 .md 或 .html):
${BUN_X} {baseDir}/scripts/wechat-api.ts <file> --theme <theme> [--color <color>] [--title <title>] [--summary <summary>] [--author <author>] [--cover <cover_path>] [--source-url <url>] [--no-cite]
始终传入 --theme,即使其值为 default 也不例外。仅当用户或 EXTEND.md 明确设置了 --color 时才传入。
远程 API 方法(使用相同脚本,增加 --remote):
${BUN_X} {baseDir}/scripts/wechat-api.ts <file> --theme <theme> --remote [--remote-host <host>] [--remote-user <user>] [--remote-port <port>] [--remote-identity-file <path>] [--remote-known-hosts-file <path>] [--remote-strict-host-key-checking yes|no|accept-new] [--remote-connect-timeout <s>] [--remote-proxy-jump <spec>]
任何 --remote-* 标志都会启用 --remote。CLI 值优先于 EXTEND.md 中账号级别的 remote_publish_* 键,其次优先于全局键。设置 default_publish_method: remote-api 也会启用远程模式,无需使用 --remote。
draft/add 负载规则:
- 端点:
POST https://api.weixin.qq.com/cgi-bin/draft/add?access_token=ACCESS_TOKEN article_type:news(默认值)或newspic- 对于
news,请包含thumb_media_id(必须提供封面) - 请求正文中始终包含
need_open_comment(默认值为1)和only_fans_can_comment(默认值为0),即使 CLI 未提供这两个选项也是如此 - 对于
news,可选包含content_source_url(原文 URL,以“阅读原文”链接形式显示,最大 1 KB)。通过 CLI 标志--source-url或 frontmatter 中的sourceUrl/contentSourceUrl/content_source_url提供
浏览器方法(接受 --markdown 或 --html):
${BUN_X} {baseDir}/scripts/wechat-article.ts --markdown <markdown_file> --theme <theme> [--color <color>] [--no-cite]
${BUN_X} {baseDir}/scripts/wechat-article.ts --html <html_file>
步骤 5:完成报告
WeChat Publishing Complete!
Input: [type] - [path]
Method: [API | Browser]
Theme: [theme] [color if set]
Article:
• Title: [title]
• Summary: [summary]
• Images: [N] inline
• Comments: [open/closed], [fans-only/all] ← API method only
Result:
✓ Draft saved to WeChat Official Account
• media_id: [media_id] ← API method only
Next Steps (API):
→ Manage drafts: https://mp.weixin.qq.com (登录后进入「内容管理」→「草稿箱」)
Files created:
[• post-to-wechat/YYYY-MM-DD/slug.md (if plain text input)]
[• slug.html (converted)]
功能比较
| 功能 | 图文 | 文章(API) | 文章(远程 API) | 文章(浏览器) |
|---|---|---|---|---|
| 纯文本输入 | ✗ | ✓ | ✓ | ✓ |
| HTML 输入 | ✗ | ✓ | ✓ | ✓ |
| Markdown 输入 | 标题/内容 | ✓ | ✓ | ✓ |
| 多张图片 | ✓(最多 9 张) | ✓(行内) | ✓(行内) | ✓(行内) |
| 主题 | ✗ | ✓ | ✓ | ✓ |
| 自动生成元数据 | ✗ | ✓ | ✓ | ✓ |
默认封面回退(imgs/cover.png) | ✗ | ✓ | ✓ | ✗ |
| 评论控制 | ✗ | ✓ | ✓ | ✗ |
| 需要 Chrome | ✓ | ✗ | ✗ | ✓ |
| 需要 API 凭据 | ✗ | ✓ | ✓ | ✗ |
| 需要可通过 SSH 访问且 IP 已加入允许名单的服务器 | ✗ | ✗ | ✓ | ✗ |
| 速度 | 中等 | 快 | 快 | 慢 |
故障排查
| 问题 | 解决方法 |
|---|---|
| 缺少 API 凭据 | 按照步骤 2 中的引导完成设置 |
| 访问令牌错误 | 确认凭据有效且未过期 |
| 未登录(浏览器) | 首次运行时会打开浏览器 — 扫描二维码登录。设置 TELEGRAM_BOT_TOKEN + TELEGRAM_CHAT_ID,即可通过 Telegram 接收二维码图片 |
| 找不到 Chrome | 设置 WECHAT_BROWSER_CHROME_PATH |
| 缺少标题/摘要 | 使用自动生成,或手动提供 |
| 没有封面图片 | 添加 frontmatter 封面,或将 imgs/cover.png 放在文章目录中 |
| 评论默认值错误 | 检查 EXTEND.md 中的 need_open_comment / only_fans_can_comment |
| 粘贴失败 | 检查系统剪贴板权限 |
Remote publish host is required | 设置 --remote-host,或在 EXTEND.md 中设置 remote_publish_host |
SOCKS proxy on 127.0.0.1:… not ready | SSH 无法启动隧道 — 检查密钥、主机、StrictHostKeyChecking,或使用 --remote-connect-timeout |
远程发布时出现 ssh exited early | 确认用户可以通过 ssh 非交互式连接到服务器;如果连接较慢,请提高 --remote-connect-timeout |
远程 API 调用返回 errcode 40164(IP 无效) | 远程服务器的出口 IP 不在微信允许名单中;请在公众号设置 → IP 白名单中添加 |
参考资料
| 文件 | 内容 |
|---|---|
references/image-text-posting.md | 图文参数、自动压缩 |
references/article-posting.md | 文章主题、图片处理 |
references/multi-account.md | 多账号兼容性、凭据、Chrome 配置文件、CLI |
references/api-setup.md | 引导式凭据设置 |
references/config/first-time-setup.md | 首次设置 EXTEND.md |
扩展支持
通过 EXTEND.md 自定义配置。路径和支持的选项请参阅“偏好设置”。