图例
功能
对接钉钉自动推送消息
代码
controller
package com.yingwei.dingding.controller;
import com.alibaba.fastjson.JSONObject;
import com.yingwei.common.constant.SysConfigConstants;
import com.yingwei.common.core.domain.AjaxResult;
import com.yingwei.common.core.domain.entity.SysUser;
import com.yingwei.dingding.domain.DdMpConfig;
import com.yingwei.dingding.utils.DingDingUtils;
import com.yingwei.system.service.ISysConfigService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.Date;
/**
* @authorzxq
* @description钉钉推送服务类
* @date2020/11/26
*/
@RestController
@RequestMapping("/dd/news")
public class DingController {
@Autowired
private ISysConfigService sysConfigService;
@PostMapping("/sendDdMsg")
public AjaxResult sendDdMsg(@RequestBody SysUseruser) throws IOException {
String msgcontent = "{\"message_url\":\"http://baidu.com\",\"head\":{\"bgcolor\":\"FFBBBBBB\",\"text\":\"头部标题\"},\"body\":{\"title\":\"正文标题\",\"form\":[{\"key\":\"姓名:\",\"value\":\"张三\"},{\"key\":\"爱好:\",\"value\":\"打球、听音乐\"},{\"key\":\"时间:\",\"value\":\"" + newDate() + "\"}],\"rich\":{\"num\":\"15.6\",\"unit\":\"元\"},\"content\":\"大段文本大段文本大段djfksj\",\"image\":\"@lADOADmaWMzazQKA\",\"file_count\":\"3\",\"author\":\"李a\"}}";
String wxConfig = sysConfigService.selectConfigByKey(SysConfigConstants.MSG_DD);
DdMpConfig ddMpConfig = JSONObject.parseObject(wxConfig, DdMpConfig.class);
String token = DingDingUtils.getToken(ddMpConfig.getAppKey(), ddMpConfig.getAppSecret());
String userId = DingDingUtils.getUserId(user.getPhonenumber(), token);
if (userId != null) {
DingDingUtils.sendDDMessage(Long.valueOf(ddMpConfig.getAgentId()), userId, msgcontent, token);
}
return AjaxResult.success("手机不存在");
}
}
Domain
package com.yingwei.dingding.domain;
import lombok.Data;
import java.util.List;
/**
*@authorzxq
*@description发送钉钉消息入参
*@date2020/11/13
*/
@Data
public class DdMpConfig {
//消息类型
private String msgType;
//webhook
private String webhook;
//微应用的ID
private String agentId;
//应用的唯一标识key
private String appKey;
//密钥
private String appSecret;
//文本
private String text;
//指定对象
private List<String> mobileList;
//是否推送所有人
private boolean isAtAll;
public String getMsgType() {
return msgType;
}
public void setMsgType(String msgType) {
this.msgType = msgType;
}
public String getWebhook() {
return webhook;
}
public void setWebhook(String webhook) {
this.webhook = webhook;
}
public String getAgentId() {
return agentId;
}
public void setAgentId(String agentId) {
this.agentId = agentId;
}
public String getAppKey() {
return appKey;
}
public void setAppKey(String appKey) {
this.appKey = appKey;
}
public String getAppSecret() {
return appSecret;
}
public void setAppSecret(String appSecret) {
this.appSecret = appSecret;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public List<String> getMobileList() {
return mobileList;
}
public void setMobileList(List<String> mobileList) {
this.mobileList = mobileList;
}
public boolean isAtAll() {
return isAtAll;
}
public void setAtAll(booleanatAll) {
isAtAll = atAll;
}
}
Utils
package com.yingwei.dingding.utils;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.CorpMessageCorpconversationAsyncsendRequest;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiUserGetByMobileRequest;
import com.dingtalk.api.response.CorpMessageCorpconversationAsyncsendResponse;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiUserGetByMobileResponse;
import com.taobao.api.ApiException;
import net.sf.json.JSONObject;
/**
* @Description:对接钉钉的工具类
*/
public class DingDingUtils {
/**
* 获得token
*
* @paramappKey应用的唯一标识key
* @paramappSecret企业应用的凭证密钥
*/
public static String getToken(String appKey, String appSecret) {
try {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
OapiGettokenRequest req = new OapiGettokenRequest();
req.setAppkey(appKey);
req.setAppsecret(appSecret);
req.setHttpMethod("GET");
OapiGettokenResponse rsp = client.execute(req);
return rsp.getAccessToken();
} catch (ApiExceptione) {
e.printStackTrace();
}
return null;
}
/**
* 发送消息
*
* @paramagentId微应用的ID
* @paramuserId接收者的用户userid列表
* @parammsgContent消息内容
*/
public static JSONObjectsendDDMessage(LongagentId, String userId, String msgContent, String token) {
DingTalkClient client = new DefaultDingTalkClient("https://eco.taobao.com/router/rest");
CorpMessageCorpconversationAsyncsendRequest req = new CorpMessageCorpconversationAsyncsendRequest();
req.setMsgtype("oa");//消息类型
req.setAgentId(agentId);
req.setUseridList(userId);
req.setToAllUser(false);//是否发送给企业全部用户
req.setMsgcontentString(msgContent);
try {
CorpMessageCorpconversationAsyncsendResponse rsp = client.execute(req, token);
JSONObject json = JSONObject.fromObject(rsp.getResult());
if (json != null) {
return json;
}
} catch (ApiExceptione) {
e.printStackTrace();
}
return null;
}
/**
* 获取钉钉用户id
*
* @return
* @paramphone
* @paramaccessToken
*/
public static String getUserId(String phone, String accessToken) {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get_by_mobile");
OapiUserGetByMobileRequest req = new OapiUserGetByMobileRequest();
req.setMobile(phone);
req.setHttpMethod("GET");
OapiUserGetByMobileResponse rsp = null;
try {
rsp = client.execute(req, accessToken);
} catch (ApiExceptione) {
e.printStackTrace();
}
System.out.println(rsp.getUserid());
return rsp.getUserid();
}
}
Pom.xml
<!--钉钉消息推送-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>1.0.1</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>