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] }