WeChatAuto.SDK

repository·master·Indexed 42 days ago

https://github.com/scottfly189/wechatauto.sdk

A modern RPA framework for WeChat automation optimized for AI/LLM integration. It supports .NET and Python, offering automation strategies like UI Tree and Hybrid OCR (via RapidOCRCSharp) for WeChat versions 4.1.xx and 3.9.12.xx. Features include message listening, social capabilities, group management, and a WebSocket server to bridge .NET functionality to other languages. Includes a Python community edition package (wechat-auto4x-sdk v1.0.0).

Tokens
8.6K
Snippets
25
Records
38
Agent score
54%

What's inside WeChatAuto.SDK

  1. Overview of WeChatAuto.SDK

    master
    WeChatAuto.SDK is a modern WeChat RPA (Robotic Process Automation) framework designed for AI integration (e.g., LLM context interaction). It is built on .NET and UI automation technologies. The SDK supports both .NET and Python, allowing developers to integrate WeChat automation into existing systems. It offers features like message listening, social capabilities (sending text, emojis, files, @mentions), group management, and support for multiple WeChat instances via local execution or WebSocket long connections.
  2. Overview of WebChatAuto.SDK Web Support

    master

    WebChatAuto.SDK Web Support extends the core SDK by providing a Visual UI and a REST API service. This allows developers to control the WeChat client via HTTP requests.

    To mitigate WeChat risk control (anti-spam) measures, the system implements a "pooling" mechanism for outgoing messages. This means messages are queued and sent at a controlled rate, making it suitable for high-volume batch messaging without the risk of being logged out due to high frequency.

  3. Compare WeChat 4.1.xx and 3.9.12.xx automation solutions

    master

    The SDK provides two distinct automation paths depending on the WeChat version:

    WeChat 4.1.xx (Latest Version)

    • Solution: Hybrid UI Tree + OCR automation.
    • OCR Engine: Uses local onnx-runtime models via RapidOCRCSharp.
    • Platforms: Supports both .NET and Python.
    • Open Source: Core code and documentation are 100% open source; business code is 80% open source (20% of APIs are VIP-exclusive).

    WeChat 3.9.12.xx (Legacy Version)

    • Solution: Pure UI Tree automation.
    • Platforms: Supports .NET only.
    • Open Source: 100% open source.
  4. System Requirements and Risk Mitigation

    master

    System Requirements

    • OS: Windows (Linux and MacOS are not supported).
    • Runtime: .NET 6.0 or higher (Note: Python SDK does not require .NET).

    Risk Management (Anti-Detection)

    Frequent automated operations may trigger WeChat's risk control mechanisms. To mitigate this:

    • Use Hardware Keyboard/Mouse Simulators: Instead of software-based API injection (like PostMessage), use hardware simulators to mimic real human input. This is highly recommended for high-sensitivity operations like adding friends.
    • Control Frequency: Avoid performing large volumes of actions in a short period.
    • Simulate Human Behavior: Use the simulator to provide more natural input patterns.
  5. Setup and use WeChatAuto.SDK for .NET (WeChat 4.x)

    master

    To use the latest WeChat 4.x automation in a .NET project:

    1. Create a project: Create a .NET console project.
    2. Configure Target Framework: Ensure your .csproj targets a Windows-specific framework (e.g., net10.0-windows, net6.0-windows, or net7.0-windows).
    3. Install Dependencies:
      dotnet add package Microsoft.Extensions.DependencyInjection
      dotnet add package WeChatAuto4x.SDK
    4. Add OCR Models: Download the OCR model files and place the models folder in your project root.
    5. Initialize and Run: Use WeAutomation.Initialize with EnableOCR = true to start the service.
    using WeChatAuto.Services;
    using WeChatAuto.Components;
    using Microsoft.Extensions.DependencyInjection;
    
    var serviceProvider = WeAutomation.Initialize(options =>
    {
        options.DebugMode = false;   // Optional
        options.EnableOCR = true;    // Required for 4.x
    });
    
    using var clientFactory = serviceProvider.GetRequiredService<WeChatClientFactory>();
    
    // Check available WeChat clients
    var wechat_list = clientFactory.GetWeChatClientNames();
    Console.WriteLine($"Open WeChat clients: {string.Join(',', wechat_list)}");
    
    // Open the first client and send a message
    var client = clientFactory.GetWeChatClient(wechat_list.First());
    await client.SendMessage("AI.Net_test", "hello world!!");
  6. Set up and use WebChatAuto.SDK Web Support

    master

    Follow these steps to integrate WeChat automation via REST API:

    1. Prepare WeChat: Ensure your WeChat client is version 3.9.12.xx. This specific release is built for this version.
    2. Start the Service: Run the application and click the 【运行】 (Run) button in the UI. This starts the local Web API service.
    3. Access API Documentation: Once running, the service defaults to http://localhost:5000. You can access the Swagger UI at http://localhost:5000/swagger to view available endpoints and test them manually.
    4. Integrate: Use any language that supports HTTP (e.g., Python, Node.js) to send requests to the local API endpoints.
    http://localhost:5000/swagger
  7. Setup and use WeChatAuto.SDK for Python (WeChat 4.x)

    master

    The Python SDK communicates with a WebSocket Server and does not require .NET installed on the client machine.

    1. Start the WebSocket Server:
      • Download the WeChatAuto4x.SDK WebSocket Server.
      • Run Server.exe. It typically listens on http://localhost:5000.
    2. Initialize Python Project:
      uv init demo-python
      cd demo-python
      uv add wechat-auto4x-sdk
    3. **Implement Automation**:
       Connect to the server using `WechatFactory.create_factory` with a WebSocket URI (e.g., `ws://localhost:5000/ws`).
    
    ```python
    import asyncio
    from wechat_auto_sdk import WeChatClient
    from wechat_auto_sdk import WeChatConfig
    from wechat_auto_sdk import WechatFactory
    
    DEFAULT_URI = "ws://localhost:5000/ws"
    
    async def wechat_automation():
        async with WechatFactory.create_factory(DEFAULT_URI, WeChatConfig()) as factory:
            await factory.initialize()
            wechat_list = factory.client_list
            print(f"Connected to {len(wechat_list)} clients: {list(wechat_list.keys())}")
            
            # Access a specific client by name
            client: WeChatClient = wechat_list["Alex"]
            
            # Send message with @mentions
            await client.send_message(
                "DroidMirror官方技术支持",
                "hello world!",
                at_user=["AI.Net_test", "智影工坊"]
            )
    
    if __name__ == "__main__":
        asyncio.run(wechat_automation())
  8. Troubleshoot WeChat automation failures

    master

    If the example automation fails to control the WeChat client, perform the following checks:

    1. Taskbar Placement: Ensure the WeChat window is dragged to the taskbar.
    2. OS Compatibility: Verify you are using Windows 10 or Windows 11.
    3. UI Tree Availability: If using WeChat version 4.1.xx, check if a UI Tree is available. You can verify this using tools like inspect.exe or FlaUInspect (available in the project's Tools directory).
      • If no UI Tree is detected, refer to the specific issue documentation for handling missing UI Trees.

    If these steps do not resolve the issue, contact the author with your code for assistance.