##给钉钉用户发送消息,首先在钉钉开放平台创建一个微应用
最近研究了一下系统向钉钉发送工作消息通知的的内容最后实现的结果就是这样
###先看代码在一点一点解释
//这个就是一个基本模型大致能实现功能,后期可以根据自己的项目 需求来完善相关代码,
我后台用的是jeecgboot框架@AutoLog@ApiOperation是测试接口用的可以忽略
package org.jeecg.modules.demo.test.controller;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
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.OapiMessageCorpconversationAsyncsendV2Request.Body;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request.Head;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request.Msg;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request.OA;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request.Text;
import com.dingtalk.api.request.OapiUserGetuserinfoRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiUserGetuserinfoResponse;
import com.taobao.api.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
/**
* 钉钉发送消息接口
* @author
*
*/
@Slf4j
@Api(tags="钉钉推送消息")
@RestController
@RequestMapping("/DinDinController")
public class DinDinController {
private static Long AGENTLD = 1154xxxxxxL;//微应用ID
private static String APPSECRET ="M-EtLzxxxxx-QWn1xxxxx0zxxxxxxxxzxxxxxxxxxxxxxx";//企业应用凭证密匙
private static String CORPID = "ding4b5686e63xxxxxxxxxxxxxxxxx";//企业ID
private static String APPKEY = "dingdwtxxxxxxxxxxxx";//企业凭证唯一标识key
//发送消息
@AutoLog(value = "钉钉推送消息")
@ApiOperation(value="钉钉推送消息", notes="钉钉推送消息")
@RequestMapping("/Message") //requestAuthCode 免密登陆授权码前端获取传进来
public void DDMessage(@RequestParam String msgcontent,@RequestParam String requestAuthCode) throws ApiException {
//获取token appkey+appsecret通过下面接口就能获取到token
DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
OapiGettokenRequest request = new OapiGettokenRequest();
//Appkey
request.setAppkey(APPKEY);
//Appsecret
request.setAppsecret(APPSECRET);
/*请求方式*/
request.setHttpMethod("GET");
OapiGettokenResponse response = client.execute(request);
//获取到token
String AccessToken = response.getAccessToken();
//获取接收者ID接口
DingTalkClient clientUserId = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
OapiUserGetuserinfoRequest reqUID = new OapiUserGetuserinfoRequest();
reqUID.setCode(requestAuthCode);
reqUID.setHttpMethod("GET");
OapiUserGetuserinfoResponse rspUID = clientUserId.execute(reqUID, AccessToken);
//获取到接收者的ID
String userId = rspUID.getUserid();
//发送消息接口
DingTalkClient client1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
req.setAgentId(AGENTLD);//微应用ID
req.setToAllUser(false);//是否发给所有人 false否 ,否的情况下要获取接受者id 可以是多个和接收者部门id
req.setUseridList(userId);//接收者ID
//---------消息体------------
Msg obj1 = new Msg();
obj1.setMsgtype("oa");//消息类型
Text obj2 = new Text();
obj2.setContent("测试");//文本消息
obj1.setText(obj2);
OA obj3 = new OA();
Body obj4 = new Body();
obj4.setContent(msgcontent);//发送消息内容
obj3.setBody(obj4);
Head obj5 = new Head();//消息头
obj5.setText("测试11");//消息头内容
obj3.setHead(obj5);
obj1.setOa(obj3);
req.setMsg(obj1);
//发送消息
OapiMessageCorpconversationAsyncsendV2Response rsp = client1.execute(req, AccessToken);
System.out.println(rsp.getBody());
}
}
###首先第一部你要在钉钉开放平台创建一个H5微应用
###生成的三个参数要保留好 很重要后期会用到
###在开发管理
###这个权限管理里边添加上你要用到的权限,版本管理里面可以设置你要进行测试的相关人员
##这个微应用就创建好了,向钉钉发送消息 首先要获取到access_token这个是授权凭证
//前半部分就是生成access_token的代码
//获取token appkey+appsecret通过下面接口就能获取到token
DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
OapiGettokenRequest request = new OapiGettokenRequest();
//Appkey
request.setAppkey(APPKEY);
//Appsecret
request.setAppsecret(APPSECRET);
/*请求方式*/
request.setHttpMethod("GET");
OapiGettokenResponse response = client.execute(request);
//获取到token
String AccessToken = response.getAccessToken();
##获取到token之后,你要是发给钉钉指定用户的话 就必须先获取到 钉钉用户的userid 通过免登录获取用户信息
获取用户userid需要两个参数Code 和 access_token 前边我们已经获取到token了直接拿来用就可以
获取code需要先获取免等授权码 根据这个链接可以先获取一个临时的测试用: https://open-dev.dingtalk.com/apiExplorer?spm=ding_open_doc.document.0.0.78494981SP2pJv#/jsapi?api=runtime.permission.requestAuthCode.
//这里的requestAuthCode是从前台获取到的,就是免登授权码
//获取接收者ID接口
DingTalkClient clientUserId = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
OapiUserGetuserinfoRequest reqUID = new OapiUserGetuserinfoRequest();
reqUID.setCode(requestAuthCode);
reqUID.setHttpMethod("GET");
OapiUserGetuserinfoResponse rspUID = clientUserId.execute(reqUID, AccessToken);
//获取到接收者的ID
String userId = rspUID.getUserid();;
##到这我们需要的基本参数都取到了 这个钉钉测试接口生成的代码可以拿来直接用很方便https://open-dev.dingtalk.com/apiExplorer#/?devType=org&api=dingtalk.oapi.message.corpconversation.asyncsend_v2述
##也就是我最后这部分代码
DingTalkClient clientUserId = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
OapiUserGetuserinfoRequest reqUID = new OapiUserGetuserinfoRequest();
reqUID.setCode(requestAuthCode);
reqUID.setHttpMethod("GET");
OapiUserGetuserinfoResponse rspUID = clientUserId.execute(reqUID, AccessToken);
//获取到接收者的ID
String userId = rspUID.getUserid();
//发送消息接口
DingTalkClient client1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
req.setAgentId(AGENTLD);//微应用ID
req.setToAllUser(false);//是否发给所有人 false否 ,否的情况下要获取接受者id 可以是多个和接收者部门id
req.setUseridList(userId);//接收者ID
//---------消息体------------
Msg obj1 = new Msg();
obj1.setMsgtype("oa");//消息类型
Text obj2 = new Text();
obj2.setContent("测试");//文本消息
obj1.setText(obj2);
OA obj3 = new OA();
Body obj4 = new Body();
obj4.setContent(msgcontent);//发送消息内容
obj3.setBody(obj4);
Head obj5 = new Head();//消息头
obj5.setText("测试11");//消息头内容
obj3.setHead(obj5);
obj1.setOa(obj3);
req.setMsg(obj1);
//发送消息
OapiMessageCorpconversationAsyncsendV2Response rsp = client1.execute(req, AccessToken);
System.out.println(rsp.getBody());
基本就ok了,可以根据自己的需求完善相关数据,初次了解钉钉接口有问题还请各位大佬指点一二,谢谢!