|
|
@@ -3,9 +3,11 @@ package cn.reghao.tnb.message.app.service.notifier.ding;
|
|
|
import cn.reghao.jutil.jdk.http.WebRequest;
|
|
|
import cn.reghao.jutil.jdk.http.WebResponse;
|
|
|
import cn.reghao.jutil.jdk.serializer.JsonConverter;
|
|
|
-import cn.reghao.jutil.tool.http.DefaultWebRequest;
|
|
|
+import cn.reghao.tnb.message.app.db.mapper.WebhookMapper;
|
|
|
+import cn.reghao.tnb.message.app.model.po.Webhook;
|
|
|
import cn.reghao.tnb.message.app.service.notifier.Notify;
|
|
|
import com.google.gson.JsonObject;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.crypto.Mac;
|
|
|
@@ -22,17 +24,32 @@ import java.util.Base64;
|
|
|
* @author reghao
|
|
|
* @date 2020-05-07 01:31:03
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Component
|
|
|
public class DingNotify implements Notify<DingMsg> {
|
|
|
private final WebRequest webRequest;
|
|
|
+ private final WebhookMapper webhookMapper;
|
|
|
|
|
|
- public DingNotify() {
|
|
|
- this.webRequest = new DefaultWebRequest();
|
|
|
+ public DingNotify(WebRequest webRequest, WebhookMapper webhookMapper) {
|
|
|
+ this.webRequest = webRequest;
|
|
|
+ this.webhookMapper = webhookMapper;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void send(String receiver, DingMsg msg) throws Exception {
|
|
|
- WebResponse webResponse = webRequest.postJson(receiver, JsonConverter.objectToJson(msg));
|
|
|
+ Webhook webhook = webhookMapper.findByName(receiver);
|
|
|
+ if (webhook == null) {
|
|
|
+ log.info("没有可用于发送通知的 webhook 配置");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String url = webhook.getUrl();
|
|
|
+ String sign = webhook.getSign();
|
|
|
+ if (sign != null) {
|
|
|
+ url = getReceiver(url, sign);
|
|
|
+ }
|
|
|
+
|
|
|
+ WebResponse webResponse = webRequest.postJson(url, JsonConverter.objectToJson(msg));
|
|
|
if (webResponse.getStatusCode() != 200) {
|
|
|
throw new Exception(webResponse.getBody());
|
|
|
}
|
|
|
@@ -47,7 +64,6 @@ public class DingNotify implements Notify<DingMsg> {
|
|
|
|
|
|
private String getReceiver(String url, String secret) throws InvalidKeyException, NoSuchAlgorithmException {
|
|
|
long timestamp = System.currentTimeMillis();
|
|
|
- //String secret = "SEC4d7e0c126147b3679c4d15e47dc26311ca053bd47b21f4025832a6e246a93ec4";
|
|
|
String sign = calcSign(timestamp, secret);
|
|
|
return url + String.format("×tamp=%s&sign=%s", timestamp, sign);
|
|
|
}
|