飞书钩子

This commit is contained in:
yuejiajun 2025-10-15 18:20:38 +08:00
parent d3432062b8
commit 60f6020c2c

View File

@ -0,0 +1,124 @@
package com.example.demo.common.utils;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
@Slf4j
public class WebHookUtil {
@SneakyThrows
public static void main(String[] args) {
demo01();
}
@SneakyThrows
private static void demo01() {
webHook(
"key",
"通知",
"",
"""
任务执行\r
核心思想化整为零动态调整\r
将复杂的项目目标分解为一个个可执行可跟踪可验收的小任务并通过持续的沟通和调整来确保方向正确\r
本消息由 虚兵001 提供""");
webHook(
"key",
"最新通知",
"",
"""
AI资讯\r
- 多模态模型社区全新升级LiblibAI 2.0正式上线\r
- 快手推出AI原生IDE工具CodeFlicker对标Cursor\r
- 蚂蚁百灵正式发布Ling 2.0系列的首款旗舰非思考模型Ling-1T\r
本消息由 虚兵002 提供""");
String secret = "secret";
webHook(
"key",
"通知",
secret,
"北京市朝阳区今天实况:" +
"\n 17度 晴湿度62%北风1级。" +
"\n 白天22度,晴。 " +
"\n 夜间11度温度适中。" +
"\n注本消息由 虚兵003 提供");
webHook(
"key",
"",
secret,
"今天中国的黄金价格是 每克 CN¥921.28 24k 金子, 每克 CN¥844.51 " +
"\n注本消息由 虚兵003 提供");
}
public static void webHook(String url, String prefix, String secret, String message) throws NoSuchAlgorithmException, InvalidKeyException {
String baseURL = "https://open.feishu.cn/open-apis/bot/v2/hook/";
int timestamp = (int) (System.currentTimeMillis() / 1000);
String sign = GenSign(secret, timestamp);
String fullMessage = StrUtil.isBlank(prefix) ? message : String.format("%s\n %s", prefix, message);
Map<String, Object> requestData = createMessage(sign, timestamp, fullMessage);
String fullURL = String.format("%s%s", baseURL, url);
// 直接发送map报错参数错误
// String responseData = HttpUtil.post(fullURL, requestData);
String responseData = HttpUtil.post(fullURL, JSON.toJSONString(requestData));
log.info("responseData: {}", responseData);
log.info("URL: {}", fullURL);
log.info("requestData: {}", JSON.toJSONString(requestData));
}
public static String createWebHookData(String secret, String message) throws NoSuchAlgorithmException, InvalidKeyException {
int timestamp = (int) (System.currentTimeMillis() / 1000);
String sign = GenSign(secret, timestamp);
Map<String, Object> requestData = createMessage(sign, timestamp, message);
@SuppressWarnings("all")
String requestJSONString = JSON.toJSONString(requestData);
return requestJSONString;
}
private static Map<String, Object> createMessage(String secret, int timestamp, String message) throws NoSuchAlgorithmException, InvalidKeyException {
Map<String, Object> map = new HashMap<>();
if(StrUtil.isNotBlank(secret)) {
map.put("timestamp", timestamp);
map.put("sign", secret);
}
map.put("msg_type", "text");
Map<String, Object> content = new HashMap<>();
content.put("text", message);
map.put("content", content);
return map;
}
public static String createWebHook(String message) {
return null;
}
private static String GenSign(String secret, int timestamp) throws NoSuchAlgorithmException, InvalidKeyException {
if(StrUtil.isBlank(secret)) {
return null;
}
//把timestamp+"\n"+密钥当做签名字符串
String stringToSign = timestamp + "\n" + secret;
//使用HmacSHA256算法计算签名
Mac 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));
}
}