执门文章
java通过webhook给飞书发送群消息
于 2024-09-26 23:41:59 发布
访问139
收藏0
文章标签:
webhhok
飞书
机器人
群消息
java

现在使用飞书的人越来越多了,飞书有一个最大的好处,可以使用webhook简便的发送群消息。而在工作中,也经常会因为一些运维方面的工作,需要给飞书发送群消息,来实时提醒相关负责人,及时处理工作。

一、先看一下效果吧:

最后有整个项目代码下载

java通过webhook给飞书发送群消息


二、飞书创建群

java通过webhook给飞书发送群消息

java通过webhook给飞书发送群消息

java通过webhook给飞书发送群消息

java通过webhook给飞书发送群消息

java通过webhook给飞书发送群消息

java通过webhook给飞书发送群消息


三、java通过webhook发飞书发送消息

通过上一步,已经获取到如下信息(根据实际情况复制出来,后面会用到):

webhook地址:https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxx

签名校验:xxxxxxx​

上面两个获取到了,下面就是java发送了

1、bootstrap.yml中配置如下:

spring:
  application:
    name: base
server:
  port: 9080
  servlet:
    context-path: /
feishu:
  aiUrl: https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxx #飞书机器人通知
  secret: xxxxxxxxxxxxxxxx
  signName: 基础平台

2、controller代码

package com.ck.controller;

import com.ck.config.FeiShuAiClient;
import com.ck.service.TestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/test")
@Api(tags = "TestController", description = "测试")
public class TestController {
    private static final Logger logger = LoggerFactory.getLogger(TestController.class);
    @Autowired
    private TestService testService;
    @Autowired
    private FeiShuAiClient feiShuAiClient;

    @GetMapping("/send")
    @ApiOperation("发送内空")
    public String find(String name) {
        name="当前发送内容:"+name;
        feiShuAiClient.sendMsg(name);
        return "发送成功";
    }
}

3、发送飞书代码

 /**
     * 发送结果
     * @param content
     */
    public void sendMsg(String content){
        content="【"+signName+"】"+content;
        Long timestamp = getTimestamp();
        String sign = Sign(timestamp);
        FeiShuContentVo contentVo = new FeiShuContentVo(content);
        FeiShuAiVo aiVo = new FeiShuAiVo();
        aiVo.setTimestamp(timestamp.toString());
        aiVo.setSign(sign);
        aiVo.setMsg_type("text");
        aiVo.setContent(contentVo);
        String paramJson = GsonUtils.toJson(aiVo);
        String result = doPost(aiUrl,paramJson);
        log.info("飞书发送内容:"+content+",发送结果:"+result);
    }


    public String genSign(String secret, long timestamp) {
        //把timestamp+"\n"+密钥当做签名字符串
        String stringToSign = timestamp + "\n" + secret;

        //使用HmacSHA256算法计算签名
        Mac mac = null;
        try {
            mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(stringToSign.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
            byte[] signData = mac.doFinal(new byte[]{});
            return new String(Base64.encodeBase64(signData));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


四、验证

java通过webhook给飞书发送群消息

java通过webhook给飞书发送群消息

五:代码下载地址:

https://pan.baidu.com/s/1Dlyy64Tqwer8sSJu7vJGgQ?pwd=yv0l



于 2024-09-26 23:41:59 发布
访问139
收藏0

实力雄厚的技术网站

产品

概述

资源

文档

版权问题

请联系客服

联系我们

联系我们


© 财瑞智能科技 2024蜀ICP备2023018175号-2川公网安备51011202000656号