飞书消息推送对接
This commit is contained in:
+29
@@ -0,0 +1,29 @@
|
|||||||
|
package com.jero.modules.document.service;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liyawei
|
||||||
|
* @Description:
|
||||||
|
* @Date: Created in 13:40 2022/3/10
|
||||||
|
*/
|
||||||
|
public interface IFeishuService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取token
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
String getTenantAccessToken() throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量发送消息
|
||||||
|
* @param userIds 用户域账号,对应数据库字段:third_id
|
||||||
|
* @param message 消息内容
|
||||||
|
* @param title 消息类型
|
||||||
|
* @param backUrl 消息内容中的跳转链接
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
String batchSendMessage(String[] userIds, String message, String title, String backUrl) throws IOException;
|
||||||
|
}
|
||||||
+114
@@ -0,0 +1,114 @@
|
|||||||
|
package com.jero.modules.document.service.impl;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.jero.modules.document.service.IFeishuService;
|
||||||
|
import com.jero.modules.system.util.HttpRequestUtil;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liyawei
|
||||||
|
* @Description:
|
||||||
|
* @Date: Created in 13:41 2022/3/10
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class FeishuServiceImpl implements IFeishuService {
|
||||||
|
@Value("${Feishu.appId}")
|
||||||
|
private String appId;
|
||||||
|
|
||||||
|
@Value("${Feishu.appSecret}")
|
||||||
|
private String appSecret;
|
||||||
|
|
||||||
|
@Value("${Feishu.getTenantAccessTokenUrl}")
|
||||||
|
private String getTenantAccessTokenUrl;
|
||||||
|
|
||||||
|
@Value("${Feishu.batchSendMessageUrl}")
|
||||||
|
private String batchSendMessageUrl;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取token
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public String getTenantAccessToken() throws IOException {
|
||||||
|
Map<String, String> headerMap = new HashMap<>();
|
||||||
|
headerMap.put("Content-Type", "application/json; charset=utf-8");
|
||||||
|
Map<String, String> paramsMap = new HashMap<>();
|
||||||
|
paramsMap.put("app_id", appId);
|
||||||
|
paramsMap.put("app_secret", appSecret);
|
||||||
|
String response = HttpRequestUtil.getResponseOfPOST(getTenantAccessTokenUrl, headerMap, JSON.toJSONString(paramsMap));
|
||||||
|
//返回结果样例: {"code":0,"expire":7167,"msg":"ok","tenant_access_token":"t-3c05f981f36a8a92050718c6875e3c8b02c62304"}
|
||||||
|
JSONObject tokenJson = JSONObject.parseObject(response);
|
||||||
|
if(!tokenJson.get("code").toString().equals("0")) {
|
||||||
|
log.error("请求出现异常:" + tokenJson.get("msg") + " " + tokenJson);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String tenantAccessToken = tokenJson.get("tenant_access_token").toString();
|
||||||
|
return tenantAccessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量发送消息
|
||||||
|
* @param userIds 用户域账号,对应数据库字段:third_id
|
||||||
|
* @param message 消息内容
|
||||||
|
* @param title 消息类型
|
||||||
|
* @param backUrl 消息内容中的跳转链接
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public String batchSendMessage(String[] userIds, String message, String title, String backUrl) throws IOException {
|
||||||
|
String token = getTenantAccessToken();
|
||||||
|
// 设置请求头
|
||||||
|
Map<String, String> headerMap = new HashMap<>();
|
||||||
|
headerMap.put("Authorization", "Bearer " + token);
|
||||||
|
headerMap.put("Content-Type", "application/json; charset=utf-8");
|
||||||
|
// 设置消息标题
|
||||||
|
JSONObject zh_cn = new JSONObject();
|
||||||
|
zh_cn.put("title", title);
|
||||||
|
// 设置消息内容
|
||||||
|
// 第一行
|
||||||
|
JSONObject contentOne = new JSONObject();
|
||||||
|
contentOne.put("tag", "text");
|
||||||
|
contentOne.put("text", message);
|
||||||
|
// 第二行
|
||||||
|
JSONObject contentTwo = new JSONObject();
|
||||||
|
contentTwo.put("tag", "a");
|
||||||
|
contentTwo.put("text", "跳转链接");
|
||||||
|
contentTwo.put("href", backUrl);
|
||||||
|
List<JSONObject> contentList = new ArrayList<>();
|
||||||
|
contentList.add(contentOne);
|
||||||
|
contentList.add(contentTwo);
|
||||||
|
|
||||||
|
List<List<JSONObject>> list = new ArrayList<>();
|
||||||
|
list.add(contentList);
|
||||||
|
zh_cn.put("content", list);
|
||||||
|
|
||||||
|
JSONObject post = new JSONObject();
|
||||||
|
post.put("zh_cn", zh_cn);
|
||||||
|
JSONObject contentObj = new JSONObject();
|
||||||
|
contentObj.put("post", post);
|
||||||
|
// 设置请求参数
|
||||||
|
JSONObject paramsMap = new JSONObject();
|
||||||
|
paramsMap.put("user_ids", userIds);
|
||||||
|
paramsMap.put("msg_type", "post");
|
||||||
|
paramsMap.put("content", contentObj);
|
||||||
|
// 发送请求给飞书
|
||||||
|
String response = HttpRequestUtil.getResponseOfPOST(batchSendMessageUrl, headerMap, paramsMap.toJSONString());
|
||||||
|
// 返回结果样例:{"code":0,"data":{"invalid_department_ids":[],"invalid_open_ids":[],"invalid_union_ids":[],"invalid_user_ids":[],"message_id":"bm-d595567e4f448fafe397bc916f0be646"},"msg":"ok"}
|
||||||
|
// 获取返回结果
|
||||||
|
JSONObject tokenJson = JSONObject.parseObject(response);
|
||||||
|
if(!tokenJson.get("code").toString().equals("0")) {
|
||||||
|
log.error("请求出现异常:" + tokenJson.get("msg") + " " + tokenJson);
|
||||||
|
}
|
||||||
|
return tokenJson.get("msg").toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user