飞书钩子
This commit is contained in:
parent
d3432062b8
commit
60f6020c2c
124
src/main/java/com/example/demo/common/utils/WebHookUtil.java
Normal file
124
src/main/java/com/example/demo/common/utils/WebHookUtil.java
Normal 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user