最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot項(xiàng)目發(fā)送釘釘消息功能實(shí)現(xiàn)

 更新時(shí)間:2024年02月02日 11:16:07   作者:c103363  
在工作中的一些告警需要發(fā)送釘釘通知,有的是發(fā)給個(gè)人,有的要發(fā)到群里,這時(shí)項(xiàng)目就需要接入釘釘,實(shí)現(xiàn)發(fā)消息的功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

背景:在工作中的一些告警需要發(fā)送釘釘通知,有的是發(fā)給個(gè)人,有的要發(fā)到群里,這時(shí)項(xiàng)目就需要接入釘釘,實(shí)現(xiàn)發(fā)消息的功能

1. 添加釘釘依賴

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

2. 發(fā)送工作通知

發(fā)送通知需要使用access_token,獲取token需要根據(jù)AppKey、AppSecret,所以需要在釘釘后臺(tái)建一個(gè)應(yīng)用,獲許AppKey和AppSecret

2.1. 后臺(tái)建一個(gè)應(yīng)用

獲取token文檔:

https://open.dingtalk.com/document/orgapp/obtain-the-access_token-of-an-internal-app 

發(fā)消息文檔:

https://open.dingtalk.com/document/orgapp/asynchronous-sending-of-enterprise-session-messages

2.2. 詳細(xì)代碼如下

替換相應(yīng)的配置就可以直接發(fā)送了

import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
import com.aliyun.tea.TeaException;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
/**
 * @author csn
 * @date 2024/1/26
 * @description 發(fā)送個(gè)人消息
 */
public class SimpleExample {
    // 釘釘官方文檔 - 獲取 AccessToken
    // https://open.dingtalk.com/document/orgapp/obtain-the-access_token-of-an-internal-app
    // 釘釘官方文檔 - 發(fā)送工作通知消息
    // https://open.dingtalk.com/document/orgapp/asynchronous-sending-of-enterprise-session-messages
    /**
     * 應(yīng)用的 AgentId
     */
    private static Long AGENT_ID = 2883514974L;
    /**
     * 應(yīng)用的 AppKey
     */
    private static String APP_KEY = "dingiektffikbgglasadcs";
    /**
     * 應(yīng)用的 AppSecret
     */
    private static String APP_SECRET = "_Ha8VQMwM_Hht3jWrPR8502O3BIQ_Lzf-G7DsK4c-u2hz6y2hzFZ7WinumIy6X7khg";
    /**
     * 使用 Token 初始化賬號(hào)Client
     *
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dingtalkoauth2_1_0.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkoauth2_1_0.Client(config);
    }
    /**
     * 獲取 AccessToken
     *
     * @return AccessToken
     * @throws Exception
     */
    public static String getAccessToken() throws Exception {
        com.aliyun.dingtalkoauth2_1_0.Client client = createClient();
        com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest getAccessTokenRequest = new com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest()
        .setAppKey(APP_KEY)
        .setAppSecret(APP_SECRET);
        try {
            GetAccessTokenResponse accessToken = client.getAccessToken(getAccessTokenRequest);
            return accessToken.body.getAccessToken();
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 屬性,可幫助開發(fā)定位問題
            }
        } catch (Exception exception) {
            TeaException err = new TeaException(exception.getMessage(), exception);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 屬性,可幫助開發(fā)定位問題
            }
        }
        return null;
    }
    /**
     * 發(fā)送消息
     */
    public static void sendMessage() throws Exception {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        // 應(yīng)用上可以看到
        request.setAgentId(AGENT_ID);
        // 發(fā)送給誰 (接收人的釘釘 userid)
        request.setUseridList("0168333232323702649");
        request.setToAllUser(false);
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        // 消息類型 - markdown
        msg.setMsgtype("markdown");
        msg.setMarkdown(new OapiMessageCorpconversationAsyncsendV2Request.Markdown());
        msg.getMarkdown().setText("##### text");
        msg.getMarkdown().setTitle("### Title");
        request.setMsg(msg);
        // token
        OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, getAccessToken());
        System.out.println(rsp.getBody());
    }
    /**
     * 測試發(fā)送個(gè)人消息
     */
    public static void main(String[] args) throws Exception {
        sendMessage();
    }
}

3. 發(fā)送群消息

3.1. 群內(nèi)建立機(jī)器人

獲取secret和Webhook就可以發(fā)送了

文檔:

https://open.dingtalk.com/document/orgapp/custom-bot-to-send-group-chat-messages

建立機(jī)器人步驟如下:

img

img

給機(jī)器人取個(gè)名字

安全設(shè)置必選選一個(gè),這里選擇加簽,后面要用到,具體原因可以看文檔

img

完成后又個(gè)Webhook,這個(gè)是發(fā)送消息用的url

img

3.2. 詳細(xì)代碼如下

替換相應(yīng)的配置就可以直接發(fā)送了

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.codec.binary.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
/**
 * @author csn
 * @date 2024/1/26
 * @description 機(jī)器人發(fā)送群消息
 */
@Slf4j
public class BotExample {
    // 釘釘官方文檔
    // https://open.dingtalk.com/document/orgapp/custom-bot-to-send-group-chat-messages
    /**
     * 機(jī)器人的加簽秘鑰 (替換成自己的)
     */
    private static final String SECRET = "SEC9a6b2202a0b92847fd7098630d20b4da9c02862d3696968a27b96d1e5fa073400o23412";
    /**
     * 機(jī)器人的webhook (替換成自己的)
     */
    private static final String URL = "https://oapi.dingtalk.com/robot/send?access_token=3c3628a434166b0b9180ddba360asd06b9999270e1655a031c74b362b069ef2f328";
    /**
     * 組裝簽名url
     *
     * @return url
     */
    public static String getUrl() throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
        Long timestamp = System.currentTimeMillis();
        String stringToSign = timestamp + "\n" + SECRET;
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(SECRET.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
        byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
        String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
        String signResult = "&timestamp=" + timestamp + "&sign=" + sign;
        // 得到拼接后的 URL
        return URL + signResult;
    }
    /**
     * 獲取客戶端
     *
     * @return
     */
    public static DingTalkClient getClient() throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
        return new DefaultDingTalkClient(getUrl());
    }
    /**
     * 發(fā)送消息
     *
     * @param content
     * @throws ApiException
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     * @throws InvalidKeyException
     */
    public static void sendText(String content) throws ApiException, NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
        DingTalkClient client = getClient();
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        // 消息類型 - 文本
        request.setMsgtype("text");
        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
        text.setContent(content);
        request.setText(text);
        // @人
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
        // isAtAll類型如果不為Boolean,請升級至最新SDK
        at.setIsAtAll(false);
        // 釘釘上注冊的手機(jī)號(hào)就行了
        at.setAtMobiles(Collections.singletonList("13123920295"));
        request.setAt(at);
        OapiRobotSendResponse response = client.execute(request);
        log.info("success:{}, code:{}, errorCode:{}, errorMsg:{}", response.isSuccess(), response.getCode(), response.getErrcode(), response.getErrmsg());
    }
    /**
     * 發(fā)送消息測試
     */
    public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException, ApiException {
        sendText("測試消息");
    }
}

到此這篇關(guān)于SpringBoot項(xiàng)目發(fā)送釘釘消息的文章就介紹到這了,更多相關(guān)SpringBoot發(fā)送釘釘消息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MyBatisPlus+Lombok實(shí)現(xiàn)分頁功能的方法詳解

    MyBatisPlus+Lombok實(shí)現(xiàn)分頁功能的方法詳解

    Lombok是一個(gè)Java類庫,提供了一組注解,簡化POJO實(shí)體類開發(fā)。本文將為大家介紹一下Lombok的使用以及如何利用MyBatisPlus+Lombok實(shí)現(xiàn)分頁功能,感興趣的可以動(dòng)手嘗試一下
    2022-07-07
  • 基于Java的電梯系統(tǒng)實(shí)現(xiàn)過程

    基于Java的電梯系統(tǒng)實(shí)現(xiàn)過程

    這篇文章主要介紹了基于Java的電梯系統(tǒng)實(shí)現(xiàn)過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • SpringBoot與JWT整合方式

    SpringBoot與JWT整合方式

    文章介紹了如何在Spring?Boot項(xiàng)目中整合JWT(JSON?Web?Token),包括JWT的結(jié)構(gòu)、使用方法、測試以及配置,主要內(nèi)容涵蓋了依賴配置、數(shù)據(jù)庫表設(shè)計(jì)、實(shí)體類、數(shù)據(jù)訪問層、服務(wù)層、JWT工具類、攔截器配置和控制器測試等多個(gè)方面
    2024-11-11
  • Java全能工具類之Hutool的用法詳解

    Java全能工具類之Hutool的用法詳解

    Hutool是一個(gè)Java工具類庫,由國內(nèi)的程序員loolly開發(fā),目的是提供一些方便、快捷、實(shí)用的工具類和工具方法,本文就來詳細(xì)聊聊它的使用吧
    2023-03-03
  • Maven的porfile與SpringBoot的profile結(jié)合使用案例詳解

    Maven的porfile與SpringBoot的profile結(jié)合使用案例詳解

    這篇文章主要介紹了Maven的porfile與SpringBoot的profile結(jié)合使用,通過maven的profile功能,在打包的時(shí)候,通過-P指定maven激活某個(gè)pofile,這個(gè)profile里面配置了一個(gè)參數(shù)activatedProperties,不同的profile里面的這個(gè)參數(shù)的值不同,需要的朋友可以參考下吧
    2021-12-12
  • Java中的分布式概念說明

    Java中的分布式概念說明

    在Java中,分布式系統(tǒng)是由一組通過網(wǎng)絡(luò)進(jìn)行通信、為了完成共同的任務(wù)而協(xié)調(diào)工作的計(jì)算機(jī)節(jié)點(diǎn)組成的系統(tǒng),這種系統(tǒng)架構(gòu)的目的是利用更多的機(jī)器處理更多的數(shù)據(jù),從而解決單個(gè)計(jì)算機(jī)無法應(yīng)對的計(jì)算、存儲(chǔ)任務(wù),本文給大家介紹Java中的分布式概念說明,感興趣的朋友一起看看吧
    2025-09-09
  • 解決springmvc使用@PathVariable路徑匹配問題

    解決springmvc使用@PathVariable路徑匹配問題

    這篇文章主要介紹了解決springmvc使用@PathVariable路徑匹配問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Spring Cloud詳解實(shí)現(xiàn)聲明式微服務(wù)調(diào)用OpenFeign方法

    Spring Cloud詳解實(shí)現(xiàn)聲明式微服務(wù)調(diào)用OpenFeign方法

    這篇文章主要介紹了Spring Cloud實(shí)現(xiàn)聲明式微服務(wù)調(diào)用OpenFeign方法,OpenFeign 是 Spring Cloud 家族的一個(gè)成員, 它最核心的作用是為 HTTP 形式的 Rest API 提供了非常簡潔高效的 RPC 調(diào)用方式,希望對大家有所幫助。一起跟隨小編過來看看吧
    2022-07-07
  • Mybatis延遲加載原理和延遲加載配置詳解

    Mybatis延遲加載原理和延遲加載配置詳解

    這篇文章主要介紹了Mybatis延遲加載原理和延遲加載配置詳解,MyBatis中的延遲加載,也稱為懶加載,是指在進(jìn)行表的關(guān)聯(lián)查詢時(shí),按照設(shè)置延遲規(guī)則推遲對關(guān)聯(lián)對象的select查詢,需要的朋友可以參考下
    2023-10-10
  • SpringBoot整合RocketMQ實(shí)現(xiàn)發(fā)送同步消息

    SpringBoot整合RocketMQ實(shí)現(xiàn)發(fā)送同步消息

    RocketMQ 是一款開源的分布式消息中間件,由阿里巴巴開源,它具有高可用性、高性能、低延遲等特點(diǎn),廣泛應(yīng)用于阿里巴巴集團(tuán)內(nèi)部以及眾多外部企業(yè)的業(yè)務(wù)系統(tǒng)中,本文給大家介紹了SpringBoot整合RocketMQ實(shí)現(xiàn)發(fā)送同步消息,需要的朋友可以參考下
    2024-04-04

最新評論

凤山县| 佛坪县| 陆良县| 喀喇| 荆门市| 抚州市| 沂源县| 延边| 海南省| 黔西县| 油尖旺区| 香港 | 广东省| 会理县| 城口县| 五莲县| 安岳县| 嘉荫县| 永兴县| 白沙| 玉溪市| 墨竹工卡县| 大方县| 峨山| 咸宁市| 皋兰县| 页游| 浙江省| 定西市| 曲阳县| 马鞍山市| 安顺市| 华宁县| 锡林郭勒盟| 慈利县| 五原县| 台东县| 酒泉市| 阿尔山市| 东港市| 班玛县|