zoukankan      html  css  js  c++  java
  • 通知神器——java调用钉钉群自定义机器人

    创建群自定义机器人

    在指定钉钉群(或者随便拉两个人建个群,然后把别人T出去)的群设置里选择 群机器人 -> 自定义,如图:
    file

    然后,添加机器人,设置名字,添加成功时如下图:
    file

    其中webhook非常重要,下文详述。点击设置说明可以看相关使用文档,文档链接见本文末尾。

    使用HTTP POST请求发送消息

    直接向上文所述的webhook链接发送POST请求已达到自定义机器人向群内发消息的功能

    按照文档中所属的参数格式直接向链接发送请求即可,我使用Postman发送请求如下:
    file

    效果如下:
    file

    发送其他格式(如:链接消息、markdown、ActionCard)的消息效果如下:
    file
    file
    file
    file
    file

    确定你需要哪种消息了吗?如果需要使用,通过这种方式来实现,组织参数还是挺麻烦的,不过我们可以使用sdk来简化操作

    使用sdk调用自定义机器人

    钉钉服务端API SDK下载

     public static void main(String[] args) throws ApiException {
        final String serverUrl =
            "https://oapi.dingtalk.com/robot/send?access_token=*******************************************************";
    
        DingTalkClient client = new DefaultDingTalkClient(serverUrl);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
    
        At at = new At();
        at.setIsAtAll("true");
        request.setAt(at);
    		
       // 若上一步isAtAll没有设置true,则根据此处设置的手机号来@指定人
        List<String> mobiles = new ArrayList<>();
        mobiles.add("166********");
        at.setAtMobiles(mobiles);
    
        // 以下是设置各种消息格式的方法
        sentText(request);
        //    sendLink(request);
        //    sendMarkdown(request);
        //    sendActionCard(request);
        //    sendFeedCard(request);
    
        OapiRobotSendResponse response = client.execute(request);
        System.out.println(response.getErrmsg());
      }
    

    text文本消息

      public static void sentText(OapiRobotSendRequest request) {
        Text text = new Text();
        text.setContent("sdk demo");
    
        request.setMsgtype("text");
        request.setText(text);
      }
    

    link链接消息

      public static void sendLink(OapiRobotSendRequest request) {
        Link link = new Link();
        link.setTitle("好消息!好消息!");
        link.setText("本群与百度成功达成合作关系,今后大家有什么不懂的可以直接百度搜索,不用再群里提问浪费时间啦!");
        link.setMessageUrl("https://www.baidu.com");
        link.setPicUrl("http://www.baidu.com/img/bd_logo1.png");
    
        request.setMsgtype("link");
        request.setLink(link);
      }
    

    markdown消息

      public static void sendMarkdown(OapiRobotSendRequest request) {
        Markdown markdown = new Markdown();
        markdown.setTitle("好消息!好消息!");
        markdown.setText(
            "#### 杭州天气 @156xxxx8827\n> 9度,西北风1级,空气良89,相对温度73%\n\n"
                + "> ![screenshot](https://img2018.cnblogs.com/blog/1775867/201908/1775867-20190823214506246-1471056011.png)\n"
                + "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n");
        
        request.setMsgtype("markdown");
        request.setMarkdown(markdown);
      }
    

    ActionCard消息

      public static void sendActionCard(OapiRobotSendRequest request) {
        Actioncard actioncard = new Actioncard();
        actioncard.setTitle("乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身");
        actioncard.setText(
            "![screenshot](@lADOpwk3K80C0M0FoA) \n"
                + " ### 乔布斯 20 年前想打造的苹果咖啡厅  Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划");
        actioncard.setHideAvatar("0");
        actioncard.setBtnOrientation("1");
        //    actioncard.setSingleTitle("阅读全文");
        //    actioncard.setSingleURL("https://www.baidu.com/");
        List<Btns> btns = new ArrayList<>();
        Btns btn0 = new Btns();
        btn0.setTitle("内容不错");
        btn0.setActionURL("https://www.qq.com/");
        btns.add(btn0);
    
        Btns btn1 = new Btns();
        btn1.setTitle("不感兴趣");
        btn1.setActionURL("https://www.baidu.com/");
        btns.add(btn1);
    
        actioncard.setBtns(btns);
        request.setMsgtype("actionCard");
        request.setActionCard(actioncard);
      }
    

    FeedCard消息

    类似公众号头条、次条消息

     public static void sendFeedCard(OapiRobotSendRequest request) {
        Feedcard feedcard = new Feedcard();
        List<Links> linksList = new ArrayList<>();
    
        Links links0 = new Links();
        links0.setTitle("时代的火车向前开1");
        links0.setMessageURL(
            "https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI");
        links0.setPicURL("https://www.dingtalk.com/");
        linksList.add(links0);
    
        Links links1 = new Links();
        links1.setTitle("时代的火车向前开2");
        links1.setMessageURL(
            "https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI");
        links1.setPicURL("https://www.dingtalk.com/");
        linksList.add(links1);
    
        feedcard.setLinks(linksList);
    
        request.setMsgtype("feedCard");
        request.setFeedCard(feedcard);
      }
    

    参考资源

    钉钉自定义机器人

    钉钉开放平台附录

    钉钉服务端API SDK下载

  • 相关阅读:
    Python正则表达式------进阶
    基本数据类型(字符串_数字_列表_元祖_字典_集合)
    python目录
    python------异步IO数据库队列缓存
    saltstack技术入门与实践
    扒一扒JavaScript 预解释
    微信二维码防伪
    web前端页面性能优化小结
    js 中 continue 与 break 熟练使用
    倒计时原生js
  • 原文地址:https://www.cnblogs.com/lw5946/p/11402822.html
Copyright © 2011-2022 走看看