PowerWeChat SDK

repository·release/3.4.0·Indexed 23 days ago

https://github.com/artisancloud/powerwechat

A Golang-based SDK framework designed to simplify WeChat development. It provides abstractions for Mini Programs, WeChat Official Accounts, WeChat Pay, WeCom (Enterprise WeChat), Open Platform, and Open Work. The SDK handles complex tasks such as AES encryption, signature verification, callback decryption, and token management. Version 3 introduces context support and updated token refresh behavior.

Tokens
65.8K
Snippets
123
Records
742
Agent score
83%

What's inside PowerWeChat

  1. Migrate from PowerWeChat V2 to V3

    release/3.4.0

    When upgrading from version 2 to version 3, note the following breaking changes:

    • Context Support: Most interfaces now include a context.Context parameter.
    • Token Management: The automatic token refresh behavior from V2 has been resolved/changed.
  2. Use the DataCube Client for Official Account Analytics

    release/3.4.0

    The dataCube.Client provides access to WeChat Official Account analytics data, including user growth, article engagement, message distribution, and interface usage. Most methods require a context.Context and a date range defined by from and to strings (typically in YYYY-MM-DD format).

    Methods are categorized by the type of data they retrieve:

    • User Analysis: Get user summary or cumulative data.
    • Graphic/Article Analysis: Get article summaries, totals, user read summaries, or share data.
    • Message Analysis: Get upstream message summaries, hourly/weekly/monthly data, or distribution patterns.
    • Interface Analysis: Get API usage summaries and hourly data.
    • Cards & Offers: Get summaries for free cards or member cards.
  3. Use the Express Client for WeChat Mini Program Logistics

    release/3.4.0

    The express.Client provides an interface to interact with WeChat Mini Program logistics APIs, including order management, account binding, and tracking. It requires a *kernel.BaseClient to handle the underlying HTTP communication with WeChat servers.

    Key capabilities include:

    • Order Management: Add, batch get, cancel, and retrieve order details.
    • Account Services: Bind/unbind logistics accounts and list all bound accounts.
    • Logistics Tracking: Query waybill paths and update tracking information.
    • Provider Services: Preview templates, get contact info, and update business audit results.
  4. Manage large files with chunked uploads in Mini Drama VOD

    release/3.4.0

    For large files (videos up to 500MB, covers up to 10MB), use the three-step chunked upload process:

    1. Apply for Upload ID: Call ApplyMediaUploadId to get a unique upload_id for the session.
    2. Upload Chunks: Call ApplyMediaChunkUpload multiple times (up to 100 chunks, max 5MB each) using the upload_id and a part_number. Chunks can be uploaded out of order.
    3. Complete Upload: Call UploadMediaChunkComplete to merge all chunks. You must provide the part_number and etag for every chunk to verify integrity.
    func (comp *Client) ApplyMediaUploadId(ctx context.Context, in *request.VideoApplyChunkUploadByIdRequest) (result *response.VideoApplyChunkUploadByIdResponse, err error)
    func (comp *Client) ApplyMediaChunkUpload(ctx context.Context, in *request.VideoApplyChunkUploadRequest) (result *response.VideoApplyChunkUploadResponse, err error)
    func (comp *Client) UploadMediaChunkComplete(ctx context.Context, in *request.VideoChunkUploadCompleteRequest) (result *response.VideoChunkUploadCompleteResponse, err error)
  5. How to use SubscribeAndServe for long-running bots

    release/3.4.0

    The SubscribeAndServe method is the recommended way to run an AIBot. It performs the following lifecycle steps:

    1. Subscribes to the bot using botID and secret.
    2. Starts a background goroutine to Listen for incoming messages and pass them to your MessageHandler.
    3. Starts a background goroutine that sends a Heartbeat (ping) every 30 seconds to keep the connection alive.
    4. Blocks until the context is cancelled or an error occurs.

    If an error occurs in either the listener or the heartbeat loop, the connection is closed and the error is returned.

  6. Initialize a Mini Program instance and call Session API

    release/3.4.0

    To use the SDK, initialize an application instance using NewMiniProgram with a UserConfig object, then use the sub-modules (like Auth) to call WeChat APIs.

    Key configuration fields in miniProgram.UserConfig:

    • AppID: The AppID for the Mini Program, Official Account, or WeCom.
    • Secret: The AppSecret.
    • HttpDebug: Boolean to enable HTTP debugging.
    • Debug: Boolean to enable debug mode.
  7. Configure HTTP client via Application Config

    release/3.4.0

    The BaseClient reads several configuration keys from the ApplicationInterface to set up the underlying HTTP transport. Use these keys in your application configuration:

    • http.base_uri: The base URL for requests (defaults to /).
    • http.proxy_uri: The proxy URI to use.
    • http.timeout: Request timeout in seconds (defaults to 5).
    • http.transport: An http.RoundTripper implementation for custom transport logic.
    • http.proxy: An alternative proxy configuration key.
    • debug: Boolean to enable debug mode (adds debug=1 to queries).
    • http_debug: Boolean to enable HTTP debug logging.
  8. Configure Merchant ID and RSA Signer

    release/3.4.0

    If you need to sign requests using WeChat Pay's RSA mechanism, provide the following configuration keys in your application config. If all three are present, a SHA256WithRSASigner is automatically initialized in the BaseClient:

    • mch_id: The Merchant ID.
    • serial_no: The Certificate Serial Number.
    • key_path: The file path to the private key.