wechat Ruby Gem

repository·main·Indexed 23 days ago

https://github.com/eric-guo/wechat

A Ruby gem for Rails developers to integrate WeChat Official Accounts, WeChat Mini Programs, and Enterprise Accounts. It provides support for sending and receiving messages, JS-SDK configuration, OAuth 2.0 authentication, session management, and a command-line interface (CLI) for user, media, and menu management.

Tokens
11.2K
Snippets
19
Records
84
Agent score
79%

What's inside wechat

  1. Understand API privilege and error handling

    main

    The wechat gem does not handle API privilege exceptions. Access to specific WeChat APIs is controlled by Tencent based on your public account type and certification level.

    Note on Token Timeouts: The gem automatically handles token timeout retries and recovery internally, so these specific errors typically do not require manual intervention.

  2. How to manage multiple WeChat accounts

    main

    You can host multiple WeChat accounts (Public, Enterprise, or Mini Programs) in a single Rails application using two methods:

    1. Configuration-based (YAML)

    In config/wechat.yml, add new segments following the environment pattern. For example, to add an account named wx2, add wx2_development, wx2_test, and wx2_production.

    To use a specific account in your code:

    • Use Wechat.api(:wx2) to access the API for that account.
    • Declare the responder with wechat_responder account: :wx2.

    2. Controller-based (Dynamic)

    You can provide configuration directly in the controller or use a Proc to determine the account from the request via account_from_request.

    class WechatFirstController < ActionController::Base
       wechat_responder account: :new_account, account_from_request: Proc.new{ |request| request.params[:wechat] }
    
       on :text, with:"help", respond: "help content"
    end
    wechat_responder account: :new_account, account_from_request: Proc.new{ |request| request.params[:wechat] }
  3. Support WeChat Mini Programs

    main

    The library provides support for WeChat Mini Programs, including:

    • Mini Program APIs and signature checks (v0.10.0).
    • Sending template messages with Mini Programs (v0.10.0).
    • Generating Mini Program QR codes via wxa_get_wxacode (v0.8.7).
    • Generating unlimited-use Mini Program codes via wxa_get_wxacode_unlimit (v0.11.3).
  4. Support multiple WeChat accounts

    main

    The library supports multiple WeChat accounts in several ways:

    • wechat_responder supports multiple accounts (v0.10.0).
    • Accounts can be loaded dynamically from a database (v0.9.0).
    • Improved support for multiple accounts (v0.8.8).
    • Complete support for multiple WeChat public accounts (v0.8.0).
  5. Use the JS-SDK helper for WeChat App behavior

    main

    The wechat_config_js helper injects a configuration signature to control WeChat App behavior in HTML.

    Requirements:

    1. You must include wechat_api or wechat_responder in your controller before using the helper.
    2. If running behind a reverse proxy in development, you must configure trusted_domain_fullname in your settings.

    Usage Example:

    <body>
    <%= wechat_config_js debug: false, api: %w(hideMenuItems closeWindow) -%>
    <script type="application/javascript">
      wx.ready(function() {
          wx.hideOptionMenu();
      });
    </script>
    <a href="javascript:wx.closeWindow();">Close</a>
    </body>
  6. Install WeChat using the Rails generator (v0.5.0)

    main

    Version 0.5.0 introduced support for a Rails generator to simplify the installation process.

    Run the following command in your Rails application to install the gem and set up the necessary components:

    rails g wechat:install

    Note: This feature was introduced in v0.5.0.

    rails g wechat:install
  7. Install the wechat gem

    main

    Depending on your environment, use one of the following installation methods:

    For Ruby versions < 2.6

    Install the specific version via command line:

    gem install wechat -v 0.12.4

    For Rails versions < 6.0

    Add the gem to your Gemfile:

    gem 'wechat', '~> 0.12.4'
  8. Enable session recording for user messages

    main

    To optionally record sessions when receiving messages from users, run the session generator and migrate your database. This creates a wechat_sessions table. You can extend this table with more columns or link it to your users table. If using PostgreSQL, hstore or json columns are recommended for storing data, though adding dedicated columns is the standard Rails approach.

    rails g wechat:session
    rake db:migrate
  9. Use the new syntax for Scan, Batch Job, and Click events (v0.6.0)

    main

    Starting from version 0.6.0, the syntax for handling specific events has changed to be more explicit. While some old syntax may still work, you should use the following patterns for better performance and correctness:

    • 2D Barcode Scanning: Use on :scan, with: 'BINDING_QR_CODE' instead of the old :event syntax.
    • Batch Jobs: Use on :batch_job, with: 'replace_user' instead of the old :event syntax.
    • Menu Clicks: Use on :click, with: 'BOOK_LUNCH' instead of :event. The :click syntax is faster and more natural.

    Note: These changes were introduced in v0.6.0.

  10. Create and Upload WeChat Menus via YAML

    main

    You can manage WeChat menus using YAML configuration files. To generate a template for a menu definition in a Rails environment, use the generator:

    rails g wechat:menu

    Once the YAML file (e.g., menu.yaml) is configured, upload it using the CLI:

    wechat menu_create menu.yaml

    Note: Ensure you have management privileges for the application, otherwise you may encounter error [60011].

    button:
     -
      name: "Want"
      sub_button:
       - 
        type: "scancode_waitmsg"
        name: "绑定用餐二维码"
        key: "BINDING_QR_CODE"
       - 
        type: "click"
        name: "预订午餐"
        key:  "BOOK_LUNCH"
     - 
      type: "view"
      name: "About"
      url: "http://blog.cloud-mes.com/"
  11. Enable database-backed WeChat configurations

    main

    To support multiple WeChat accounts, enable database-backed configurations. This creates a wechat_configs table where you can store settings for various accounts.

    rails g wechat:config
    rake db:migrate