wechat-article-publisher

安装量: 819
排名: #1548

安装

npx skills add https://github.com/iamzifei/wechat-article-publisher-skill --skill wechat-article-publisher

WeChat Article Publisher

Publish Markdown or HTML content to WeChat Official Account drafts via API, with automatic format conversion.

Prerequisites WECHAT_API_KEY environment variable set (from .env file) Python 3.9+ Authorized WeChat Official Account on wx.limyai.com Scripts

Located in ~/.claude/skills/wechat-article-publisher/scripts/:

wechat_api.py

WeChat API client for listing accounts and publishing articles:

List authorized accounts

python wechat_api.py list-accounts

Publish from markdown file

python wechat_api.py publish --appid --markdown /path/to/article.md

Publish from HTML file (preserves formatting)

python wechat_api.py publish --appid --html /path/to/article.html

Publish with custom options

python wechat_api.py publish --appid --markdown /path/to/article.md --type newspic

parse_markdown.py

Parse Markdown and extract structured data (optional, for advanced use):

python parse_markdown.py [--output json|html]

Workflow

Strategy: "API-First Publishing"

Unlike browser-based publishing, this skill uses direct API calls for reliable, fast publishing.

Load WECHAT_API_KEY from environment List available WeChat accounts (if user hasn't specified) Detect file format (Markdown or HTML) and parse accordingly Call publish API to create draft in WeChat Report success with draft details

Supported File Formats:

.md files → Parsed as Markdown, converted by WeChat API .html files → Sent as HTML, formatting preserved Step-by-Step Guide Step 1: Check API Key

Before any operation, verify the API key is available:

Check if .env file exists and contains WECHAT_API_KEY

cat .env | grep WECHAT_API_KEY

If not set, remind user to:

Copy .env.example to .env Set their WECHAT_API_KEY value Step 2: List Available Accounts

Get the list of authorized WeChat accounts:

python ~/.claude/skills/wechat-article-publisher/scripts/wechat_api.py list-accounts

Output example:

{ "success": true, "data": { "accounts": [ { "name": "我的公众号", "wechatAppid": "wx1234567890", "username": "gh_abc123", "type": "subscription", "verified": true, "status": "active" } ], "total": 1 } }

Important:

If only one account, use it automatically If multiple accounts, ask user to choose Note the wechatAppid for publishing Step 3: Publish Article

For Markdown files:

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

For HTML files (preserves formatting):

python ~/.claude/skills/wechat-article-publisher/scripts/wechat_api.py publish \ --appid \ --html /path/to/article.html

For 小绿书 (image-text mode):

python ~/.claude/skills/wechat-article-publisher/scripts/wechat_api.py publish \ --appid \ --markdown /path/to/article.md \ --type newspic

Success response:

{ "success": true, "data": { "publicationId": "uuid-here", "materialId": "uuid-here", "mediaId": "wechat-media-id", "status": "published", "message": "文章已成功发布到公众号草稿箱" } }

Step 4: Report Result

After successful publishing:

Confirm the draft was created Remind user to review and publish manually in WeChat admin panel Provide any relevant IDs for reference API Reference Authentication

All API requests require the X-API-Key header:

X-API-Key: WECHAT_API_KEY

Get Accounts List POST https://wx.limyai.com/api/openapi/wechat-accounts

Publish Article POST https://wx.limyai.com/api/openapi/wechat-publish

Parameters:

Parameter Type Required Description wechatAppid string Yes WeChat AppID title string Yes Article title (max 64 chars) content string Yes Article content (Markdown/HTML) summary string No Article summary (max 120 chars) coverImage string No Cover image URL author string No Author name contentFormat string No 'markdown' (default) or 'html' articleType string No 'news' (default) or 'newspic' Error Codes Code Description API_KEY_MISSING API key not provided API_KEY_INVALID API key invalid ACCOUNT_NOT_FOUND Account not found or unauthorized ACCOUNT_TOKEN_EXPIRED Account authorization expired INVALID_PARAMETER Invalid parameter WECHAT_API_ERROR WeChat API call failed INTERNAL_ERROR Server error Critical Rules NEVER auto-publish - Only save to drafts, user publishes manually Check API key first - Fail fast if not configured List accounts first - User may have multiple accounts Handle errors gracefully - Show clear error messages Preserve original content - Don't modify user's markdown unnecessarily Supported Formats Markdown Files (.md) H1 header (# ) → Article title H2/H3 headers (##, ###) → Section headers Bold (text) Italic (text) Links text Blockquotes (> ) Code blocks (...) Lists (- or 1.) Images → Auto-uploaded to WeChat HTML Files (.html) or <h1> → Article title All HTML formatting preserved (styles, tables, etc.) <img> tags → Images auto-uploaded to WeChat First <p> → Auto-extracted as summary Supports inline styles and rich formatting</p> <p>HTML Title Extraction Priority:</p> <p><title> tag content First <h1> tag content "Untitled" as fallback</p> <p>HTML Content Extraction:</p> <p>If <body> exists, uses body content Otherwise, strips <html>, <head>, <!DOCTYPE> and uses remaining content Article Types news (普通文章) Standard WeChat article format Full Markdown/HTML support Rich text with images newspic (小绿书/图文消息) Image-focused format (like Instagram posts) Maximum 20 images extracted from content Text content limited to 1000 characters Images auto-uploaded to WeChat Example Flow Markdown File</p> <p>User: "把 ~/articles/ai-tools.md 发布到微信公众号"</p> <h1 id="step-1-verify-api-key">Step 1: Verify API key</h1> <p>cat .env | grep WECHAT_API_KEY</p> <h1 id="step-2-list-accounts">Step 2: List accounts</h1> <p>python ~/.claude/skills/wechat-article-publisher/scripts/wechat_api.py list-accounts</p> <h1 id="step-3-publish-assuming-single-account-with-appid-wx1234567890">Step 3: Publish (assuming single account with appid wx1234567890)</h1> <p>python ~/.claude/skills/wechat-article-publisher/scripts/wechat_api.py publish \ --appid wx1234567890 \ --markdown ~/articles/ai-tools.md</p> <h1 id="step-4-report">Step 4: Report</h1> <h1 id="_1">"文章已成功发布到公众号草稿箱!请登录微信公众平台预览并发布。"</h1> <p>HTML File</p> <p>User: "把这个HTML文章发布到公众号:~/articles/newsletter.html"</p> <h1 id="step-1-verify-api-key_1">Step 1: Verify API key</h1> <p>cat .env | grep WECHAT_API_KEY</p> <h1 id="step-2-list-accounts_1">Step 2: List accounts</h1> <p>python ~/.claude/skills/wechat-article-publisher/scripts/wechat_api.py list-accounts</p> <h1 id="step-3-publish-html-auto-detects-format">Step 3: Publish HTML (auto-detects format)</h1> <p>python ~/.claude/skills/wechat-article-publisher/scripts/wechat_api.py publish \ --appid wx1234567890 \ --html ~/articles/newsletter.html</p> <h1 id="step-4-report_1">Step 4: Report</h1> <h1 id="html">"文章已成功发布到公众号草稿箱!HTML格式已保留。请登录微信公众平台预览并发布。"</h1> <p>Error Handling API Key Not Found Error: WECHAT_API_KEY environment variable not set.</p> <p>Solution: Ask user to set up .env file with their API key.</p> <p>Account Not Found Error: ACCOUNT_NOT_FOUND - 公众号不存在或未授权</p> <p>Solution: Ask user to authorize their account on wx.limyai.com.</p> <p>Token Expired Error: ACCOUNT_TOKEN_EXPIRED - 公众号授权已过期</p> <p>Solution: Ask user to re-authorize on wx.limyai.com.</p> <p>WeChat API Error Error: WECHAT_API_ERROR - 微信接口调用失败</p> <p>Solution: May be temporary issue, retry or check WeChat service status.</p> <p>Best Practices Why use API instead of browser automation? Reliability: Direct API calls are more stable than browser automation Speed: No browser startup, page loading, or UI interactions Simplicity: Single command to publish Portability: Works on any system with Python (no macOS-only dependencies) Content Guidelines Images: Use public URLs when possible; local images will be uploaded Title: Keep under 64 characters Summary: Auto-extracted from first paragraph if not provided Cover: First image in markdown becomes cover if not specified Workflow Efficiency Minimal workflow (1 command): - list-accounts → get appid → publish → done</p> <p>Full workflow (with verification): 1. Check .env → list accounts → confirm with user 2. Publish with options → report result</p> <p>Troubleshooting Q: How do I get a WECHAT_API_KEY?</p> <p>A: Register and authorize your WeChat account at wx.limyai.com to get your API key.</p> <p>Q: Can I publish to multiple accounts?</p> <p>A: Yes, use list-accounts to see all authorized accounts, then specify the target --appid.</p> <p>Q: Images not showing in WeChat?</p> <p>A: Ensure images are accessible URLs. Local images are auto-uploaded but may fail if path is incorrect.</p> <p>Q: Title is too long?</p> <p>A: WeChat limits titles to 64 characters. The script will use the first 64 chars of H1.</p> <p>Q: What's the difference between news and newspic?</p> <p>A: news is standard article format; newspic (小绿书) is image-focused with limited text.</p> </article> <a href="/" class="back-link">← <span data-i18n="detail.backToLeaderboard">返回排行榜</span></a> </div> <aside class="sidebar"> <section class="related-skills" id="relatedSkillsSection"> <h2 class="related-title" data-i18n="detail.relatedSkills">相关 Skills</h2> <div class="related-list" id="relatedSkillsList"> <div class="skeleton-card"></div> <div class="skeleton-card"></div> <div class="skeleton-card"></div> </div> </section> </aside> </div> </div> <script src="https://unpkg.com/i18next@23.11.5/i18next.min.js" defer></script> <script src="https://unpkg.com/i18next-browser-languagedetector@7.2.1/i18nextBrowserLanguageDetector.min.js" defer></script> <script defer> // Language resources - same pattern as index page const resources = { 'zh-CN': null, 'en': null, 'ja': null, 'ko': null, 'zh-TW': null, 'es': null, 'fr': null }; // Load language files (only current + fallback for performance) async function loadLanguageResources() { const savedLang = localStorage.getItem('i18nextLng') || 'en'; const langsToLoad = new Set([savedLang, 'en']); // current + fallback await Promise.all([...langsToLoad].map(async (lang) => { try { const response = await fetch(`/locales/${lang}.json`); if (response.ok) { resources[lang] = { translation: await response.json() }; } } catch (error) { console.warn(`Failed to load ${lang} language file:`, error); } })); } // Load a single language on demand (for language switching) async function loadLanguage(lang) { if (resources[lang]) return; try { const response = await fetch(`/locales/${lang}.json`); if (response.ok) { resources[lang] = { translation: await response.json() }; i18next.addResourceBundle(lang, 'translation', resources[lang].translation); } } catch (error) { console.warn(`Failed to load ${lang} language file:`, error); } } // Initialize i18next async function initI18n() { try { await loadLanguageResources(); // Filter out null values from resources const validResources = {}; for (const [lang, data] of Object.entries(resources)) { if (data !== null) { validResources[lang] = data; } } console.log('Loaded languages:', Object.keys(validResources)); console.log('zh-CN resource:', validResources['zh-CN']); console.log('detail.home in resource:', validResources['zh-CN']?.translation?.detail?.home); // 检查是否有保存的语言偏好 const savedLang = localStorage.getItem('i18nextLng'); // 如果没有保存的语言偏好,默认使用英文 const defaultLang = savedLang && ['zh-CN', 'en', 'ja', 'ko', 'zh-TW', 'es', 'fr'].includes(savedLang) ? savedLang : 'en'; await i18next .use(i18nextBrowserLanguageDetector) .init({ lng: defaultLang, // 强制设置初始语言 fallbackLng: 'en', supportedLngs: ['zh-CN', 'en', 'ja', 'ko', 'zh-TW', 'es', 'fr'], resources: validResources, detection: { order: ['localStorage'], // 只使用 localStorage,不检测浏览器语言 caches: ['localStorage'], lookupLocalStorage: 'i18nextLng' }, interpolation: { escapeValue: false } }); console.log('i18next initialized, language:', i18next.language); console.log('Test translation:', i18next.t('detail.home')); // Set initial language in selector const langSwitcher = document.getElementById('langSwitcher'); langSwitcher.value = i18next.language; // Update page language updatePageLanguage(); // Language switch event langSwitcher.addEventListener('change', async (e) => { await loadLanguage(e.target.value); // load on demand i18next.changeLanguage(e.target.value).then(() => { updatePageLanguage(); localStorage.setItem('i18nextLng', e.target.value); }); }); } catch (error) { console.error('i18next init failed:', error); } } // Translation helper function t(key, options = {}) { return i18next.t(key, options); } // Update all translatable elements function updatePageLanguage() { // Update HTML lang attribute document.documentElement.lang = i18next.language; // Update elements with data-i18n attribute document.querySelectorAll('[data-i18n]').forEach(el => { const key = el.getAttribute('data-i18n'); el.textContent = t(key); }); } // Copy command function function copyCommand() { const command = document.getElementById('installCommand').textContent; const btn = document.getElementById('copyBtn'); navigator.clipboard.writeText(command).then(() => { btn.textContent = t('copied'); btn.classList.add('copied'); setTimeout(() => { btn.textContent = t('copy'); btn.classList.remove('copied'); }, 2000); }).catch(() => { // Fallback for non-HTTPS const textArea = document.createElement('textarea'); textArea.value = command; textArea.style.position = 'fixed'; textArea.style.left = '-9999px'; document.body.appendChild(textArea); textArea.select(); document.execCommand('copy'); document.body.removeChild(textArea); btn.textContent = t('copied'); btn.classList.add('copied'); setTimeout(() => { btn.textContent = t('copy'); btn.classList.remove('copied'); }, 2000); }); } // Initialize document.getElementById('copyBtn').addEventListener('click', copyCommand); initI18n(); // 异步加载相关 Skills async function loadRelatedSkills() { const owner = 'iamzifei'; const skillName = 'wechat-article-publisher'; const currentLang = 'en'; const listContainer = document.getElementById('relatedSkillsList'); const section = document.getElementById('relatedSkillsSection'); try { const response = await fetch(`/api/related-skills/${encodeURIComponent(owner)}/${encodeURIComponent(skillName)}?limit=6`); if (!response.ok) { throw new Error('Failed to load'); } const data = await response.json(); const relatedSkills = data.related_skills || []; if (relatedSkills.length === 0) { // 没有相关推荐时隐藏整个区域 section.style.display = 'none'; return; } // 渲染相关 Skills listContainer.innerHTML = relatedSkills.map(skill => { const desc = skill.description || ''; const truncatedDesc = desc.length > 60 ? desc.substring(0, 60) + '...' : desc; return ` <a href="${currentLang === 'en' ? '' : '/' + currentLang}/skill/${skill.owner}/${skill.repo}/${skill.skill_name}" class="related-card"> <div class="related-name">${escapeHtml(skill.skill_name)}</div> <div class="related-meta"> <span class="related-owner">${escapeHtml(skill.owner)}</span> <span class="related-installs">${skill.installs}</span> </div> <div class="related-desc">${escapeHtml(truncatedDesc)}</div> </a> `; }).join(''); } catch (error) { console.error('Failed to load related skills:', error); // 加载失败时显示提示或隐藏 listContainer.innerHTML = '<div class="related-empty">暂无相关推荐</div>'; } } // HTML 转义 function escapeHtml(text) { const div = document.createElement('div'); div.textContent = text; return div.innerHTML; } // 页面加载完成后异步加载相关 Skills if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', loadRelatedSkills); } else { loadRelatedSkills(); } </script> </body> </html>