赞
踩
- <dependency>
- <groupId>com.larksuite.oapi</groupId>
- <artifactId>oapi-sdk</artifactId>
- <version>2.1.3</version>
- </dependency>
-
- public Result<?> sendTextByGroupRobots() {
- feiShuNotifyRequest.sendTextByGroupRobots(FeiShuConfig.WEB_HOOK_URL,"牛爷爷吃饭");
- return Result.OK();
- }
-
-
- public Result<?> sendTextAboutUserByGroupRobots() {
- feiShuNotifyRequest.sendTextAboutUserByGroupRobots(FeiShuConfig.WEB_HOOK_URL,"牛爷爷打游戏","ou_b406xxxxxxxxxxx221630");
- return Result.OK();
- }
-
- public Result<?> sendRichTextByGroupRobots() {
- feiShuNotifyRequest.sendRichTextByGroupRobots(FeiShuConfig.WEB_HOOK_URL,"牛爷爷看电影","https://xxxxxxxxxxxxxxxxxxC0nVg","ou_b4xxxxxxxxxxxxx30");
- return Result.OK();
- }
-
-
- public Result<?> sendMessageToUser() {
- feiShuNotifyRequest.sendMessageToUser("ou_bxxxxxxxxxxxxxxxxxxx30","强壮的牛爷爷下班啦");
- return Result.OK();
- }
-
- public Result<?> sendImage() {
- SendImageRequest request = new SendImageRequest();
- try {
- feiShuNotifyRequest.sendImage(request);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return Result.OK();
- }
-
- public Result<?> sendFile() {
- SendFileRequest request = new SendFileRequest();
- request.setFileType("xlsx");
- request.setFileName("demo.xlsx");
- request.setFile(new File("D:\\demo.xlsx"));
- request.setReceiveIdType("open_id");
- request.setReceiveId("ou_b4xxxxxxxxxxxxxxxxxxxxx1630");
- try {
- feiShuNotifyRequest.sendFile(request);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return Result.OK();
- }
-
-
- public Result<?> sendFilePdf() {
- SendFileRequest request = new SendFileRequest();
- request.setFileType("pdf");
- request.setFileName("高并发解决方案回答.pdf");
- request.setFile(new File("E:\\高并发解决方案回答.pdf"));
- request.setReceiveIdType("open_id");
- request.setReceiveId("ou_b4xxxxxxxxxxxxxxxxxxxx1630");
- try {
- feiShuNotifyRequest.sendFile(request);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return Result.OK();
- }
-
-
- /**
- * @author Sherry
- * @description 文本消息
- */
- @Data
- @Service
- public class FeiShuTextNotifyRequest implements FeiShuNotifyRequest {
- private static final Client client = Client.newBuilder(FeiShuConfig.APP_ID, FeiShuConfig.APP_SECRET).build();
-
- @Override
- public void sendTextByGroupRobots(String webhookUrl,String data) {
- //请求的JSON数据,这里用map在工具类里转成json格式
- JSONObject json=new JSONObject();
- JSONObject text=new JSONObject();
- json.put("msg_type", "text");
- text.put("text", "项目告警通知:" + data);
- json.put("content", text);
-
- String result = HttpRequest.post(webhookUrl).body(JSONObject.toJSONString(json), "application/json;charset=UTF-8").execute().body();
- System.out.println(result);
- }
-
- @Override
- public void sendTextAboutUserByGroupRobots(String webhookUrl, String data, String userOpenId) {
- JSONObject json=new JSONObject();
- json.put("msg_type", "text");
- json.put("content", "{\"text\":\"<at user_id=\\\""+ userOpenId +"\\\"></at> "+data+"\"}");
-
- String result = HttpRequest.post(webhookUrl).body(JSONObject.toJSONString(json), "application/json;charset=UTF-8").execute().body();
- }
-
- @Override
- public void sendRichTextByGroupRobots(String webhookUrl, String data, String href, String userOpenId) {
- RichText richText = new RichText();
- richText.setMsg_type("post");
-
- JSONObject postJson = new JSONObject();
-
- JSONArray content = new JSONArray();
- JSONArray contentArray = new JSONArray();
-
- JSONObject urlJson = new JSONObject();
- urlJson.put("tag", "a");
- urlJson.put("text","请查看");
- urlJson.put("href", href);
- contentArray.add(urlJson);
-
- JSONObject userJson = new JSONObject();
- userJson.put("tag", "at");
- userJson.put("user_id", userOpenId);
- contentArray.add(userJson);
- content.add(contentArray);
-
- HashMap<String, Object> title = new HashMap<>();
-
- JSONObject contentJson = new JSONObject();
- contentJson.put("title", data);
- contentJson.put("content", content);
-
- JSONObject zhCnJson = new JSONObject();
- zhCnJson.put("zh_cn", contentJson);
-
-
- postJson.put("post",zhCnJson);
- richText.setContent(postJson);
- String result = HttpRequest.post(webhookUrl).body(JSONObject.toJSONString(richText), "application/json;charset=UTF-8").execute().body();
- }
-
- @Override
- public void sendMessageToUser(String userOpenId,String message){
- // 创建请求对象
- CreateMessageReq req = CreateMessageReq.newBuilder()
- .receiveIdType("open_id")
- .createMessageReqBody(CreateMessageReqBody.newBuilder()
- .receiveId(userOpenId)
- .msgType("text")
- .content("{\"text\":\" "+message+"\"}")
- .uuid(UUIDGenerator.generate())
- .build())
- .build();
- // 发起请求
- CreateMessageResp resp = null;
- try {
- resp = client.im().message().create(req);
- } catch (Exception e) {
- e.printStackTrace();
- }
- // 处理服务端错误
- if(!resp.success()) {
- System.out.println(String.format("code:%s,msg:%s,reqId:%s", resp.getCode(), resp.getMsg(), resp.getRequestId()));
- return;
- }
-
- // 业务数据处理
- System.out.println(Jsons.DEFAULT.toJson(resp.getData()));
-
- }
-
- /**
- * 发送图片消息,使用到两个OpenAPI:
- * 1. [上传图片](<a href="https://open.feishu.cn/document/server-docs/im-v1/image/create">...</a>)
- * 2. [发送消息](<a href="https://open.feishu.cn/document/server-docs/im-v1/message/create">...</a>)
- */
- @Override
- public BaseResponse<?> sendImage(SendImageRequest request) throws Exception {
- request.setImage(new File("C:/Users/jhf/Pictures/Camera Roll/emo.jpg"));
- request.setReceiveIdType("open_id");
- request.setReceiveId("ou_b40xxxxxxxxxxxxxxxxxxxx21630");
-
-
- // 上传图片
- CreateImageReq req = CreateImageReq.newBuilder()
- .createImageReqBody(CreateImageReqBody.newBuilder()
- .imageType("message")
- .image(request.getImage())
- .build())
- .build();
-
- CreateImageResp createImageResp = client.im().image().create(req);
- System.out.println(createImageResp);
- if (!createImageResp.success()) {
- System.out.printf("client.im.image.create failed, code: %d, msg: %s, logId: %s%n",
- createImageResp.getCode(), createImageResp.getMsg(), createImageResp.getRequestId());
- return createImageResp;
- }
-
- // 发送消息
- CreateMessageReq createMessageReq = CreateMessageReq.newBuilder()
- .receiveIdType(request.getReceiveIdType())
- .createMessageReqBody(CreateMessageReqBody.newBuilder()
- .receiveId(request.getReceiveId())
- .msgType("image")
- .content(Jsons.DEFAULT.toJson(createImageResp.getData()))
- .uuid(request.getUuid())
- .build())
- .build();
-
- CreateMessageResp createMessageResp = client.im().message().create(createMessageReq);
- if (!createMessageResp.success()) {
- System.out.printf("client.im.message.create failed, code: %d, msg: %s, logId: %s%n",
- createMessageResp.getCode(), createMessageResp.getMsg(), createMessageResp.getRequestId());
- return createMessageResp;
- }
-
- // 返回结果
- SendImageResponse response = new SendImageResponse();
- response.setCode(0);
- response.setMsg("success");
- response.setCreateImageResponse(createImageResp.getData());
- response.setCreateMessageResponse(createMessageResp.getData());
-
- return response;
- }
-
- @Override
- public BaseResponse<?> sendFile(SendFileRequest request) throws Exception {
- // 上传文件
- CreateFileReq createFileReq = CreateFileReq.newBuilder()
- .createFileReqBody(CreateFileReqBody.newBuilder()
- .fileType(request.getFileType())
- .fileName(request.getFileName())
- .duration(request.getDuration())
- .file(request.getFile())
- .build())
- .build();
-
- CreateFileResp createFileResp = client.im().file().create(createFileReq);
- if (!createFileResp.success()) {
- System.out.printf("client.im.file.create failed, code: %d, msg: %s, logId: %s%n",
- createFileResp.getCode(), createFileResp.getMsg(), createFileResp.getRequestId());
- return createFileResp;
- }
-
- // 发送消息
- CreateMessageReq createMessageReq = CreateMessageReq.newBuilder()
- .receiveIdType(request.getReceiveIdType())
- .createMessageReqBody(CreateMessageReqBody.newBuilder()
- .receiveId(request.getReceiveId())
- .msgType("file")
- .content(Jsons.DEFAULT.toJson(createFileResp.getData()))
- .uuid(request.getUuid())
- .build())
- .build();
-
- CreateMessageResp createMessageResp = client.im().message().create(createMessageReq);
- if (!createMessageResp.success()) {
- System.out.printf("client.im.message.create failed, code: %d, msg: %s, logId: %s%n",
- createMessageResp.getCode(), createMessageResp.getMsg(), createMessageResp.getRequestId());
- return createMessageResp;
- }
-
- // 返回结果
- SendFileResponse response = new SendFileResponse();
- response.setCode(0);
- response.setMsg("success");
- response.setCreateFileResponse(createFileResp.getData());
- response.setCreateMessageResponse(createMessageResp.getData());
-
- return response;
- }
-
- @Override
- public void sendWarnMessage(String userOpenId, String msgType, String content) throws Exception {
- CreateMessageReq req = CreateMessageReq.newBuilder()
- .receiveIdType("open_id")
- .createMessageReqBody(CreateMessageReqBody.newBuilder()
- .receiveId(userOpenId)
- .msgType(msgType)
- .content(BuildCard())
- .build())
- .build();
-
- CreateMessageResp resp = client.im().message().create(req);
-
- if (!resp.success()) {
- throw new Exception(String.format("client.im.message.create failed, code: %d, msg: %s, logId: %s",
- resp.getCode(), resp.getMsg(), resp.getRequestId()));
- }
- }
-
- // 构建卡片
- public static String BuildCard() throws Exception {
-
- // 获取卡片结构
- File jsonFile = new File("E:\\Coding\\xxxxxxxxxxxxxxxxx\\card\\card.json");
- FileReader fileReader = new FileReader(jsonFile);
- Reader reader = new InputStreamReader(new FileInputStream(jsonFile), StandardCharsets.UTF_8);
- int ch = 0;
- StringBuilder sb = new StringBuilder();
- while ((ch = reader.read()) != -1) {
- sb.append((char) ch);
- }
- fileReader.close();
- reader.close();
- String card = sb.toString();
-
-
- return card;
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。