Baoyu Post To Wechat

Baoyu Post To Wechat

Baoyu Post To Wechat automation and integration for streamlined publishing

Category: productivity Source: JimLiu/baoyu-skills

Baoyu Post To Wechat is a community skill for formatting and publishing content to WeChat Official Accounts, covering article formatting, rich media embedding, WeChat-compatible HTML generation, cover image handling, and automated publishing workflows for the WeChat platform.

What Is This?

Overview

Baoyu Post To Wechat provides patterns for converting content into WeChat-compatible format and publishing through the Official Account API. It covers article formatting that converts Markdown or HTML content into WeChat-supported inline-styled HTML, rich media embedding that uploads and inserts images, audio, and video into articles, WeChat-compatible HTML generation that handles the platform-specific restrictions on CSS and JavaScript, cover image handling that uploads and assigns featured images for article previews, and automated publishing that drafts and publishes articles through the WeChat API. The skill enables content teams to automate WeChat publishing workflows.

Who Should Use This

This skill serves content teams publishing regularly to WeChat Official Accounts, marketing departments distributing content across WeChat and other platforms, and developers building cross-platform publishing tools that include WeChat.

Why Use It?

Problems It Solves

WeChat article editor requires manual formatting for each publication. Standard HTML and CSS features are restricted on the WeChat platform requiring special handling. Media assets must be uploaded to WeChat servers before embedding in articles. Cross-platform publishing needs separate formatting for WeChat compatibility.

Core Highlights

HTML converter transforms standard content into WeChat-compatible inline-styled markup. Media uploader handles image and video upload to WeChat temporary or permanent storage. Cover manager assigns and resizes featured images for article cards. API publisher automates drafting and publishing through the Official Account.

How to Use It?

Basic Usage

import requests

class WeChatPublisher:
  API_BASE = (
    'https://api.weixin'
    '.qq.com/cgi-bin')

  def __init__(
    self,
    app_id: str,
    app_secret: str
  ):
    self.app_id = app_id
    self.app_secret =\
      app_secret
    self.token = None

  def get_token(self):
    resp = requests.get(
      f'{self.API_BASE}'
      f'/token',
      params={
        'grant_type':
          'client_credential',
        'appid':
          self.app_id,
        'secret':
          self.app_secret})
    self.token = resp.json(
      )['access_token']

  def upload_image(
    self,
    image_path: str
  ) -> str:
    resp = requests.post(
      f'{self.API_BASE}'
      f'/media/upload',
      params={
        'access_token':
          self.token,
        'type': 'image'},
      files={
        'media': open(
          image_path, 'rb')})
    return resp.json(
      )['media_id']

  def create_draft(
    self,
    title: str,
    content: str,
    cover_id: str
  ) -> str:
    resp = requests.post(
      f'{self.API_BASE}'
      f'/draft/add',
      params={
        'access_token':
          self.token},
      json={'articles': [{
        'title': title,
        'content': content,
        'thumb_media_id':
          cover_id}]})
    return resp.json(
      )['media_id']

Real-World Examples

import markdown
import re

def to_wechat_html(
  md_text: str
) -> str:
  html = markdown.markdown(
    md_text,
    extensions=[
      'tables',
      'fenced_code'])

  # WeChat requires inline
  # styles - no external CSS
  html = html.replace(
    '<h1>',
    '<h1 style="font-size:'
    '24px;font-weight:bold;'
    'margin:20px 0 10px;">')
  html = html.replace(
    '<p>',
    '<p style="line-height:'
    '1.8;margin:10px 0;'
    'font-size:16px;">')
  html = html.replace(
    '<code>',
    '<code style="background:'
    '#f5f5f5;padding:'
    '2px 4px;font-size:'
    '14px;">')
  return html

Advanced Tips

Upload images to WeChat permanent material storage for reuse across multiple articles. Convert external image URLs to WeChat-hosted URLs as the platform blocks external image references. Test formatted articles in WeChat preview mode before publishing to verify rendering. Cache access tokens and refresh them before expiry to avoid failed publish attempts during batch operations.

When to Use It?

Use Cases

Publish blog articles to a WeChat Official Account with automatic formatting and media upload. Build a cross-platform CMS that publishes to WeChat alongside web and email. Convert Markdown documentation to WeChat-formatted articles for distribution.

Related Topics

WeChat publishing, content formatting, Official Account API, cross-platform publishing, and Chinese social media.

Important Notes

Requirements

WeChat Official Account with API access enabled. App ID and App Secret for authentication. Verified account status for publishing through the API. Python requests library for HTTP calls to WeChat endpoints.

Usage Recommendations

Do: use inline CSS styles as WeChat strips external stylesheets and style tags. Upload all images to WeChat servers before embedding in articles. Test article formatting in preview mode before publishing.

Don't: use JavaScript or external CSS which WeChat removes from article content. Reference external images that WeChat will block from rendering. Exceed the daily API call limits for media uploads.

Limitations

WeChat HTML rendering supports a limited subset of CSS properties. Daily API rate limits restrict the volume of automated publishing. Media file size limits apply for images, audio, and video uploads. Access tokens expire after two hours requiring periodic refresh in long-running publishing workflows. Article content cannot exceed 20000 characters per single WeChat article post.