钉钉发送消息详细教程-- java
希望能对你有所帮助
首先是进行前期准备工作
流程
- 随便申请一个企业钉钉(这个不用详细说了吧) 不用企业认证 只是成为管理员 (公司的话就跟管理员要管理员权限才能登进去)
- 申请成功之后登陆开发者后台钉钉开发者后台
3.进入首页点击应用开发-企业内部开发
4.进入到应用开发 进行应用创建
5.进行接口查看接口文档
我们以工作通知为例子
看一下文档 我们之后可以线上调试
获取token
用户ID获取 登陆钉钉后台通讯录查看钉钉
下面是硬货 直接复制到项目改一下就行了
/ * 项目中的常量定义类 */public class Constant { / * 应用的AppKey,登录开发者后台,点击应用管理,进入应用详情可见 */ public static final String APPKEY = "XXX"; / * 应用的AppSecret,登录开发者后台,点击应用管理,进入应用详情可见 */ public static final String APPSECRET = "XXX"; / * 应用的agentdId,登录开发者后台可查看 */ public static final Long AGENTID = XXXL;}
下面的无需改动只需要传值就行了
public class URLConstant { / * 钉钉网关gettoken地址 */ public static final String URL_GET_TOKKEN = "https://oapi.dingtalk.com/gettoken"; / * 发送企业通知消息的接口url */ public static final String MESSAGE_ASYNCSEND = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2";}
import lombok.Data;@Datapublic class SendDingTalkBo { public final static SendDingTalkBo SEND_DING_TALK_BO = new SendDingTalkBo(); private SendDingTalkBo (){ } // 快递类型 private String expressDeliveryType; // 快递单号 private String expressDeliveryNumber; //快递节点时间 private String expressDeliveryTime; //客服名字 private String customerServiceName; // 品牌名称 private String supplierName; // 通知人Userid private String UserId; // 快递状态 private String expressDeliveryState; // 货物地址 private String acceptAddress;}
发送的类
import com.baomidou.mybatisplus.extension.exceptions.ApiException;import com.boailian.excelhander.config.Constant;import com.boailian.excelhander.config.URLConstant;import com.boailian.excelhander.dto.bo.SendDingTalkBo;import com.dingtalk.api.DefaultDingTalkClient;import com.dingtalk.api.DingTalkClient;import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;import org.springframework.stereotype.Component;import java.util.ArrayList;import java.util.List;@Componentpublic class SendDingTalkUtil { private static int A= 0; / * 将消息推送给对应客服 */ public static void sendDingTalk(SendDingTalkBo sendDingTalkBo) { try { DingTalkClient client = new DefaultDingTalkClient(URLConstant.MESSAGE_ASYNCSEND); OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request(); request.setAgentId(Constant.AGENTID); request.setUseridList(sendDingTalkBo.getUserId()); request.setToAllUser(false); OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg(); msg.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA()); OapiMessageCorpconversationAsyncsendV2Request.OA oa = msg.getOa(); oa.setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head()); oa.setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body()); oa.getBody().setContent(sendDingTalkBo.getCustomerServiceName() + " 您好 , 您所管理的品牌客户[" + sendDingTalkBo.getSupplierName() + "]有物流更新 请查收"); List<OapiMessageCorpconversationAsyncsendV2Request.Form> list = new ArrayList<OapiMessageCorpconversationAsyncsendV2Request.Form>(); OapiMessageCorpconversationAsyncsendV2Request.Form form = new OapiMessageCorpconversationAsyncsendV2Request.Form(); form = new OapiMessageCorpconversationAsyncsendV2Request.Form(); form.setKey("货物当前所在地: "); form.setValue(sendDingTalkBo.getAcceptAddress()); list.add(form); form.setKey("快递状态: "); form.setValue(sendDingTalkBo.getExpressDeliveryState()); list.add(form); form = new OapiMessageCorpconversationAsyncsendV2Request.Form(); form.setKey("快递类型: "); form.setValue(sendDingTalkBo.getExpressDeliveryType()); list.add(form); form = new OapiMessageCorpconversationAsyncsendV2Request.Form(); form.setKey("快递单号: "); form.setValue(sendDingTalkBo.getExpressDeliveryNumber()); list.add(form); form = new OapiMessageCorpconversationAsyncsendV2Request.Form(); form.setKey("更新时间: "); form.setValue(sendDingTalkBo.getExpressDeliveryTime()); list.add(form); OapiMessageCorpconversationAsyncsendV2Request.Rich rich = new OapiMessageCorpconversationAsyncsendV2Request.Rich(); rich.setUnit("请悉知 系统通知无需回复"); oa.getBody().setRich(rich); oa.getBody().setForm(list); msg.setMsgtype("oa"); request.setMsg(msg); OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, AccessTokenUtil.getToken()); } catch (ApiException | com.taobao.api.ApiException e) { e.printStackTrace(); } }
所需jar 外部引用需要下载注意 免费的jar
<dependency> <groupId>com.taobao.top</groupId> <artifactId>top-api-sdk-dev</artifactId> <version>dingtalk-SNAPSHOT</version> <scope>system</scope> <systemPath>${pom.basedir}/lib/taobao-sdk-java-auto_1479188381469-20190227.jar</systemPath> </dependency> <dependency> <groupId>com.taobao.top</groupId> <artifactId>lippi-oapi-encrpt</artifactId> <version>dingtalk-SNAPSHOT</version> <scope>system</scope> <systemPath>${pom.basedir}/lib/lippi-oapi-encrpt.jar</systemPath> </dependency>
下载完成后放在
可能会有遗漏 有问题留言解决。希望能有所帮助