钉钉机器人接入定时器(钉钉API+XXL-JOB)

钉钉机器人接入定时器(钉钉API+XXL-JOB)

首先需要创建钉钉内部群

 

在群设置中找到机器人选项
选择“自定义”机器人

通过Webhook接入自定义服务

创建完成后会生成一个send URL和一个加签码

下面就是干货 代码部分了

DingDingUtil.sendMessageByText(webhook, sign, text, mobileList, isAtAll);
// 其中webhook为机器人生成的 sendUrl
// sign为加签码
// text为机器人播报文本
// mobileList为List<String>格式的手机号
// isAtAll 为是否@全体人员 boolean值

Util如下


/**
 * 钉钉机器人工具类
 * @author zhangjiaxuan
 */
@Slf4j
public class DingDingUtil {
    /**
     ** 发送普通文本消息
     *
     * @param content    文本消息
     * @param mobileList 指定@ 联系人
     * @param isAtAll    是否@ 全部联系人
     * @return OapiRobotSendResponse
     */
    public static OapiRobotSendResponse sendMessageByText(String ACCESS_TOKEN, String SECRET, String content, List<String> mobileList, boolean isAtAll) {
        String sign = null;
        try {
            Long timestamp = System.currentTimeMillis();
            String stringToSign = timestamp + "\n" + SECRET;
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(SECRET.getBytes("UTF-8"), "HmacSHA256"));
            byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
            sign = "&timestamp=" + timestamp + "&sign=" + URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        DingTalkClient client = null;
        client = new DefaultDingTalkClient(ACCESS_TOKEN + sign);
        if (StringUtils.isEmpty(content)) {
            return null;
        }

        //参数	参数类型	必须	说明
        //msgtype	String	是	消息类型,此时固定为:text
        //content	String	是	消息内容
        //atMobiles	Array	否	被@人的手机号(在content里添加@人的手机号)
        //isAtAll	bool	否	@所有人时:true,否则为:false
        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
        text.setContent(content);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        if (!CollectionUtils.isEmpty(mobileList)) {
            // 发送消息并@ 以下手机号联系人
            OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
            at.setAtMobiles(mobileList);
            at.setIsAtAll(isAtAll);
            request.setAt(at);
        }
        request.setMsgtype("text");
        request.setText(text);

        OapiRobotSendResponse response = new OapiRobotSendResponse();
        try {
            response = client.execute(request);
            System.out.println("【DingTalkUtils】发送普通文本消息 响应参数:" + JSON.toJSONString(response));
        } catch (ApiException e) {
            log.error("[发送普通文本消息]: 发送消息失败, 异常捕获{}", e.getMessage());
        }
        return response;
    }
}

简单说一下XXL-JOB

java中的引入方式为

XXL面板上

先创建执行器

需要和yml文件中的job地址/token/app-name 全部对应

然后打开 - 任务管理 - 后选择对应的执行器创建任务

创建完成后可以选择执行一次 或者配置好Corn或者秒数后点击开启 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GG-0408

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值