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

ops/2024/10/19 23:39:35/

现在使用飞书的人越来越多了,飞书有一个最大的好处,可以使用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: 9080servlet:context-path: /
feishu:aiUrl: https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxx #飞书机器人通知secret: xxxxxxxxxxxxxxxxsignName: 基础平台

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);@Autowiredprivate TestService testService;@Autowiredprivate 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


http://www.ppmy.cn/ops/122054.html

相关文章

数据结构--集合框架

目录 1. 什么是集合框架 2. 背后所涉及的数据结构以及算法 2.1 什么是数据结构 2.2 容器背后对应的数据结构 1. 什么是集合框架 Java 集合框架 Java Collection Framework ,又被称为容器 container ,是定义在 java.util 包下的一组接口 int…

音视频入门基础:FLV专题(12)——FFmpeg源码中,解析DOUBLE类型的ScriptDataValue的实现

一、引言 从《音视频入门基础:FLV专题(9)——Script Tag简介》中可以知道,根据《video_file_format_spec_v10_1.pdf》第80到81页,SCRIPTDATAVALUE类型由一个8位(1字节)的Type和一个ScriptDataV…

Linux自动化构建工具Make/Makefile

make是一个命令 makefile是一个文件 touch 创建并用vim打开makefile 写入依赖对象和依赖方法 mycode是目标文件 第二行数依赖方法 以tab键开头 make makefile原理 makefile中写的是依赖关系和依赖方法 clean英语清理文件 后不用加源文件。.PHONY定义clean是伪目标。 make只…

SQL进阶技巧:统计各时段观看直播的人数

目录 0 需求描述 1 数据准备 2 问题分析 3 小结 如果觉得本文对你有帮助,那么不妨也可以选择去看看我的博客专栏 ,部分内容如下: 数字化建设通关指南 专栏 原价99,现在活动价39.9,十一国庆后将上升至59.9&#…

UE5.4.3 Replay 重播回放系统

工程的配置文件DefaultEngine.ini中需要加入 +NetDriverDefinitions=(DefName=“DemoNetDriver”,DriverClassName=“/Script/Engine.DemoNetDriver”,DriverClassNameFallback=“/Script/Engine.DemoNetDriver”) 此步骤将启用并加载DemoNetDriver .ini添加示例 [/Script/En…

5QI(5G QoS Identifier)

5QI(5G QoS Identifier,5G 服务质量标识符)是在5G网络中用于定义特定数据流所需服务级别的指标。它用于优先处理流量,并根据流量的类型及其特定需求分配网络资源。5QI值从1到255,每个值对应一组QoS参数,这些…

前端Vue项目的自动打包、上传与部署

文章目录 前言思路与流程脚本实现1. 打包前端项目2. 上传前端项目4. 传递密码5. 代码优化完整脚本结语前言 在实际项目开发中,并不是所有项目都会配置 CI/CD 流程,特别是在中小型团队或者公司内部测试环境中,很多时候我们仍然需要手动打包、上传和部署项目。这个过程虽然简…

react+antdMobie实现消息通知页面样式

一、实现效果 二、代码 import React, { useEffect, useState } from react; import style from ./style/index.less; import { CapsuleTabs, Ellipsis, Empty, SearchBar, Tag } from antd-mobile; //消息通知页面 export default function Notification(props) {const [opti…