Skip to content

Integrate Feishu with Extension Library

Overview

This article demonstrates how to use the extension library to send Feishu message notifications.

For Feishu integration, please refer to Third-Party Integration

Implementation Method

Define Input Parameter Object

java
public class FeishuImMsgReq {
    private String receiveType;
    private String receiveId;
    private String msgType;
    private String content;
    getter...
    setter...
}

Define Feishu Response Message

java
public class FeishuImMessageRsp {
    private Integer code;
    private String msg;
    private FeishuImMessageInfo data;
    getter...
    setter...
}

Define Static Function to Send Feishu Message

java
public static FeishuImMessageInfo sendFeishuMessage(FeishuImMsgReq req) {
    Map<String, String> feishuMsgReq = Map.of(
            "receive_type", req.getReceiveType(),
            "receive_id", req.getReceiveId(),
            "msg_type", req.getMsgType(),
            "content", req.getContent(),
            "uuid", UUID.randomUUID().toString()
            );
    // Call Feishu tenant access token via SPI
    String tenantAccessToken = Informat.app().feishuTenantAccessToken();

    String url = "https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=" + req.getReceiveType();
    // Initiate HTTP request to call Feishu message sending interface
    HttpResponse httpResp = HttpUtil.createPost(url)
            .addHeaders(Map.of(
                    "Authorization", "Bearer " + tenantAccessToken,
                    "Content-Type", "application/json; charset=utf-8"
            ))
            .body(JSON.toJSONString(feishuMsgReq)).execute();
    if (!httpResp.isOk()) {
        throw new RuntimeException(String.format("Feishu message sending network request failed: %s, %s", httpResp.getStatus(), httpResp.body()));
    }
    String body = httpResp.body();
    // Call SPI to print response
    Informat.console().log("FeishuService getAppAccessToken: {}", body);
    FeishuImMessageRsp rsp = JSON.parseObject(body, FeishuImMessageRsp.class);
    Informat.console().log("rsp.Msg", rsp.getMsg());
    if (0 != rsp.getCode()) {
        Informat.console().log("FeishuService getAppAccessToken error, code: {}, msg: {}",
                rsp.getCode(), rsp.getMsg());
        throw new RuntimeException("Feishu message sending failed: " + rsp.getMsg());
    }
    // Return response value
    return rsp.getData();
}

Local Test API Call

In local calls, you need to specify the appId and appKey, which can be obtained from "Application Management - Application Settings" in the Informat platform.

java
public static void main(String[] args) {
    Informat.setServerAddress("https://next.informat.cn/web0");
    Informat.setAppId("appid");
    Informat.setApiKey("apiKey");
    FeishuImMsgReq req = new FeishuImMsgReq();
    // Feishu userId
    req.setReceiveId("xxxxxx");
    req.setReceiveType("user_id");
    req.setMsgType("text");
    req.setContent("{\"text\":\"Local RPC calls Informat Spi Plugin\"}");
    FeishuImMessageInfo msg = sendFeishuMessage(req);
    System.out.println(msg);
}

Operation Results

  • After successful local calling, package it into a zip file and upload it to the extension library

  • After successful upload, call the extension library through expressions

  • Call the extension library through scripts

  • Finally, check the results in Feishu