钉钉机器人群消息

1、消息实体类

/**
 * 消息实体类
 * @author yimoyuyang
 */
public class TextMessage {
    private String text;
    private List<String> atMobiles;
    private boolean isAtAll;
 
    public TextMessage(String text) {
        this.text = text;
    }
 
    public String getText() {
        return text;
    }
 
    public void setText(String text) {
        this.text = text;
    }
 
    public List<String> getAtMobiles() {
        return atMobiles;
    }
 
    public void setAtMobiles(List<String> atMobiles) {
        this.atMobiles = atMobiles;
    }
 
    public boolean isAtAll() {
        return isAtAll;
    }
 
    public void setIsAtAll(boolean isAtAll) {
        this.isAtAll = isAtAll;
    }
 
    public String toJsonString() {
        Map<String, Object> items = new HashMap<>();
        items.put("msgtype", "text");
 
        Map<String, String> textContent = new HashMap<>();
        if (StringUtils.isBlank(text)) {
            throw new IllegalArgumentException("text should not be blank");
        }
        textContent.put("content", text);
        items.put("text", textContent);
 
        Map<String, Object> atItems = new HashMap<>();
        if (atMobiles != null && !atMobiles.isEmpty()) {
            atItems.put("atMobiles", atMobiles);
        }
        if (isAtAll) {
            atItems.put("isAtAll", isAtAll);
        }
        items.put("at", atItems);
 
        return JSON.toJSONString(items);
    }
}

2、发送消息结果实体类

/**
 * 发送消息结果实体类
 * @author yimoyuyang
 */
public class SendResult {
	private boolean isSuccess;
    private Integer errorCode;
    private String errorMsg;
 
    public boolean isSuccess() {
        return isSuccess;
    }
 
    public void setIsSuccess(boolean isSuccess) {
        this.isSuccess = isSuccess;
    }
 
    public Integer getErrorCode() {
        return errorCode;
    }
 
    public void setErrorCode(Integer errorCode) {
        this.errorCode = errorCode;
    }
 
    public String getErrorMsg() {
        return errorMsg;
    }
 
    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }
 
    @Override
    public String toString() {
        Map<String, Object> items = new HashMap<String, Object>();
        items.put("errorCode", errorCode);
        items.put("errorMsg", errorMsg);
        items.put("isSuccess", isSuccess);
        return JSON.toJSONString(items);
    }
}

3、消息通用类

/**
 * 消息通用类
 * @author yimoyuyang
 */
public class RobotClient {
	HttpClient httpclient = HttpClients.createDefault();
    public SendResult send(String webhook, TextMessage message) throws IOException {
        HttpPost httppost = new HttpPost(webhook);
        httppost.addHeader("Content-Type", "application/json; charset=utf-8");
        StringEntity se = new StringEntity(message.toJsonString(), "utf-8");
        httppost.setEntity(se);
        SendResult sendResult = new SendResult();
        HttpResponse response = httpclient.execute(httppost);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            String result = EntityUtils.toString(response.getEntity());
            JSONObject obj = JSONObject.parseObject(result);
            Integer errcode = obj.getInteger("errcode");
            sendResult.setErrorCode(errcode);
            sendResult.setErrorMsg(obj.getString("errmsg"));
            sendResult.setIsSuccess(errcode.equals(0));
        }
        return sendResult;
    }
}

4、发送消息

public class SendDingTextMessage {
	//机器人消息token
    public static String WEBHOOK_TOKEN = "***********";
 
    private static RobotClient robot = new RobotClient();
 
    /**
     * 发送普通文本消息
     *
     * @param message
     * @return
     */
    public static SendResult send(String message) {
        TextMessage textMessage = new TextMessage(message);
        SendResult sendResult = null;
        try {
            sendResult = robot.send(WEBHOOK_TOKEN, textMessage);
        } catch (Exception e) {
        	e.printStackTrace();
        }
        return sendResult;
    }
 
    /**
     * 发送文本消息 可以@部分人
     *
     * @param message
     * @param atMobiles 要@人的电话号码 ArrayList<String>
     * @return
     */
    public static SendResult sendWithAt(String message, ArrayList<String> atMobiles) {
        TextMessage textMessage = new TextMessage(message);
        SendResult sendResult = null;
        textMessage.setAtMobiles(atMobiles);
        try {
            sendResult = robot.send(WEBHOOK_TOKEN, textMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sendResult;
    }
 
    /**
     * 发送文本消息 并@所有人
     *
     * @param message
     * @return
     */
    public static SendResult sendWithAtAll(String message) {
        TextMessage textMessage = new TextMessage(message);
        SendResult sendResult = null;
        textMessage.setIsAtAll(true);
        try {
            sendResult = robot.send(WEBHOOK_TOKEN, textMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sendResult;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值