钉钉群消息推送

1. 添加钉钉群机器人

  • PC端登录(当前版本手机端无法进行推送关键词设置),群设置--> 机器人 --> webhook
  • 进行安全设置
  • 复制webhook对应的url

2. 群消息推送

钉钉群消息支持纯文本和markdown类型

2.1 调用示例源码

import com.alibaba.fastjson.JSONObject;
import com.hz.utils.HttpUtils;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * 钉钉消息推送
 *
 * @author pp_lan
 */
public class MessagePusher {

    /**
     * 推送url
     */
    public static final String Webhook = "https://oapi.dingtalk.com/robot******thisisyourowngroupinfo";

    @Test
    public void testSimpleMessage() {

        JSONObject text = new JSONObject();
        text.put("content", "【预警】SimpleMessage");

        JSONObject params = new JSONObject();
        params.put("msgtype", "text");
        params.put("text", text);

        HttpUtils.sendPost(Webhook, params.toJSONString(), null);
    }

    @Test
    public void testMarkdownMessage() {

        JSONObject markdown = new JSONObject();
        markdown.put("title", "【预警】MarkdownMessage");
        markdown.put("text", "* collections 类集\n* classloader <font color=\"red\">类加载器</font>");

        JSONObject params = new JSONObject();
        params.put("msgtype", "markdown");
        params.put("markdown", markdown);

        HttpUtils.sendPost(Webhook, params.toJSONString(), null);
    }
}

2.2 调用说明

消息中需要包含群机器人设置的安全词(此处为预警),否则会被过滤

2.3 成功示例

猜你喜欢

转载自blog.csdn.net/pp_lan/article/details/132063890