Skip to main content

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 cachetools for 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_CORP_SECRET=your_corp_secret_here
WEIXIN_WORK_AGENT_ID=your_agent_id_here

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_CORP_SECRET="your_corp_secret_here"
export WEIXIN_WORK_AGENT_ID="your_agent_id_here"

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 ID
  • corp_secret (str): Your application secret
  • agent_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 settings
  • config_file (str, optional): Path to .env file
  • token_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 long
  • name (str, optional): Member name, 1-64 UTF8 characters
  • alias (str, optional): Alias, 1-64 UTF8 characters
  • mobile (str, optional): Mobile number. Must be unique within the enterprise
  • department (list, optional): List of department IDs the member belongs to, up to 100
  • order (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 characters
  • gender (str, optional): Gender. 1 for male, 2 for female
  • email (str, optional): Email address. 6-64 bytes and valid email format, must be unique within enterprise
  • biz_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 enterprise
  • biz_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 aliases
  • telephone (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-True
  • direct_leader (list, optional): Direct supervisor, can set members within the enterprise as direct supervisor, max 1 can be set
  • avatar_mediaid (str, optional): Member's avatar mediaid, obtained through media management API upload
  • enable (int, optional): Enable/disable member. 1 for enabled, 0 for disabled
  • extattr (dict, optional): Extended attributes. Fields need to be added in WEB management first
  • external_profile (dict, optional): Member's external attributes
  • external_position (str, optional): External position. If set, used as the displayed position, otherwise use position. Up to 12 Chinese characters
  • nickname (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" page
  • address (str, optional): Address. Max 128 characters
  • main_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 examples
  • advanced_usage.py: Advanced usage with environment variables
  • modular_demo.py: Demonstration of the static modular architecture

Development

Setup

  1. Clone the repository
  2. Install dependencies with uv (or pip):
    # Using uv (recommended)
    uv venv
    uv pip install -e ".[dev]"
    

Running Tests

python -m pytest tests/

Code Formatting

black .

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Run the test suite
  6. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

weixin_work_reborn-0.1.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

weixin_work_reborn-0.1.0-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file weixin_work_reborn-0.1.0.tar.gz.

File metadata

  • Download URL: weixin_work_reborn-0.1.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.3

File hashes

Hashes for weixin_work_reborn-0.1.0.tar.gz
Algorithm Hash digest
SHA256 00c706a4cbde7e8c3d8a5042c1b2f383719fa5e245c4a661245c20fe702d3718
MD5 6c8609a7296f195164cfbb117251abc4
BLAKE2b-256 0e49b4150000ede5b341325aac20ee0c49aa3531e8139be987b33bff9b2777c4

See more details on using hashes here.

File details

Details for the file weixin_work_reborn-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for weixin_work_reborn-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a1e778115e253531c5787b453bc4bb3bfcf2ee5a017ca5e2390321123e5f3117
MD5 acbdabba30792e81a79025b6c56ca295
BLAKE2b-256 0da857e2ca23efde3a6b4cb21ddd7f495b912b26dde2aebc64c60d3c557ff3f2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page