钉钉通知调整
This commit is contained in:
parent
668e248e84
commit
72868f61d0
@ -258,7 +258,7 @@ public class DeliveryDemandController extends JeecgController<DeliveryDemand, ID
|
||||
// 新增到明细表
|
||||
deliveryDemandDetailService.saveBatch(demandDetails);
|
||||
log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
|
||||
return Result.ok("文件导入成功!数据行数:");
|
||||
return Result.ok("文件导入成功!数据行数:" + demandDetails.size());
|
||||
} catch (Exception e) {
|
||||
//update-begin-author:taoyan date:20211124 for: 导入数据重复增加提示
|
||||
String msg = e.getMessage();
|
||||
|
@ -1,20 +1,33 @@
|
||||
package org.jeecg.modules.tms.utils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import okhttp3.*;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class DingTalkUtils {
|
||||
|
||||
@Slf4j
|
||||
public class DingTalkUtils {
|
||||
|
||||
// 获取 token 地址
|
||||
private static final String GET_TOKEN_URL = "https://oapi.dingtalk.com/gettoken";
|
||||
// 通过手机号获取 userId 地址
|
||||
private static final String GET_BY_MOBILE = "https://oapi.dingtalk.com/topapi/v2/user/getbymobile";
|
||||
// 发送消息地址
|
||||
private static final String ASYNC_SEND_V2_URL = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2";
|
||||
private static final String APP_KEY = "dingdz6no5d2igfucd8h"; //AppKey
|
||||
private static final String APP_SECRET = "YB0ieklneXHEUZ0dyRxu36w9AKSbjqVpS7z6iPE1_3YzTvS3yeQ-jieeP4Al0sIW";
|
||||
private static final String AGENT_ID = "3206304704"; // 应用的 AgentId
|
||||
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||||
|
||||
public static String getAccessToken() throws IOException {
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
String url = "https://oapi.dingtalk.com/gettoken?appkey=" + APP_KEY + "&appsecret=" + APP_SECRET;
|
||||
|
||||
String url = GET_TOKEN_URL + "?appkey=" + APP_KEY + "&appsecret=" + APP_SECRET;
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(url)
|
||||
.build();
|
||||
@ -22,7 +35,7 @@ public class DingTalkUtils {
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
if (!response.isSuccessful()) throw new IOException("Failed to retrieve access token");
|
||||
|
||||
String responseBody = response.body().string();
|
||||
String responseBody = response.body().string();
|
||||
JSONObject jsonObject = new JSONObject(responseBody);
|
||||
return jsonObject.getString("access_token");
|
||||
}
|
||||
@ -30,38 +43,42 @@ public class DingTalkUtils {
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
* @param accessToken 调用接口的身份凭证
|
||||
* @param agentId 应用的 AgentId
|
||||
* @param userId 用户id
|
||||
* @param chatId 群聊id
|
||||
* 给同一员工一天只能发送一条内容相同的消息通知
|
||||
* @param userId 用户id列表,最大用户列表长度 100
|
||||
* @param deptId 部门id列表,最大列表长度 20
|
||||
* @param message 消息
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void sendTextMessage(String accessToken, String agentId, String userId, String chatId, String message) throws IOException {
|
||||
public static void sendTextMessage(String userId, String deptId, String message) throws IOException {
|
||||
String accessToken = DingTalkUtils.getAccessToken();
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
String json = "{\"agent_id\":\"" + agentId + "\",\"userid_list\":\"" + userId + "\",\"deptid_list\":[\"" + userId + "\"],\"chatid\":\"" + chatId + "\",\"msg\":{\"msgtype\": \"text\",\"text\": {\"content\": \"" + message + "\"}}}";
|
||||
|
||||
String json;
|
||||
if (StringUtils.isNotEmpty(deptId)) {
|
||||
json = "{\"agent_id\":\"" + AGENT_ID + "\",\"userid_list\":\"" + userId + "\",\"dept_id_list\":\"" + deptId + "\",\"msg\":{\"msgtype\": \"text\",\"text\": {\"content\": \"" + message + "\"}}}";
|
||||
} else {
|
||||
json = "{\"agent_id\":\"" + AGENT_ID + "\",\"userid_list\":\"" + userId + "\",\"msg\":{\"msgtype\": \"text\",\"text\": {\"content\": \"" + message + "\"}}}";
|
||||
}
|
||||
RequestBody body = RequestBody.create(json, JSON);
|
||||
Request request = new Request.Builder()
|
||||
.url("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token=" + accessToken)
|
||||
.url(ASYNC_SEND_V2_URL + "?access_token=" + accessToken)
|
||||
.post(body)
|
||||
.build();
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
|
||||
System.out.println(response.body().string());
|
||||
log.info("发送消息响应:{}", response.body().string());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过手机号获取用户的 userId
|
||||
* @param accessToken 调用接口的身份凭证
|
||||
* @param mobile 手机号
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static JSONObject getUserByMobile(String accessToken, String mobile) throws IOException {
|
||||
public static String getUserByMobile(String mobile) throws IOException {
|
||||
String accessToken = DingTalkUtils.getAccessToken();
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
HttpUrl url = HttpUrl.parse("https://oapi.dingtalk.com/user/get_by_mobile")
|
||||
HttpUrl url = HttpUrl.parse(GET_BY_MOBILE)
|
||||
.newBuilder()
|
||||
.addQueryParameter("access_token", accessToken)
|
||||
.addQueryParameter("mobile", mobile)
|
||||
@ -75,19 +92,47 @@ public class DingTalkUtils {
|
||||
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
|
||||
|
||||
String responseBody = response.body().string();
|
||||
return new JSONObject(responseBody);
|
||||
log.info("获取手机号响应:{}", responseBody);
|
||||
String userid = "";
|
||||
JSONObject jsonObject = new JSONObject(responseBody);
|
||||
String errmsg = jsonObject.getString("errmsg");
|
||||
if ("ok".equals(errmsg)) {
|
||||
JSONObject result = jsonObject.getJSONObject("result");
|
||||
userid = result.get("userid").toString();
|
||||
}
|
||||
return userid;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取多个用户的 userId
|
||||
* @param mobiles 手机号
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String getUserByMobiles(List<String> mobiles) throws IOException {
|
||||
List<String> userIds = new ArrayList<>();
|
||||
for (String mobile : mobiles) {
|
||||
String userByMobile = getUserByMobile(mobile);
|
||||
if (StringUtils.isNotEmpty(userByMobile)) {
|
||||
userIds.add(userByMobile);
|
||||
}
|
||||
}
|
||||
return String.join(",", userIds);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
String accessToken = DingTalkUtils.getAccessToken();
|
||||
String agentId = "3206304704"; // 如果API调用不需要,则忽略此参数
|
||||
// String chatId = "095452471826097763,02000826241189265"; // 账号
|
||||
String chatId = "620665223329579"; // 账号
|
||||
String message = "Hello, this is a test message from Java!"; //发送内容
|
||||
sendTextMessage(accessToken, agentId, chatId, chatId,message);
|
||||
} catch (IOException e) {
|
||||
String message = "你好啊"; //发送内容
|
||||
sendTextMessage(chatId, "",message);
|
||||
/*List<String> phoneList = Arrays.asList("18726282704","18726282704");
|
||||
String a = getUserByMobiles(phoneList);
|
||||
System.out.println(a);*/
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user