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.4.tar.gz (251.7 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.4-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.4.tar.gz.

File metadata

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

File hashes

Hashes for wecom_audit-0.1.4.tar.gz
Algorithm Hash digest
SHA256 deb5bc73bd977addcb3697bf648314ec17ffff033c2c1d9be0f2f37d6d0b63dd
MD5 e1412e8831c25ed4507a53e064ce4d85
BLAKE2b-256 ee2ce5533fced066b5d5bcf36e4a61276a44419785f482ce6e5b06d2b9b80488

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wecom_audit-0.1.4-cp311-cp311-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 49b35d08e0f23ad311de4f9a824ee7b047d87d2f0e63220a2659950e40a06ec2
MD5 149fc39cb8e7f46bcd11e53dd52d382a
BLAKE2b-256 72246e4b19f2fc097f4bc4f1616822208da2c0d89917c398a979571b89787c2f

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