钉钉推送消息
public void pushMessage(final String id, final String devNum) {
String pushIds = mobileCaseReceiveRecordMapper.queryPushIdByEventId(id);
if (!StringUtils.isEmpty(pushIds)) {
Long send = dingTalkService.send(pushIds);
dingTalkService.getSendResult(send);
}
}
package com.microvideo.ewcp.notSceneMove.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.request.OapiMessageCorpconversationGetsendresultRequest;
import com.dingtalk.api.request.OapiUserGetRequest;
import com.dingtalk.api.request.OapiUserGetuserinfoRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiMessageCorpconversationGetsendresultResponse;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.dingtalk.api.response.OapiUserGetuserinfoResponse;
import com.microvideo.ewcp.notSceneMove.constants.Constants;
import com.microvideo.ewcp.notSceneMove.entity.DingTalkUserInfo;
import com.microvideo.ewcp.notSceneMove.service.DingTalkService;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Service
@Slf4j
public class DingTalkServiceImpl implements DingTalkService {
private static final String APPURL = "http://139.195.212:8089/TZTP_DD/#/pages/index/index";
@Override
public String getAccessToken() {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
OapiGettokenRequest request = new OapiGettokenRequest();
request.setAppkey(Constants.APPKEY);
request.setAppsecret(Constants.APPSECRET);
request.setHttpMethod("GET");
OapiGettokenResponse response = null;
try {
response = client.execute(request);
JSONObject object = JSONObject.parseObject(response.getBody());
if(object != null && "0".equals(object.getString("errcode"))){
return object.getString("access_token");
}
} catch (ApiException e) {
log.error("钉钉api错误");
log.error("", e);
}
return null;
}
public DingTalkUserInfo getUserInfo(final String code){
try {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
OapiUserGetRequest req = new OapiUserGetRequest();
req.setUserid(getUserid(code));
OapiUserGetResponse rsp = client.execute(req, getAccessToken());
JSONObject object = JSONObject.parseObject(rsp.getBody());
if(object != null && "0".equals(object.getString("errcode"))){
JSONObject result = object.getJSONObject("result");
DingTalkUserInfo userInfo = new DingTalkUserInfo();
userInfo.setUserId(result.getString("userid"));
userInfo.setName(result.getString("name"));
userInfo.setAssociatedUnionId(result.getString("associated_unionid"));
userInfo.setUnionId(result.getString("unionid"));
userInfo.setDeviceId(result.getString("device_id"));
userInfo.setMobile(result.getString("mobile"));
userInfo.setAvatar(result.getString("avatar"));
return userInfo;
}
} catch (ApiException e) {
log.error("钉钉api错误");
log.error("", e);
}
return null;
}
@Override
public String getUserid(final String code) {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getuserinfo");
OapiUserGetuserinfoRequest req = new OapiUserGetuserinfoRequest();
req.setCode(code);
OapiUserGetuserinfoResponse rsp = null;
try {
rsp = client.execute(req, getAccessToken());
JSONObject object = JSONObject.parseObject(rsp.getBody());
if(object != null && "0".equals(object.getString("errcode"))){
JSONObject result = object.getJSONObject("result");
return result.getString("userid");
}
} catch (ApiException e) {
log.error("钉钉api错误");
log.error("", e);
}
return null;
}
public Long send(final String userIds){
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
request.setAgentId(Long.parseLong(Constants.AGENTID));
request.setUseridList(userIds);
request.setToAllUser(false);
OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
msg.setMsgtype("link");
msg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link());
msg.getLink().setTitle("泰州非现场执法");
msg.getLink().setText(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "您有一条新的违法信息待处理");
msg.getLink().setMessageUrl(APPURL);
msg.getLink().setPicUrl("@lADPDf0i2Po0VBrNAQDNAQA");
request.setMsg(msg);
OapiMessageCorpconversationAsyncsendV2Response rsp = null;
try {
rsp = client.execute(request, getAccessToken());
} catch (ApiException e) {
log.error("钉钉api错误");
log.error("", e);
}
if(rsp==null){
return null;
}
JSONObject object = JSONObject.parseObject(rsp.getBody());
Long taskId = null;
if(object != null && "0".equals(object.getString("errcode"))){
taskId = object.getLong("task_id");
}
return taskId;
}
public OapiMessageCorpconversationGetsendresultRequest getSendResult(final Long taskId){
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/getsendresult");
OapiMessageCorpconversationGetsendresultRequest req = new OapiMessageCorpconversationGetsendresultRequest();
req.setAgentId(Long.parseLong(Constants.AGENTID));
req.setTaskId(taskId);
OapiMessageCorpconversationGetsendresultResponse rsp = null;
try {
rsp = client.execute(req, getAccessToken());
log.info(rsp.getBody());
} catch (ApiException e) {
log.error("钉钉api错误");
log.error("", e);
}
return req;
}
}