wecom-sdk

repository·release·Indexed 41 days ago

https://github.com/notfound403/wecom-sdk

A comprehensive Java implementation of the WeCom (Enterprise WeChat) Open API. It supports contact management, customer management, messaging, and robot webhooks. The SDK is built on Retrofit2, OkHttp4, Rxjava3, Jackson2, and XStream, and provides both a standard version and an RxJava version (version 1.3.4).

Tokens
1.3K
Snippets
4
Records
5
Agent score
2%

What's inside wecom-sdk

  1. How to find specific WeCom APIs in the SDK

    release

    The SDK implements over 200 WeCom interfaces. To find a specific method, you can map the official WeCom API endpoint to the SDK's internal interface structure.

    Example:

    1. Find the endpoint in official docs: https://qyapi.weixin.qq.com/cgi-bin/tag/create?access_token=ACCESS_TOKEN
    2. Extract the path: tag/create
    3. Perform a global search for tag/create in the SDK source code to find the corresponding interface method.
  2. Install the wecom-sdk via Maven

    release

    To use the standard version of the SDK, add the following dependency to your pom.xml.

    Note: The SDK requires Java 8 or higher and is built on Retrofit2, OkHttp4, Rxjava3, Jackson2, and XStream.

    <dependency>
        <groupId>cn.felord</groupId>
        <artifactId>wecom-sdk</artifactId>
        <version>1.3.4</version>
    </dependency>
  3. Install the RxJava version of wecom-sdk

    release

    If your project uses RxJava, use the rx-wecom-sdk artifact instead of the standard version.

    <dependency>
        <groupId>cn.felord</groupId>
        <artifactId>rx-wecom-sdk</artifactId>
        <version>1.3.4</version>
    </dependency>
  4. Resolve OkHttp version conflicts

    release

    If you encounter NoSuchMethodError during runtime, it is likely due to a version mismatch between the SDK's OkHttp requirements and your project's existing OkHttp version.

    To fix this, exclude the SDK's OkHttp dependencies and explicitly include version 4.12.0.

    <dependency>
        <groupId>cn.felord</groupId>
        <artifactId>wecom-sdk</artifactId>
        <version>1.3.4</version>
        <exclusions>
            <exclusion>
                <groupId>com.squareup.okhttp3</groupId>
                <artifactId>okhttp</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.squareup.okhttp3</groupId>
                <artifactId>logging-interceptor</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>4.12.0</version>
    </dependency>
    
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>logging-interceptor</artifactId>
        <version>4.12.0</version>
    </dependency>
  5. Send messages using WeCom Webhooks (Robot)

    release

    The SDK provides a high-level API for sending various message types via WeCom robot webhooks. You can send Markdown, plain text, articles, news, or images (via Base64 or InputStream).

    Use WorkWeChatApi.webhookApi().send("robot_key", body) to dispatch the message.

    // 发 markdown
    WebhookBody markdownBody=WebhookMarkdownBody.from("这里为markdown消息");
    
    // 发纯文本
    WebhookBody textBody=WebhookTextBody.from("这里为纯文本");
    
    // 发图文
    WebhookArticle article=new WebhookArticle("这里为标题","这里为图文链接")
                               .picurl("这里为封面图链接")
                               .description("这里为摘要信息");
    WebhookBody newsBody=WebhookNewsBody.from(Collections.singletonList(article));
    
    // 从base64发图片
    String base64="图片base64";
    String md5="图片base64的md5";
    WebhookBody imageBody1=WebhookImageBody.from(base64,md5);
    
    // 从流发送图片
    String path="C:\\Users\\Administrator\\Desktop\\0.png";
    InputStream inputStream=Files.newInputStream(Paths.get(path));
    WebhookBody imageBody2=WebhookImageBody.from(inputStream);
    
    // Send the message
    WeComResponse weComResponse=WorkWeChatApi.webhookApi().send("机器人key",markdownBody);
    Assertions.assertTrue(weComResponse.isSuccessful());