This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
WeChat is a Ruby gem for integrating with WeChat Official Accounts Platform, WeChat Enterprise, and WeChat Mini Program APIs. It provides both Rails integrations (wechat_responder, wechat_api) and a standalone command-line interface.
# Run full test suite and linting
bundle exec rake
# Run only RSpec tests
bundle exec rake spec
# Run specific test file
bundle exec rspec spec/lib/wechat/http_client_spec.rb
# Run RuboCop linting
bundle exec rubocopThe gem uses Zeitwerk autoloading (see lib/wechat.rb:4). The main components are:
-
API Clients (
lib/wechat/):api.rb- Public account API (inherits from ApiBase)corp_api.rb- Enterprise account APImp_api.rb- Mini program APIapi_base.rb- Shared base implementation
-
HTTP & Token Management:
http_client.rb- HTTP client with retry logic and network settingstoken/- Access token management (PublicAccessToken, CorpAccessToken)ticket/- JSAPI ticket management (PublicJsapiTicket, CorpJsapiTicket)api_loader.rb- Configuration and API instance management
-
Message Handling:
responder.rb- DSL for handling incoming messages (seewechat_responder)message.rb- Message formatting and templatescontroller_api.rb- Rails controller helpers
-
Rails Integration (
lib/action_controller/):wechat_responder.rb- Provideswechat_responderDSLwechat_api.rb- Provideswechat_apimethodcallback.rb- Webhook callback handling
-
Rails Generators (
lib/generators/):wechat/- Generators forrails generate wechat:install,wechat:config, etc.
-
Helpers & Utilities:
helpers.rb- View helpers likewechat_config_jscipher.rb- Message encryption/decryptionsignature.rb- WeChat signature verificationconcern/- Shared modules (Common, Draft, Qcloud)
- Multi-account support: Use
Wechat.api(:account_name)to access different WeChat accounts. Configure viaconfig/wechat.ymlwith environment-specific sections. - Token caching: Access tokens and tickets are cached in files or databases to avoid repeated API calls.
- Responder DSL: Use
on :text, with: 'help' do |request|syntax to define message handlers. - Error handling:
ResponseErrorwraps WeChat API errors with error codes.
- Configuration loaded via
ApiLoader(lib/wechat/api_loader.rb) - Supports multiple sources: YAML files → Environment variables → Database records
- Configuration precedence: Database > YAML > Environment variables
- Reload with
Wechat.reload_config!after database changes
The project uses RSpec with:
spec/spec_helper.rb- Test configuration and SimpleCov setupspec/dummy/- Minimal Rails app for integration testsspec/support/- Shared test helpers
Tests mirror the lib structure (e.g., spec/lib/wechat/http_client_spec.rb).
SimpleCov coverage reports are generated in coverage/index.html.
- Ruby 2.6+
- Line length: 180 characters
- RuboCop is authoritative - run before commits
- Frozen string literals in all new files
- Standard Ruby naming conventions (snake_case methods, CamelCase classes)
config/wechat.yml- Generated byrails g wechat:install, holds account configurations~/.wechat.yml- CLI configuration for standalonewechatcommandAGENTS.md- Project development guidelinesCHANGELOG.md- Recent changesREADME.md- Full documentation
- Enterprise accounts must use encrypt mode (
encrypt_mode: trueis default) - Multi-account setup mirrors Rails database.yml structure
- Use
rails g wechat:redis_storefor distributed token/ticket storage - Access tokens are auto-refreshed;
AccessTokenExpiredErroris handled internally - The
wechatcommand has different commands for public vs enterprise accounts - JS-SDK requires
trusted_domain_fullnamein development behind reverse proxies