钉钉群机器人发送消息

1 项目添加淘宝sdk依赖


    taobao-sdk-java
    taobao-sdk-java-auto
    1.0

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


    taobao-sdk-java
    taobao-sdk-java-auto
    1.0
    system
    ${basedir}/lib/taobao-sdk-java-auto-1.0.jar
 

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 = "%s";
             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 官方参考文档

自定义机器人

转载于:https://www.cnblogs.com/ycyzharry/p/11333729.html

你可能感兴趣的:(markdown,java,php)