A Python SDK for WeChat Work API
Project description
WeChat Work API SDK
A Python SDK for interacting with the WeChat Work API. This library provides a simple and efficient way to integrate your Python applications with WeChat Work, supporting key features like user management, authentication, and more. The SDK features a clean modular architecture with static imports.
Features
- Static Modular Architecture: Separate modules for configuration, common functions (access_token), and user management
- Environment Configuration: Support for .env files and environment variables with python-dotenv
- Easy Access Token Management: Automatic retrieval and caching of access tokens with TTL (Time To Live) support
- User Management: Get user information, update user profiles, and convert mobile numbers to user IDs
- Comprehensive Error Handling: Proper exception handling for API errors
- Type Hints: Full type annotation support for better IDE experience
- Thread-Safe: Safe for concurrent usage in multi-threaded applications
- Caching: Uses
cachetoolsfor efficient token caching
Installation
Install the package using pip:
pip install weixin-work-reborn
Quick Start
from weixin_work_reborn import WeChatWorkClient, Config
# Initialize the client with configuration
config = Config() # Loads from .env file or environment variables
client = WeChatWorkClient(config=config)
# Get user information
user_info = client.get_user("user_id_here")
print(user_info)
# Update user information
result = client.update_user(
user_id="user_id_here",
name="New Name",
mobile="13800138000",
email="newemail@example.com"
)
print(result)
# Convert mobile to user ID
userid_result = client.mobile_to_userid("13800138000")
print(userid_result)
Configuration
Using .env File (Recommended)
Create a .env file in your project root:
WEIXIN_WORK_BASE_URL=https://qyapi.weixin.qq.com/
WEIXIN_WORK_CORP_ID=your_corp_id_here
WEIXIN_WORK_APP_SECRET=your_app_secret_here
WEIXIN_WORK_CONTACTS_SYNC_SECRET=your_contacts_sync_secret_here
WEIXIN_WORK_AGENT_ID=your_agent_id_here
Note: WeChat Work API requires different secrets for different API endpoints:
WEIXIN_WORK_APP_SECRETis used for general API operations (e.g., getting user information, mobile to userid conversion)WEIXIN_WORK_CONTACTS_SYNC_SECRETis specifically required for user management operations (e.g., update_user)
Then in your code:
from weixin_work_reborn import WeChatWorkClient, Config
config = Config() # Automatically loads from .env file
client = WeChatWorkClient(config=config)
Environment Variables
Alternatively, you can set environment variables:
export WEIXIN_WORK_BASE_URL="https://qyapi.weixin.qq.com/"
export WEIXIN_WORK_CORP_ID="your_corp_id_here"
export WEIXIN_WORK_APP_SECRET="your_app_secret_here"
export WEIXIN_WORK_CONTACTS_SYNC_SECRET="your_contacts_sync_secret_here"
export WEIXIN_WORK_AGENT_ID="your_agent_id_here"
Note: WeChat Work API requires different secrets for different API endpoints:
WEIXIN_WORK_APP_SECRETis used for general API operations (e.g., getting user information, mobile to userid conversion)WEIXIN_WORK_CONTACTS_SYNC_SECRETis specifically required for user management operations (e.g., update_user)
API Reference
Config
Handles configuration loading from environment variables and .env files.
Constructor
Config(env_file=None)
env_file(str, optional): Path to a specific .env file to load
Properties
base_url(str): The base URL for WeChat Work API (default: "https://qyapi.weixin.qq.com/")corp_id(str): Your WeChat Work corporate IDcorp_secret(str): Your application secretagent_id(str): Your application agent ID
WeChatWorkClient
The main client class for interacting with the WeChat Work API.
Constructor
WeChatWorkClient(config=None, config_file=None, token_cache_size=100, token_cache_ttl=7000)
config(Config, optional): Config object with API settingsconfig_file(str, optional): Path to .env filetoken_cache_size(int): Size of the token cache (default: 100)token_cache_ttl(int): Time-to-live for cached tokens in seconds (default: 7000, just under the 7200s token expiry)
Methods
get_user(user_id)
Get user information by user ID.
user_id(str): The user ID to retrieve information for- Returns: User information as a dictionary
update_user(userid, **kwargs)
Update user information.
userid(str): Required. User ID. Corresponds to the account in the management console, must be unique within the enterprise. Case-insensitive, 1-64 bytes longname(str, optional): Member name, 1-64 UTF8 charactersalias(str, optional): Alias, 1-64 UTF8 charactersmobile(str, optional): Mobile number. Must be unique within the enterprisedepartment(list, optional): List of department IDs the member belongs to, up to 100order(list, optional): Sorting value within the department, defaults to 0. Effective when department is provided. Number must match department, larger number means higher priority. Valid range is [0, 2^32)position(str, optional): Position information, 0-128 UTF8 charactersgender(str, optional): Gender. 1 for male, 2 for femaleemail(str, optional): Email address. 6-64 bytes and valid email format, must be unique within enterprisebiz_mail(str, optional): If the enterprise has activated Tencent Corporate Mail (Enterprise WeChat Mail), setting this creates a corporate email account. 6-63 bytes and valid corporate email format, must be unique within enterprisebiz_mail_alias(dict, optional): Corporate email alias. 6-63 bytes and valid corporate email format, must be unique within enterprise, up to 5 aliases can be set. Updates are overwritten. Passing empty structure or empty array clears current corporate email aliasestelephone(str, optional): Landline. Composed of 1-32 digits, "-", "+", or ","is_leader_in_dept(list, optional): Department head field, count must match department, indicates whether the member is a head in the department. 0-False, 1-Truedirect_leader(list, optional): Direct supervisor, can set members within the enterprise as direct supervisor, max 1 can be setavatar_mediaid(str, optional): Member's avatar mediaid, obtained through media management API uploadenable(int, optional): Enable/disable member. 1 for enabled, 0 for disabledextattr(dict, optional): Extended attributes. Fields need to be added in WEB management firstexternal_profile(dict, optional): Member's external attributesexternal_position(str, optional): External position. If set, used as the displayed position, otherwise use position. Up to 12 Chinese charactersnickname(str, optional): Video account name (after setting, the member will display this video account externally). Must be selected from the video account bound to the enterprise WeChat, accessible in the "My Enterprise" pageaddress(str, optional): Address. Max 128 charactersmain_department(int, optional): Main department- Returns: API response as a dictionary
mobile_to_userid(mobile)
Convert mobile number to user ID.
mobile(str): The mobile number to convert- Returns: API response containing user ID as a dictionary
Examples
More examples can be found in the examples/ directory:
basic_usage.py: Basic usage examplesadvanced_usage.py: Advanced usage with environment variablesmodular_demo.py: Demonstration of the static modular architecture
Development
Setup
- Clone the repository
- Install dependencies with
uv(orpip):# Using uv (recommended) uv venv uv pip install -e ".[dev]"
Running Tests
python -m pytest tests/
Code Formatting
black .
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
If you encounter any issues, please file a bug report on the GitHub issues page.
About WeChat Work API
For more information about the WeChat Work API, visit the official documentation.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file weixin_work_reborn-0.1.1.tar.gz.
File metadata
- Download URL: weixin_work_reborn-0.1.1.tar.gz
- Upload date:
- Size: 12.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b71f5f5d6d72feecc309e9579533aadf5c1dca6edf1e74ba9c42f7da5e1f8e7e
|
|
| MD5 |
99dd0efcf9ca643ed4e86c55283dfccd
|
|
| BLAKE2b-256 |
beee926f8d859aaeecdbcc30366674c217a71bc8c4b02b486e15c516dba1045a
|
File details
Details for the file weixin_work_reborn-0.1.1-py3-none-any.whl.
File metadata
- Download URL: weixin_work_reborn-0.1.1-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4354fa3067944bb2e3e181a193b01f19884ba296f41489ca6c8e915efa0497a
|
|
| MD5 |
c7388fdbe79048c4b5c36d9ff6d647b8
|
|
| BLAKE2b-256 |
de3d76d09f0886efff8c4429f01edd534475557ff603f3cfd61b3516d3006f0c
|