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

现在使用飞书的人越来越多了,飞书有一个最大的好处,可以使用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;
    }

四、验证

五:代码下载地址:百度网盘 请输入提取码

通过百度网盘分享的文件:send-feishu-msg

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

提取码:yv0l

相关推荐
步行cgn10 小时前
Spring c 命名空间注入详解
java·后端·spring
明月_清风11 小时前
Maven 到底是什么?一篇文章搞懂 Java 项目构建与依赖管理
java·后端·maven
aramae11 小时前
MySQL复合查询(8)
java·c语言·开发语言·后端·算法
Rain的Java大神之路11 小时前
如何快速上传10G文件
java·spring boot·redis·后端·mysql·spring cloud·面试
Wang's Blog12 小时前
Java 接入Redis: 通用命令与键管理
java·服务器·redis
Wang's Blog12 小时前
Java 接入Redis: 列表集合与有序集合操作命令
java·服务器·redis
Ivanqhz13 小时前
SVD++算法
java·服务器·网络·深度学习·神经网络
qq_25183645713 小时前
springboot vue3 开发实现 拼豆管理系统
java·开发语言·ai编程
caoerzhong14 小时前
JeeWMS 开源仓库管理系统全景解读:一套 Java WMS 如何把 WMS/OMS/BMS/TMS 装进同一个系统
java·开源
曹牧14 小时前
Java集合框架:List、ArrayList、Map 和 HashMap
java