开发者社区> 问答> 正文

【创联云】钉钉企业消息demo

一、工具和环境: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]

展开
收起
steven_1205 2017-05-29 12:12:23 3084 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
钉钉小程序——为工作方式插上翅膀 立即下载
钉钉客户端自动化的应用 立即下载
使命必达 —— 钉钉企业级 消息服务的机遇与挑战 立即下载