import com.alibaba.fastjson.JSONObject;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
import com.aliyun.dingtalkrobot_1_0.Client;
import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOHeaders;
import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTORequest;
import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.dingtalk.open.app.api.OpenDingTalkClient;
import com.dingtalk.open.app.api.OpenDingTalkStreamClientBuilder;
import com.dingtalk.open.app.api.callback.OpenDingTalkCallbackListener;
import com.dingtalk.open.app.api.security.AuthClientCredential;
import lombok.extern.slf4j.Slf4j;
import java.util.Objects;
@Slf4j
public class sendPrivateMessage {
public static final String CLIENT_ID = "< your client id>";
public static final String CLIENT_SECRET = "<your client secret>";
public static void main(String[] args) throws Exception {
OpenDingTalkClient client = OpenDingTalkStreamClientBuilder
.custom()
.credential(new AuthClientCredential(CLIENT_ID, CLIENT_SECRET))
.registerCallbackListener("/v1.0/im/bot/messages/get", new sendPrivateMessage.RobotMsgCallbackConsumer())
.build();
client.start();
}
public static class RobotMsgCallbackConsumer implements OpenDingTalkCallbackListener<JSONObject, JSONObject> {
/*
* @param request
* @return
*/
@Override
public JSONObject execute(JSONObject request) {
String userId = request.get("senderStaffId").toString();
String content = request.getJSONObject("text").getString("content");
String robotCode = request.get("robotCode").toString();
log.info("receive bot message from user={}, msg={},robotCode={} ", userId, content,robotCode);
BatchSendOTOHeaders batchSendOTOHeaders = new BatchSendOTOHeaders();
batchSendOTOHeaders.setXAcsDingtalkAccessToken(getToken());
BatchSendOTORequest batchSendOTORequest = new BatchSendOTORequest();
batchSendOTORequest.setMsgKey("sampleText");
batchSendOTORequest.setRobotCode(robotCode);
batchSendOTORequest.setUserIds(java.util.Arrays.asList(userId));
JSONObject msgParam = new JSONObject();
msgParam.put("content", "java-getting-start say : " + "hello");
batchSendOTORequest.setMsgParam(msgParam.toJSONString());
try {
Config config = new Config();
config.protocol = "https";
config.regionId = "central";
com.aliyun.dingtalkrobot_1_0.Client client = new Client(config);
BatchSendOTOResponse batchSendOTOResponse = client.batchSendOTOWithOptions(batchSendOTORequest, batchSendOTOHeaders, new RuntimeOptions());
if (Objects.isNull(batchSendOTOResponse) || Objects.isNull(batchSendOTOResponse.getBody())) {
log.error("RobotPrivateMessages_send batchSendOTOResponse return error, response={}",
batchSendOTOResponse);
return null;
}
return new JSONObject();
} catch (TeaException e) {
log.error("RobotPrivateMessages_send batchSendOTOResponse throw TeaException, errCode={}, " +
"errorMessage={}", e.getCode(), e.getMessage(), e);
throw e;
} catch (Exception e) {
log.error("RobotPrivateMessages_send batchSendOTOResponse throw Exception", e);
try {
throw e;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
public static String getToken() {
GetAccessTokenRequest getAccessTokenRequest = new GetAccessTokenRequest();
getAccessTokenRequest.setAppKey(CLIENT_ID);
getAccessTokenRequest.setAppSecret(CLIENT_SECRET);
Config config = new Config();
config.protocol = "https";
config.regionId = "central";
try {
com.aliyun.dingtalkoauth2_1_0.Client client = new com.aliyun.dingtalkoauth2_1_0.Client(config);
GetAccessTokenResponse accessToken = client.getAccessToken(getAccessTokenRequest);
return accessToken.getBody().getAccessToken();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}