钉钉群机器人发送消息

1 项目添加淘宝sdk依赖

<dependency>
    <groupId>taobao-sdk-java</groupId>
    <artifactId>taobao-sdk-java-auto</artifactId>
    <version>1.0</version>
</dependency>

若添加依赖有问题,可参考Maven使用第三方Jar文件这篇文章里面方法一进行添加:

<dependency>
    <groupId>taobao-sdk-java</groupId>
    <artifactId>taobao-sdk-java-auto</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/taobao-sdk-java-auto-1.0.jar</systemPath>
 </dependency>

2 发送Markdown格式消息

public class MarkdownTest {

public void sendMessage() throws Exception {

             String WEBHOOK_TOKEN = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxx";
             String DING = "";
             String MARK_DOWN_ATX2 = "## ";
             String MARK_DOWN_ATX3 = "### ";
             String MARK_DOWN_ATX4 = "#### ";
             String MARK_DOWN_COLON = " : ";
             String MARK_DOWN_LINEFEED = " \n ";
             String MARK_DOWN_IS_RED = "<font color=#FF33FF size=8 face=\"黑体\">%s</font>";
             String MARK_DOWN_EMAIL1 = "> ";

       
            //通知钉钉群
             StringBuilder resultText = new StringBuilder();
             resultText.append(MARK_DOWN_ATX2).append("项目名").append(MARK_DOWN_COLON).append(projectappid).append(MARK_DOWN_LINEFEED);
             resultText.append(MARK_DOWN_ATX4).append("单元测试覆盖率").append(MARK_DOWN_COLON).append(String.format(MARK_DOWN_IS_RED, total_covered_instructions)).append(MARK_DOWN_LINEFEED);               
             String DING_URL = WEBHOOK_TOKEN;
             DingTalkClient client = new DefaultDingTalkClient(DING_URL);
             OapiRobotSendRequest request = new OapiRobotSendRequest();
             request.setMsgtype("markdown");
             OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
             markdown.setTitle("jacoco代码扫描");
             markdown.setText(resultText.toString());
             request.setMarkdown(markdown);
             try {
                 OapiRobotSendResponse response = client.execute(request);
                 if (response.isSuccess()) {
                     LOGGER.info(String.format("### 钉钉通知已发送成功, msg:%s", msgText));
                  //System.out.println("send message ok");
                 } else {
                     LOGGER.error(response.getErrmsg());
                 }
             } catch (com.taobao.api.ApiException e) {
                 String errorMsg = "### 发送钉钉通知异常";
                 LOGGER.error(errorMsg, e);
                 throw new ApiException(errorMsg);
             }

         }                

         }catch(NumberFormatException e) {
             e.printStackTrace();
         }

}

}

3 官方参考文档

自定义机器人

posted @ 2019-08-11 02:06  ycyzharry  阅读(6897)  评论(1编辑  收藏  举报