自学内容网 自学内容网

钉钉消息推送()

记录一下java实现消息推送

1. 首先添加依赖

 <dependencies>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>alibaba-dingtalk-service-sdk</artifactId>
            <version>2.0.0</version>
        </dependency>

        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.11</version>
        </dependency>
</dependencies>

2. 配置钉钉推送application.yml文件

dingding:
  agentId: 25702
  appKey: dingwsjy7bu
  appSecret: eFROKkCe5JQvYOuru0ZXKzSyaBL9RAI-o6KTilfmVn3STg
  messageServerUrl: https://oapi.dingtalk.com/topapi/message
  tokenServerUrl: https://oapi.dingtalk.com/
  getbymobileServerUrl: https://oapi.dingtalk.com/topapi/v2
  secret: SECc10440b6571aa687b056488d93eb418443ac8d89615489d9a0b
  accessToken: 11da3106cd3ebc2e117e0d2e4cd4e91801eb4a481ec6bb2e

创建一个读取这些配置的config类型,如下

/**
 * 读取钉钉相关配置
 *
 * @author lkx
 */
@Component
@ConfigurationProperties(prefix = "dingding")
@Data
public class DingConfig
{

    private String agentId;

    private String appKey;

    private String appSecret;

    private String messageServerUrl;

    private String tokenServerUrl;

    private String getbymobileServerUrl;

   private String secret;

   private String accessToken;
}

3. 创建一个单独的service和实现类

public interface DingMessageService {


    /**
     * 发送钉钉群
     * @param markdown
     */
    void sendCrowdMessage(OapiRobotSendRequest.Markdown markdown);
}

实现类

@Slf4j
@Service
public class DingMessageServiceImpl implements DingMessageService {

    /**
     * DingDing配置
     */
    @Autowired
    private DingConfig dingConfig;



    @Async("threadPool")
    @Override
    public void sendCrowdMessage(OapiRobotSendRequest.Markdown markdown) {

        try {
            Long timestamp = System.currentTimeMillis();
            String stringToSign = timestamp + "\n" + dingConfig.getSecret();
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(dingConfig.getSecret().getBytes("UTF-8"), "HmacSHA256"));
            byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
            String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");

            String serverUrl = "https://oapi.dingtalk.com/robot/send?access_token=" + dingConfig.getAccessToken() + "&timestamp=" + timestamp + "&sign=" + sign;

            DingTalkClient client = new DefaultDingTalkClient(serverUrl);
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("text");
            OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
            text.setContent("告警");
            request.setText(text);
            OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
            at.setIsAtAll(true);
            request.setAt(at);
            request.setMsgtype("link");
            OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
            link.setMessageUrl("https://www.dingtalk.com/");
            link.setPicUrl("");
            request.setLink(link);
            request.setMsgtype("markdown");
            request.setMarkdown(markdown);
            OapiRobotSendResponse response = client.execute(request);
        } catch (Exception e) {
            log.error("钉钉发送群消息异常:" + e.getMessage());
        }
    }


}

4. 需要直接注入调用即可

                //推送钉钉
                OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
                markdown.setTitle("物料车电量告警");
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder
                        .append("### **警告** \n\n")
                        .append("物料车电量告警\n\n")
                        .append("当前有"+size+"辆物料车电池电量低于"+batteryLevel+"%"+",请及时充电\n\n")
                        .append("告警时间:")
                        .append(DateUtils.getTime());
                markdown.setText(stringBuilder.toString());
                dingMessageService.sendCrowdMessage(markdown);

附:yml文件中 secret 和 accessToken 获取方法

首先拉人组个群,点击群设置,添加小机器人

然后选择自定义

选择加签,里面的值就是 secret

然后点击完成,出现下图,access_token 后面的就是 accessToken 的值


原文地址:https://blog.csdn.net/DreamStat/article/details/145163969

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!