Java实现钉钉群发送消息

Java实现钉钉群发送消息

1、智能群助手→添加机器人→自定义。添加后,保存其中的webhook地址和加签密钥

具体参考钉钉开放平台——开放文档

https://open.dingtalk.com/document/group/become-a-dingtalk-developer

2、代码实现

package com.xd.utils;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.xd.visual.entity.po.Event;
import com.xd.visual.utils.TimeUtils;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;

/**
 * 发送消息到钉钉群
 * https://open.dingtalk.com/document/robots/custom-robot-access
 * @author yyb
 * @date 2022年09月20日 16:21
 */
@Component
public class Dingding {
    private static String webhook;

    @Value("${dingding.webhook}")
    public void setWebhook(String webhook) {
        Dingding.webhook = webhook;
    }
    
    public static String getWebhook() {
        return webhook;
    }

    private static String secret;

    @Value("${dingding.secret}")
    public void setSecret(String secret) {
        Dingding.secret = secret;
    }

    public static String getSecret() {
        return secret;
    }

    /**
     * 钉钉群发送消息
     *
     * @param event
     * @throws Exception
     */
    public static void messageGroup(Event event) throws Exception {
        //签名认证
        Long timestamp = System.currentTimeMillis();
        String stringToSign = timestamp + "\n" + Dingding.getSecret();
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(Dingding.getSecret().getBytes("UTF-8"), "HmacSHA256"));
        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
        String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
        DingTalkClient client = new DefaultDingTalkClient(Dingding.getWebhook() + "&timestamp=" + timestamp + "&sign=" + sign);
        OapiRobotSendRequest request = new OapiRobotSendRequest();

        //md格式消息
        request.setMsgtype("markdown");
        OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
        markdown.setTitle("事件上报");
        markdown.setText("## 百花园事件上报\n" +
                "> **【事件类型】**  " + event.getAlgoname() + "\n\n" +
                "> **【点位名称】**  " + event.getPlace() + "\n\n" +
                "> **【推送时间】**  " + TimeUtils.addDateMinut(String.valueOf(event.getCreatetime()),8) + " \n" +
                "> ![screenshot](ip:port/" + event.getPicpath() + ")\n"
        );
        request.setMarkdown(markdown);
        client.execute(request);
    }

}

webhook和secret从配置文件中取,

注意:@value不能直接注入值给静态属性,需使用setter 方法注入静态变量,同时还需要使用@Component注解

 @Value("${dingding.webhook}")
 private static String webhook;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值