WeCom (Enterprise WeChat)¶
Overview¶
The WeCom channel receives messages via Webhook callbacks and sends messages through the REST API. A publicly accessible callback endpoint is required, suitable for servers with public IPs or those exposed through reverse proxies.
Public Endpoint Required
WeCom requires a publicly accessible callback URL for message delivery. Ensure your service port is reachable by WeCom servers before deployment.
Security Verification
Callback messages are protected by AES encryption and SHA1 signature verification, ensuring message authenticity and integrity.
Configuration Example¶
channels:
wecom:
corp_id: "ww1234567890abcdef"
agent_id: "1000002"
secret: "your-app-secret-here"
token: "your-callback-token"
encoding_aes_key: "43-char-base64-encoding-aes-key-from-wecom"
webhook_path: "/wecom"
host: "0.0.0.0"
port: 8084
| Field | Required | Default | Description |
|---|---|---|---|
corp_id |
Yes | — | Enterprise Corp ID |
agent_id |
Yes | — | Self-built app AgentId |
secret |
Yes | — | App Secret |
token |
Yes | — | Callback Token (for signature verification) |
encoding_aes_key |
Yes | — | Callback EncodingAESKey (43-char Base64) |
webhook_path |
No | /wecom |
Callback URL path |
host |
No | 0.0.0.0 |
Listen address |
port |
No | 8084 |
Listen port |
Credential Setup¶
Step 1: Obtain Corp ID¶
- Log in to the WeCom Admin Console
- Navigate to My Enterprise → Enterprise Info
- Locate and copy the Corp ID field
Step 2: Create a Self-Built App¶
- Go to App Management → Self-Built
- Click Create App
- Fill in the app name, logo, and visibility scope
- After creation, record the AgentId and Secret
Secret Shown Only Once
The app Secret is displayed only once upon creation. Save it immediately. If lost, you must regenerate it.
Step 3: Configure Callback¶
- In the app details page, find Receive Messages → Set API Receive
- Enter the callback URL:
https://your-domain.com/wecom - Generate or customize the Token and EncodingAESKey
- Click Save (WeCom will send a verification request at this point)
Callback/Webhook Setup¶
URL Verification Flow¶
WeCom sends a GET request to verify the callback URL when saving configuration:
Verification steps:
- Extract
msg_signature,timestamp,nonce, andechostrparameters - Compute SHA1 signature using
token,timestamp,nonce, and decryptedechostr - Compare the computed signature against
msg_signature - If valid, return the decrypted
echostrplaintext
Message Reception Flow¶
Regular messages arrive via POST:
POST /wecom?msg_signature=xxx×tamp=xxx&nonce=xxx
<xml>
<ToUserName><![CDATA[corp_id]]></ToUserName>
<Encrypt><![CDATA[encrypted_content]]></Encrypt>
<AgentID>1000002</AgentID>
</xml>
Decryption flow:
- Verify
msg_signature(SHA1(sort(token, timestamp, nonce, encrypt))) - Decrypt using
encoding_aes_keywith AES - Parse XML to extract message content
Access Token¶
Sending messages requires an access_token, obtained via Corp ID + Secret:
Token Expiry
access_token is valid for 7200 seconds (2 hours) and is auto-refreshed by the system. Avoid frequent requests to prevent rate limiting.
Capability Matrix¶
| Capability | Supported | Notes |
|---|---|---|
| Send text | Yes | — |
| Send images | No | Not implemented |
| Send voice | No | Not implemented |
| Send files | No | Not implemented |
| Edit messages | No | WeCom does not support |
| Reactions | No | WeCom does not support |
| Group chat | No | Not implemented |
| Realtime messages | Yes | Webhook push |
Technical Details¶
Message Encryption/Decryption¶
Handled by the wecom_crypto.py module:
- Algorithm: AES-256-CBC
- Key: Base64Decode(EncodingAESKey + "="), first 32 bytes
- IV: First 16 bytes of the key
- Padding: PKCS#7
Signature Verification¶
All callback requests must pass signature verification to prevent forged requests.
FAQ¶
Q: Callback URL verification fails?
- Confirm the service is running and the port is publicly accessible
- Verify Token and EncodingAESKey match the admin console values
- Confirm the callback path is correct (default
/wecom) - Check logs for signature computation results and compare with request parameters
Q: Not receiving message pushes?
- Confirm the app visibility scope includes the target users
- Check that the callback URL passed verification (green status in admin console)
- Verify firewalls are not blocking WeCom server IPs
Q: access_token retrieval fails?
- Verify Corp ID and Secret are correct
- Confirm the app has not been disabled
- Check IP allowlist settings (if configured)
Q: How to verify messages are from WeCom?
Via msg_signature verification. The signature is based on SHA1 of Token + Timestamp + Nonce + EncryptedContent, which cannot be forged.