首先详细阅读钉钉官方文档。查阅你所需要的服务端 API。
专有钉钉门户https://openplatform-portal.dg-work.cn/portal/#/helpdoc?apiType=QUICK_START&docKey=3355321例如这里我对接的是浙政钉的消息通知接口,可查官方文档看具体位置,如图:
这是我的 demo,注意:发送通知有很多种形式,我这里是以卡片形式展示,其他可参考官方文档。
1.相关配置文件
@ToString
public class DingDingConfig {
// appKey
public static String appKey = "";
// 浙政钉域名 openplatform-pro.ding.zj.gov.cn
public static String domainName = "";
// appSecret
public static String appSecret = "";
}
2.工作通知测试接口,注意:这里入参很多,只需要填写必须的即可,不需要全部填写。如图:
3.我这里的 accountId 作为每个用户的唯一标识符,即发送通知的对象。 消息通知接口测通后即可进行开发使用。
/**
* 消息通知
*
* @param accountId
* @return
*/
public Object getMessage(String accountId) {
Long time = System.currentTimeMillis();
String msgID = String.valueOf(time);
ExecutableClient executableClient = ExecutableClient.getInstance();
executableClient.setDomainName(DingDingConfig.domainName);
executableClient.setProtocal("https");
executableClient.setAccessKey(DingDingConfig.otherAppKey);
executableClient.setSecretKey(DingDingConfig.otherAppSecret);
executableClient.init();
// executableClient保证单例
IntelligentPostClient intelligentPostClient = executableClient.newIntelligentPostClient("/message/workNotification");
OapiMessageWorkNotificationRequest oapiMessageWorkNotificationRequest = new OapiMessageWorkNotificationRequest();
// 接收者的部门id列表
// oapiMessageWorkNotificationRequest.setOrganizationCodes("字符串");
// 接收人用户ID
oapiMessageWorkNotificationRequest.setReceiverIds(accountId);
// 租户ID
oapiMessageWorkNotificationRequest.setTenantId("对应的租户id");
// 业务消息id,注意这里的msgID,每次通知都需要不一样的msgID参数,如果重复会提示本条内容已重复发送等相关内容,因此我这里设置为每次发送通知的时间
oapiMessageWorkNotificationRequest.setBizMsgId(msgID);
// 设置消息对象
String msg;
msg = "{\r\n"
+ " \"msgtype\": \"action_card\",\r\n"
+ " \"action_card\": {\r\n"
+ " \"title\": \"工作通知\",\r\n"
+ " \"markdown\": \"您有一条新的审批通知\",\r\n"
+ " \"single_title\": \"查看详情点击打开链接\",\r\n"
+ " \"single_url\": \"http://域名:端口/xxxx.html?ddtab=true\", \r\n"
+ " \"single_pc_url\": \"http://域名:端口/xxxx.html?ddtab=true\" \r\n"
+ " }\r\n"
+ "}";
oapiMessageWorkNotificationRequest.setMsg(msg);
OapiMessageWorkNotificationResponse apiResult = intelligentPostClient.post(oapiMessageWorkNotificationRequest);
System.out.println("apiResult" + JSON.toJSON(apiResult));
return apiResult;
}
4.测试结果:浙政钉消息通知会收到如图所示的卡片通知消息:
注意:这里我的查看详情可跳转链接http://域名:端口/xxxx.html?ddtab=true,后面添加参数ddtab=true即可在浙政钉内部跳转打开进第三方网址。
如图: