n8n-nodes-wechat-publish
Version:
N8N nodes for WeChat Official Account publishing - Create drafts and publish articles to WeChat Official Account platform
427 lines (332 loc) • 11.5 kB
Markdown
# n8n-nodes-wechat-publish
[](https://badge.fury.io/js/n8n-nodes-wechat-publish)
[](https://opensource.org/licenses/MIT)
N8N nodes for WeChat Official Account publishing. Create drafts and publish articles to WeChat Official Account platform with ease.
## 功能特性 Features
- ✅ **创建草稿** - Create draft articles with rich content support
- ✅ **获取草稿** - Retrieve existing draft articles
- ✅ **删除草稿** - Delete unwanted draft articles
- ✅ **发布文章** - Publish draft articles to WeChat Official Account
- ✅ **发布状态检查** - Monitor publishing status and results
- ✅ **素材上传** - Upload images and media files
- ✅ **图文消息** - Support for news articles with images
- ✅ **图片消息** - Support for image-based articles
- ✅ **评论设置** - Configure comment permissions
- ✅ **自动Token管理** - Automatic access token refresh
- ✅ **错误重试** - Built-in error handling and retry mechanism
## 安装 Installation
### 通过 npm 安装
```bash
npm install n8n-nodes-wechat-publish
```
### 手动安装
1. 克隆此仓库到你的 n8n 自定义节点目录
2. 安装依赖:`npm install`
3. 构建项目:`npm run build`
4. 重启 n8n
## 配置 Configuration
### 1. 获取微信公众号凭证
1. 登录 [微信公众平台](https://mp.weixin.qq.com/)
2. 进入"设置与开发" > "基本配置"
3. 获取你的 **AppID** 和 **AppSecret**
### 2. 在 N8N 中配置凭证
1. 在 N8N 中创建新的凭证
2. 选择 "WeChat API" 凭证类型
3. 填入以下信息:
- **App ID**: 你的微信公众号 AppID
- **App Secret**: 你的微信公众号 AppSecret
- **Base URL**: `https://api.weixin.qq.com` (默认)
- **Request Timeout**: `30000` (可选,默认30秒)
## 使用方法 Usage
### 基本工作流程
1. **获取素材列表** (查看已有素材) 或 **上传素材** (上传新素材)
2. **创建草稿** (使用素材ID作为封面)
3. **发布文章**
4. **检查状态**
> **注意**:创建草稿前必须确保有有效的永久素材ID用作封面图片
### 操作说明
#### 创建草稿 (Create Draft)
**🆕 支持两种模式:**
##### 单篇文章模式 (Single Article Mode)
适用于精细化编辑单篇文章:
```json
{
"operation": "createDraft",
"batchProcessingMode": "single",
"articleType": "news",
"title": "我的文章标题",
"content": "<p>这是文章内容,支持HTML格式</p>",
"author": "作者姓名",
"thumbMediaId": "媒体ID",
"needOpenComment": true,
"onlyFansCanComment": false
}
```
##### 批量文章模式 (Multi-Article Mode) 🆕
**每3篇文章自动分组创建草稿**,适用于批量内容发布:
```json
{
"operation": "createDraft",
"batchProcessingMode": "multi",
"articlesJson": [
{
"article_type": "news",
"title": "文章标题1",
"content": "<p>文章内容1</p>",
"thumb_media_id": "媒体ID1",
"author": "作者",
"digest": "摘要1",
"need_open_comment": 0,
"only_fans_can_comment": 0
},
{
"article_type": "news",
"title": "文章标题2",
"content": "<p>文章内容2</p>",
"thumb_media_id": "媒体ID2",
"author": "作者",
"digest": "摘要2",
"need_open_comment": 1,
"only_fans_can_comment": 0
},
{
"article_type": "news",
"title": "文章标题3",
"content": "<p>文章内容3</p>",
"thumb_media_id": "媒体ID3",
"author": "作者",
"digest": "摘要3",
"need_open_comment": 0,
"only_fans_can_comment": 0
}
]
}
```
**批量模式响应示例:**
```json
{
"total_articles": 5,
"total_batches": 2,
"batch_size": 3,
"batch_results": [
{
"batch_index": 1,
"article_count": 3,
"media_id": "draft_media_id_1",
"status": "success"
},
{
"batch_index": 2,
"article_count": 2,
"media_id": "draft_media_id_2",
"status": "success"
}
],
"success_batches": 2,
"failed_batches": 0,
"usage_tip": "📝 成功处理 5 篇文章,分为 2 个批次(每批最多3篇)"
}
```
**🆕 智能图片插入功能**:
```json
{
"operation": "createDraft",
"articleType": "news",
"title": "带图片的文章",
"content": "<p>文章内容...</p>",
"thumbMediaId": "封面图片ID",
"enableImageInsertion": true,
"imageInsertionMode": "auto",
"insertionImageIds": "media_id_1,media_id_2,media_id_3",
"maxImagesPerArticle": 3,
"imageInsertionStrategy": "sequential"
}
```
**图片插入模式说明**:
- `auto` - 智能定位:基于内容结构自动找到最佳插入位置
- `paragraph` - 段落间插入:在段落之间按规律间隔插入图片
- `manual` - 手动模式:使用内容中已有的图片标签
**图片选择策略**:
- `sequential` - 顺序选择:按提供的顺序使用图片
- `random` - 随机选择:随机从图片列表中选择
- `balanced` - 均衡分布:在图片列表中均匀分布选择
#### 发布草稿 (Publish Draft)
将草稿发布到微信公众号:
```json
{
"operation": "publishDraft",
"mediaId": "草稿的媒体ID"
}
```
#### 检查发布状态 (Get Publish Status)
检查文章发布状态:
```json
{
"operation": "getPublishStatus",
"publishId": "发布ID"
}
```
#### 上传素材 (Upload Media)
上传图片等素材文件:
```json
{
"operation": "uploadMedia",
"mediaType": "image",
"mediaFile": "data" // 二进制数据属性名
}
```
#### 获取素材列表 (Get Material List)
获取已上传的永久素材列表:
```json
{
"operation": "getMaterialList",
"materialType": "image",
"offset": 0,
"count": 20
}
```
#### 批量上传Memes (Batch Upload Memes) 🆕
从Meme API获取不重复的图片并批量上传到微信公众平台:
```json
{
"operation": "batchUploadMemes",
"memeCount": 5,
"subreddit": "wholesomememes",
"skipNsfw": true,
"skipSpoiler": true,
"minUps": 100
}
```
**功能特点**:
- 🎯 自动从Reddit获取热门Memes
- 🚫 智能过滤NSFW和剧透内容
- 🔄 自动去重,避免重复上传
- 📊 支持最小点赞数筛选
- 🎨 支持指定Subreddit或随机获取
- 📈 返回详细的上传统计信息
**参数说明**:
- `memeCount`: 要获取的Meme数量 (1-50)
- `subreddit`: 指定子版块 (可选,留空为随机)
- `skipNsfw`: 跳过NSFW内容 (推荐开启)
- `skipSpoiler`: 跳过剧透内容 (推荐开启)
- `minUps`: 最小点赞数要求
**返回数据结构**:
```json
{
"success_count": 3,
"failed_count": 1,
"duplicated_count": 1,
"media_ids": ["media_id_1", "media_id_2", "media_id_3"],
"failed_urls": ["https://failed-url.com"],
"processed_memes": [...],
"summary": {
"total_requested": 5,
"successfully_uploaded": 3,
"usage_tip": "💡 Use the media_ids from this response as thumbMediaId when creating drafts"
}
}
```
**重要说明**:创建草稿时必须使用**永久素材ID**作为`thumbMediaId`。你可以:
1. 使用"获取素材列表"操作查看已有的素材ID
2. 使用"上传素材"操作上传新的永久素材
3. 使用"批量上传Memes"操作获取趣味图片素材ID
4. 在微信公众平台后台上传素材并通过"获取素材列表"获取ID
## 参数说明 Parameters
### 文章类型 (Article Type)
- `news`: 图文消息
- `newspic`: 图片消息
### 必填参数
- **标题 (Title)**: 文章标题,最大64个字符
- **内容 (Content)**: 文章内容,支持HTML,最大2万字符
- **封面图片 (Thumbnail Media ID)**: 封面图片的媒体ID
### 可选参数
- **作者 (Author)**: 文章作者
- **摘要 (Digest)**: 文章摘要,为空时自动截取前54个字符
- **原文链接 (Content Source URL)**: "阅读原文"的链接地址
- **开启评论 (Enable Comments)**: 是否允许评论
- **仅粉丝评论 (Only Fans Can Comment)**: 是否仅允许粉丝评论
## 错误处理 Error Handling
插件包含完整的错误处理机制:
- **API错误**: 自动解析微信API错误码并提供详细错误信息
- **网络错误**: 支持自动重试机制
- **Token过期**: 自动刷新访问令牌
- **参数验证**: 验证必填参数和数据格式
### 常见错误码
| 错误码 | 错误信息 | 解决方案 |
|--------|----------|----------|
| 40001 | access_token过期 | 自动处理,无需干预 |
| 40002 | 不合法的凭证类型 | 检查AppID和AppSecret |
| 40013 | 不合法的AppID | 确认AppID正确 |
| 53404 | 账号已被限制带货能力 | 删除商品信息后重试 |
## 示例工作流程 Example Workflows
### 1. 简单发布流程
```
手动触发 → 创建草稿 → 发布草稿 → 检查状态
```
### 2. 批量发布流程
```
读取数据 → 上传图片 → 创建草稿 → 发布草稿 → 记录结果
```
### 3. 定时发布流程
```
定时触发 → 从数据库读取 → 创建草稿 → 延时等待 → 发布草稿
```
### 4. 智能图片文章发布流程 🆕
```
手动触发 → 批量上传Memes → 提取media_ids → 创建带图片的草稿 → 发布文章
```
**详细步骤**:
1. **获取图片素材** - 使用"Batch Upload Memes"获取趣味图片
2. **处理图片ID** - 从返回结果中提取media_ids
3. **创建图文草稿** - 启用图片插入功能,自动在文章中插入图片
4. **发布到公众号** - 发布包含多张图片的丰富文章
## API 参考 API Reference
基于微信公众平台官方API文档:
- [新建草稿](https://developers.weixin.qq.com/doc/offiaccount/Draft_Box/Add_draft.html)
- [发布接口](https://developers.weixin.qq.com/doc/offiaccount/Publish/Publish.html)
- [素材管理](https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Adding_Permanent_Assets.html)
## 开发 Development
### 本地开发设置
```bash
# 克隆仓库
git clone https://github.com/yourusername/n8n-nodes-wechat-publish.git
cd n8n-nodes-wechat-publish
# 安装依赖
npm install
# 开发模式
npm run dev
# 构建
npm run build
# 运行测试
npm test
# 检查代码规范
npm run lint
```
### 项目结构
```
n8n-nodes-wechat-publish/
├── credentials/ # 凭证定义
├── nodes/ # 节点定义
│ └── WeChat/
├── types/ # TypeScript 类型定义
├── dist/ # 构建输出
└── docs/ # 文档
```
## 贡献 Contributing
欢迎贡献代码!请遵循以下步骤:
1. Fork 项目
2. 创建功能分支 (`git checkout -b feature/AmazingFeature`)
3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
4. 推送到分支 (`git push origin feature/AmazingFeature`)
5. 创建 Pull Request
## 许可证 License
本项目采用 MIT 许可证 - 查看 [LICENSE](LICENSE) 文件了解详情。
## 支持 Support
如果你遇到问题或有建议,请:
1. 查看 [常见问题](docs/FAQ.md)
2. 搜索 [已有问题](https://github.com/yourusername/n8n-nodes-wechat-publish/issues)
3. 创建新的 [Issue](https://github.com/yourusername/n8n-nodes-wechat-publish/issues/new)
## 更新日志 Changelog
查看 [CHANGELOG.md](CHANGELOG.md) 了解版本更新历史。
---
**注意**: 使用本插件需要有效的微信公众号和相应的API权限。请确保遵守微信公众平台的使用条款和政策。