在springboot启动时给钉钉群发通知

摘要:
1.因为springboot启动后会加载所用的配置文件,所以我们可以在main方法下写DingTalk的bean来注入DingTalk配置。

1.因为springboot启动后会加载所用的配置文件,所以我们可以在main方法下写DingTalk的bean来注入DingTalk配置。

@ServletComponentScan
public classApplication {

    //DingTalk Bean变量
    private static String DING_TALK_UTIL_BEAN = "dingtalkUtil";
    public static voidmain(String[] args) {
//new SpringApplicationBuilder(Application.class).initializers(new AqumonApplicationContextInitializer()).run(args);
ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);
        ConfigurableListableBeanFactory beanFactory =applicationContext.getBeanFactory();
        //获取DingTalk Bean
        DingtalkUtil dingTalk =(DingtalkUtil) beanFactory.getBean(DING_TALK_UTIL_BEAN);
        //发送DingTalk通知
        dingTalk.sendTextNotificationToAccountInfo("INFO:Account启动完成");
    }
}

2.DingTalk相关工具类及其方法:

@Data
public classDingtalkUtil {
    Logger logger = LogManager.getLogger(DingtalkUtil.class);

    privateURI WEBHOOK_URL_ACCOUNT;

    privateURI WEBHOOK_URL_ACCOUNT_INFO;

    publicDingtalkUtil(DingtalkConfig dingtalkConfig) {
        try{
            WEBHOOK_URL_ACCOUNT = newURI(dingtalkConfig.getAccountUrl());
            WEBHOOK_URL_ACCOUNT_INFO = newURI(dingtalkConfig.getAccountInfoUrl());

        } catch(URISyntaxException e) {
            logger.fatal("Failed to parse URI of reminder webhook, due to: " +e.getMessage());
        }
    }

    /*** 发送文本提醒至Account警告群
     *
     * @parammsg 信息
     */
    public voidsendTextNotificationToAccount(String msg) {
        sendTextNotification(msg, WEBHOOK_URL_ACCOUNT);
    }

    /*** 发送文本提醒至Account通知群
     *
     */
    public voidsendTextNotificationToAccountInfo(String msg) {
        sendTextNotification(msg, WEBHOOK_URL_ACCOUNT_INFO);
    }

    /*** 发送文本提醒至任意机器人
     *
     * @parammsg        信息
     * @paramwebhookUri 机器人的webhook地址,新的地址可在上面添加为常量
     */
    public voidsendTextNotification(String msg, URI webhookUri) {
        RestTemplate restTemplate = newRestTemplate();
        restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
        HttpHeaders headers = newHttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<String>(buildJsonTextMessage(msg, false), headers);
        HttpEntity<String> response = restTemplate.postForEntity(webhookUri, entity, String.class);
        logger.info("Response:
" +response.getBody());
    }

    /*** 创建文本提醒Body
     *
     * @parammsg
     * @paramisAtAll
     * @return
     */
    public String buildJsonTextMessage(String msg, booleanisAtAll) {
        StringBuilder sb = newStringBuilder();
        String atAll = isAtAll ? "true" : "false";
        sb.append("{ "msgtype": "text", "text": { "content": "" + msg + ""}, "isAtAll": " + atAll + "}");
        returnsb.toString();
    }

免责声明:文章转载自《在springboot启动时给钉钉群发通知》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇el-upload配合vue-cropper实现上传图片前裁剪unigui+fastReport实现web打印方案下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

PHP教程:PHPUnit学习笔记(四)断言详解

断言(Assertions)是PHPUnit提供的一系列对程序执行结果测试的方法。通俗的讲,就是断言执行程序结果为我们期待的值,如果不是则 测试失败,下面是断言方法的详细介绍,内容全部来翻译自PHPUnit的官方文档,部分方法官方介绍的很模糊,我根据官方的源码注释增加了说明和注释 assertArrayHasKey(mixed $key, array $a...

【Android】让你的安卓app国际化、支持语言自动切换

碎碎念 我在写app的时候,无论是布局上的字符串还是代码中的,我都喜欢直接敲上去,无视那个善意提醒的波浪线。 对于小项目来说,我觉得无可厚非,虽然不规范但方便直观,不需要苦于给字符串起名字。 如果你在项目初期就想着要将应用推向国际市场,就要注意字符串一定要养成习惯全部写在string.xml里,不然后期再加真的很崩溃。 创建多语言的string.xml 我...

C++ Primer学习笔记

目录 12.1 动态内存与智能指针 12.1.1 shared_ptr类 12.1.2 直接管理内存 12.1.3 shared_ptr和new结合使用 12.1.4 智能指针和异常 12.1.5 unique_ptr 12.1.6 weak_ptr 12.2 动态数组 12.2.1 new和数组 12.2.2 alllocator类...

转载:Android应用的自动更新模块

软件的自动更新一般都与Splash界面绑定在一起, 由于需要维护的软件界面很复杂, 一个Activity中嵌入ViewPager, 并且逻辑比较复杂, 索性重新写一个Activity, 现在的软件都很流行使用Splash界面, 正好与自动更新配套在一起; 在这个自动更新Splash中, 使用到了 动画设置 ,SharedPerference ,pull解析...

ES简单工具类ESUtil

packagecom.alibaba.otter.canal.utils; importcom.alibaba.otter.canal.constants.ModuleEnum; importcom.alibaba.otter.canal.custom.CanalLogUtil; importlombok.extern.slf4j.Slf4j; impo...

洛谷 2957 [USACO09OCT]谷仓里的回声Barn Echoes

题目描述 The cows enjoy mooing at the barn because their moos echo back, although sometimes not completely. Bessie, ever the excellent secretary, has been recording the exact wording...