一、工具和环境:eclipse+springBoot+Maven+51手机模拟器
二、阅读官方开发文档
[attachment=126088]
三、请参考帖子:
【创联云】钉钉免登demo
https://bbs.aliyun.com/read/317081.html?spm=0.0.0.0.xIkNnr 获取access_token 和userId然后准备访问URL的send()方法
Https请求方式: POST https://oapi.dingtalk.com/message/send?access_token=ACCESS_TOKEN
[font='Microsoft Yahei', 微软雅黑, Arial]
public static JSONObject send(String accessToken,LightAppMessageDelivery lightAppMessageDelivery ){
JSONObject json = new JSONObject();
json.put("msgtype", lightAppMessageDelivery.msgType);
json.put("agentid", lightAppMessageDelivery.agentid);
json.put("touser", lightAppMessageDelivery.touser);
json.put(lightAppMessageDelivery.msgType, lightAppMessageDelivery.messages);
String url = Env.OAPI_HOST + "/message/send?access_token=" + accessToken ;
JSONObject reponseJson = null;
try {
reponseJson = HttpHelper.httpPost(url,json);
// {"invalidparty":"","invaliduser":"","messageId":"b60c370c73ae3ea9928fe3376024273b"}
} catch (OApiException e) {
e.printStackTrace();
}
return reponseJson;
}
四、组织消息内容,本贴例举了三种类型消息:文本,链接,OA
public static void main(String[] args){
ServiceHelper serviceHelper = new ServiceHelper();
String toUsers = Env.USER_ID;
String toParties = "";
String agentId = Env.AGENT_ID;
LightAppMessageDelivery lightAppMessageDelivery = new LightAppMessageDelivery(toUsers, toParties, agentId);
try {
// 创建文本消息
MessageBody.TextBody textBody = new MessageBody.TextBody();
textBody.setContent("需求文档提交了吗?");
lightAppMessageDelivery.withMessage(MessageType.TEXT, textBody);
serviceHelper.send(AuthHelper.getAccessToken(),lightAppMessageDelivery);
// 创建链接消息
MessageBody.LinkBody linkBody = new MessageBody.LinkBody();
linkBody.setMessageUrl("http://www.baidu.com");
linkBody.setPicUrl("@lALOACZwe2Rkd");
linkBody.setTitle("Link Message");
linkBody.setText("点击链接跳到百度");
lightAppMessageDelivery.withMessage(MessageType.LINK, linkBody);
serviceHelper.send(AuthHelper.getAccessToken(),lightAppMessageDelivery);
// 创建oa消息
MessageBody.OABody oaBody = new MessageBody.OABody();
oaBody.setMessage_url("http://www.baidu.com");
Head head = new Head();
head.setBgcolor("FFBBBBBB");
oaBody.setHead(head);
Body body = new Body();
body.setAuthor("总经理室");
body.setContent("需求分析与方案定制");
body.setFile_count("1");
body.setImage("@image");
body.setTitle("创联云钉钉二次开发会议通知!!");
List<Form> formList = new ArrayList<Form>();
Form form = new Form();
form.setKey("市场部:");
form.setValue("张三");
Form form1 = new Form();
form1.setKey("研发部:");
form1.setValue("李四");
formList.add(form);
formList.add(form1);
body.setForm(formList);
oaBody.setBody(body);
lightAppMessageDelivery.withMessage(MessageType.OA, oaBody);
MessageHelper.send(AuthHelper.getAccessToken(), lightAppMessageDelivery);
} catch (Exception e) {
e.printStackTrace();
}
}
五:
演示图:
[attachment=126089][attachment=126090]
[font='Microsoft Yahei', 微软雅黑, Arial]
[font='Microsoft Yahei', 微软雅黑, Arial]
[font='Microsoft Yahei', 微软雅黑, Arial]
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。