com.hn.utils.dingtalk.MessageUtil Maven / Gradle / Ivy
package com.hn.utils.dingtalk;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.request.OapiProcessinstanceGetRequest;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiProcessinstanceGetResponse;
import com.hn.utils.dingtalk.constant.DingConstant;
import com.hn.utils.dingtalk.constant.DingURLConstant;
import com.taobao.api.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author 令久
* 2018/08/28
*/
public class MessageUtil {
private static final Logger bizLogger = LoggerFactory.getLogger(MessageUtil.class);
public static void sendMessageToOriginator(String processInstanceId) throws RuntimeException {
try {
DingTalkClient client = new DefaultDingTalkClient(DingURLConstant.URL_PROCESSINSTANCE_GET);
OapiProcessinstanceGetRequest request = new OapiProcessinstanceGetRequest();
request.setProcessInstanceId(processInstanceId);
OapiProcessinstanceGetResponse response = client.execute(request, AccessTokenUtil.getToken());
String recieverUserId = response.getProcessInstance().getOriginatorUserid();
client = new DefaultDingTalkClient(DingURLConstant.MESSAGE_ASYNCSEND);
OapiMessageCorpconversationAsyncsendV2Request messageRequest = new OapiMessageCorpconversationAsyncsendV2Request();
messageRequest.setUseridList(recieverUserId);
messageRequest.setAgentId(Long.valueOf(DingConstant.AGENTID));
messageRequest.setToAllUser(false);
OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
msg.setMsgtype("text");
msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
msg.getText().setContent("出差申请通过了,快去订机票吧");
messageRequest.setMsg(msg);
OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(messageRequest,AccessTokenUtil.getToken());
} catch (ApiException e) {
bizLogger.error("send message failed", e);
throw new RuntimeException();
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy