Skip to main content

Python wrapper for WeChat Work (WeCom) Audit API. © 2025 puyuan.tech, Ltd. All rights reserved.

Project description

WeCom Audit

企业微信会话存档API的Python封装,提供简单易用的接口来获取企业微信的会话记录。

安装

pip install wecom-audit

功能特点

  • 支持获取会话记录
  • 支持下载媒体文件
  • 提供简单易用的Python API
  • 基于企业微信官方C语言SDK封装

依赖项

  • Python >= 3.11
  • CMake
  • OpenSSL开发库

使用示例

准备一个配置文件config.json,示例内容如下:

{
    "corporation_id": "your_corp_id",
    "app_secret": "your_secret",
    "batch_size": 100,
    "private_key_path": "private.pem"
}

基础用法:

import json
import wecom_audit
import os
from typing import Optional


def download_media_data(audit: wecom_audit.WeComAudit,
                        output_dir: str,
                        filename: str,
                        sdkfileid: str) -> Optional[str]:
    """
    Download media data from WeChat Work API.

    Args:
        audit: WeComAudit instance
        output_dir: Directory to save the file
        filename: Name of the file
        sdkfileid: SDK file ID from WeChat Work

    Returns:
        str: Path to saved file if successful, None otherwise
    """
    try:
        print(f"Getting media data for {filename}:")
        media_data = audit.get_media_data(sdkfileid)

        if media_data:
            file_path = os.path.join(output_dir, filename)
            with open(file_path, 'wb') as f:
                f.write(media_data)
            print(f"Successfully downloaded media data to: {file_path}")
            return file_path
        else:
            print(f"Failed to get media data for {filename}: No data returned")
            return None

    except Exception as e:
        print(f"Error while getting media data for {filename}: {str(e)}")
        return None


if __name__ == "__main__":
    audit = wecom_audit.WeComAudit("config.puyuan.json")
    messages = audit.get_new_messages(427)

    files_dir = "tmp/files"
    images_dir = "tmp/images"
    mixed_images_dir = "tmp/mixed_images"
    os.makedirs(files_dir, exist_ok=True)
    os.makedirs(images_dir, exist_ok=True)
    os.makedirs(mixed_images_dir, exist_ok=True)

    # download files
    for msg in messages["messages"]:
        if "content" in msg and "file" in msg["content"]:
            sdkfileid = msg["content"]["file"]["sdkfileid"]
            filename = msg["content"]["file"]["filename"]
            download_media_data(audit, files_dir, filename, sdkfileid)

    # download mixed message images
    for msg in messages["messages"]:
        if msg["content"]["msgtype"] == "mixed":
            for item in msg["content"]["mixed"]["item"]:
                if item["type"] == "image":
                    image_info = json.loads(item["content"])
                    sdkfileid = image_info["sdkfileid"]
                    filename = f"{image_info['md5sum']}.jpg"
                    download_media_data(audit, mixed_images_dir, filename, sdkfileid)

    # download images
    for msg in messages["messages"]:
        if "content" in msg and "image" in msg["content"]:
            sdkfileid = msg["content"]["image"]["sdkfileid"]
            filename = f"{msg['content']['image']['md5sum']}.jpg"
            download_media_data(audit, images_dir, filename, sdkfileid)

版权

© 2025 puyuan.tech, Ltd. All rights reserved.

许可证

MIT

贡献

欢迎提交问题和Pull Request!

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

wecom_audit-0.1.6.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

wecom_audit-0.1.6-cp311-cp311-manylinux2014_x86_64.whl (3.4 MB view details)

Uploaded CPython 3.11

File details

Details for the file wecom_audit-0.1.6.tar.gz.

File metadata

  • Download URL: wecom_audit-0.1.6.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.11

File hashes

Hashes for wecom_audit-0.1.6.tar.gz
Algorithm Hash digest
SHA256 4abbb7e03b6969b99ea56263a77a2411adcf59365d52da80a5f765e3865a6326
MD5 c946417152bef6bac843c3106d8108c1
BLAKE2b-256 8af9861394f4b6f5cc2dfe5ea77d0d70c8cf72cd661a6298e8252575ced12fa5

See more details on using hashes here.

File details

Details for the file wecom_audit-0.1.6-cp311-cp311-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for wecom_audit-0.1.6-cp311-cp311-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 218b1ce492928d28ca5eec4109370602b15af0fd2231b2e9dbe7cc8c78169dd9
MD5 50676b717945dd38cdf5d1d647c25e70
BLAKE2b-256 47f94f022f404c580a5d05109d932b417e51c2d102becbbe2f94fe2920533fa1

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