即时消息告警工具(二)飞书

飞书告警

登陆飞书移动端后,点击 "添加账号"

现在登陆的是管理员账号,账号只有审批权限,没有创建应用的权限

我们要切换成能创建应用的账号的权限

登陆飞书Web端

1.https://open.feishu.cn/app
飞书后台,可以创建应用,但是需要管理员审批

2.https://va8ocx82fwg.feishu.cn/admin/appCenter/audit
飞书后台,可以审批应用,但是没办法创建应用

授权的时候 选择 "授权登陆全部账号"
这样就能在一个浏览器上同时登陆"管理员审批账号"和"创建应用账号"

飞书Web端创建应用 https://open.feishu.cn/app

申请应用的权限

这下面有多页,记得最下面翻页,我自己就是管理员,所以权限值直接通过的

创建版本和申请发布应用

飞书Web端审批应用  https://va8ocx82fwg.feishu.cn/admin/appCenter/audit

记录App ID和AppSecret (后续很重要)

调试代码

添加依赖
添加依赖
        <dependency>
            <groupId>com.larksuite.oapi</groupId>
            <artifactId>oapi-sdk</artifactId>
            <version>2.0.18</version>
        </dependency>

如果无法获取依赖,继续添加
    <repositories>
        <repository>
            <id>Central Repository</id>
            <url>https://repo1.maven.org/maven2/</url>
        </repository>
    </repositories>
调试代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
import java.util.UUID;


public class SendMessageFeishu {

    public static JSONObject http_post_value_json(String url,String head,String requestBody,String errcode,String errcode_value) throws Exception {
        HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type""application/json");
        JSONObject head_json_object = new JSONObject(head);
        for (String key :head_json_object.keySet()){
            con.setRequestProperty(key, head_json_object.getString(key));
        }
        con.setDoOutput(true);

        OutputStream os = con.getOutputStream();
        os.write(requestBody.getBytes());
        os.flush();
        os.close();

        int responseCode = con.getResponseCode();
        BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String response = "";
        String line;
        while ((line = br.readLine()) != null) {
            response += line;
        }
        br.close();

        if(responseCode == 200){
            JSONObject jsonObject = new JSONObject(response);
            if(jsonObject.getString(errcode).equals(errcode_value)){
                return jsonObject;
            }
        }
        System.out.println(response);
        System.out.println("调用失败");
        return new JSONObject();
    }

    public static void main(String arg[]) throws Exception {
        String tenant_access_token_url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal";
        String tenant_access_token_header = "{}";
        String tenant_access_token_requestBody = "{\"app_id\":\"替换成APPID\",\"app_secret\":\"替换成AppSecret\"}";
        String tenant_access_token = http_post_value_json(tenant_access_token_url,tenant_access_token_header,tenant_access_token_requestBody,"code","0").getString("tenant_access_token");
        System.out.println("tenant_access_token:"+tenant_access_token);


        String get_id_url = "https://open.feishu.cn/open-apis/contact/v3/users/batch_get_id?user_id_type=open_id";
        String get_id_header = "{\"Authorization\":\"Bearer "+tenant_access_token+"\"}";
        String get_id_requestBody = "{\"mobiles\":[\"通过电话查找用户ID\"]}";
        String user_id = http_post_value_json(get_id_url,get_id_header,get_id_requestBody,"code","0")
                .getJSONObject("data").getJSONArray("user_list").getJSONObject(0).getString("user_id");
        System.out.println("user_id:"+user_id);


        String send_message_url = "https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=open_id";
        String send_message_header = "{\"Authorization\":\"Bearer "+tenant_access_token+"\"}";
        String send_message_requestBody = "{\"content\": \"{\\\"text\\\":\\\"飞书测试消息\\\"}\",\"msg_type\": \"text\"," +
                "\"receive_id\": \""+user_id+"\"," + "\"uuid\": \""+UUID.randomUUID()+"\"}";
//        System.out.println(send_message_requestBody);
        System.out.println(http_post_value_json(send_message_url,send_message_header,send_message_requestBody,"code","0").get("data"));
        System.out.println("发送成功");

    }
}
posted @ 2023-04-23 19:26  Kotlin  阅读(128)  评论(0编辑  收藏  举报
Live2D