Skip to content

Commit

Permalink
Merge pull request #413 from KamToHung/feature/395_simple_webhook
Browse files Browse the repository at this point in the history
[ISSUE #395] Simplify notifiy Webhook
  • Loading branch information
yanhom1314 authored Mar 8, 2024
2 parents b23fa34 + 300cfd0 commit 815c177
Show file tree
Hide file tree
Showing 15 changed files with 343 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ public final class DingNotifyConst {

private DingNotifyConst() { }

public static final String DING_WEBHOOK = "https://oapi.dingtalk.com/robot/send?access_token=";
public static final String DING_WEBHOOK = "https://oapi.dingtalk.com/robot/send";

public static final String ACCESS_TOKEN_PARAM = "access_token";

public static final String TIMESTAMP_PARAM = "timestamp";

public static final String SIGN_PARAM = "sign";

public static final String WARNING_COLOR = "#EA9F00";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private LarkNotifyConst() { }
/**
* lark bot url
*/
public static final String LARK_WEBHOOK = "https://open.feishu.cn/open-apis/bot/v2/hook/";
public static final String LARK_WEBHOOK = "https://open.feishu.cn/open-apis/bot/v2/hook";

/**
* lark at format. openid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public final class WechatNotifyConst {

private WechatNotifyConst() { }

public static final String WECHAT_WEB_HOOK = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=";
public static final String WECHAT_WEB_HOOK = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send";

public static final String KEY_PARAM = "key";

public static final String WARNING_COLOR = "warning";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public class NotifyPlatform {
private String secret;

/**
* webHook, may be null.
* webhook, may be null.
*/
private String webHook;
private String webhook;

/**
* Receivers, split by ,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.dromara.dynamictp.common.notifier;

import cn.hutool.core.net.url.UrlBuilder;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
Expand Down Expand Up @@ -73,24 +74,29 @@ protected String buildMsgBody(NotifyPlatform platform, String content) {

@Override
protected String buildUrl(NotifyPlatform platform) {
String webHook = Optional.ofNullable(platform.getWebHook()).orElse(DingNotifyConst.DING_WEBHOOK);
return getTargetUrl(platform.getSecret(), platform.getUrlKey(), webHook);
String webhook = Optional.ofNullable(platform.getWebhook()).orElse(DingNotifyConst.DING_WEBHOOK);
return getTargetUrl(platform.getSecret(), platform.getUrlKey(), webhook);
}

/**
* Build target url.
*
* @param secret secret
* @param accessToken accessToken
* @param webHook webHook
* @param webhook webhook
* @return url
*/
private String getTargetUrl(String secret, String accessToken, String webHook) {
if (StringUtils.isBlank(secret)) {
return webHook + accessToken;
private String getTargetUrl(String secret, String accessToken, String webhook) {
UrlBuilder builder = UrlBuilder.of(webhook);
if (StringUtils.isNotBlank(accessToken) && StringUtils.isBlank(builder.getQuery().get(DingNotifyConst.ACCESS_TOKEN_PARAM))) {
builder.addQuery(DingNotifyConst.ACCESS_TOKEN_PARAM, accessToken);
}
long timestamp = System.currentTimeMillis();
String sign = DingSignUtil.dingSign(secret, timestamp);
return webHook + accessToken + "&timestamp=" + timestamp + "&sign=" + sign;
if (StringUtils.isNotBlank(secret)) {
long timestamp = System.currentTimeMillis();
builder.addQuery(DingNotifyConst.TIMESTAMP_PARAM, timestamp);
builder.addQuery(DingNotifyConst.SIGN_PARAM, DingSignUtil.dingSign(secret, timestamp));
}
return builder.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.dromara.dynamictp.common.notifier;

import cn.hutool.core.net.url.UrlBuilder;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.codec.binary.Base64;
Expand All @@ -30,6 +31,8 @@
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static org.dromara.dynamictp.common.constant.LarkNotifyConst.SIGN_PARAM_PREFIX;
Expand Down Expand Up @@ -94,7 +97,15 @@ protected String buildMsgBody(NotifyPlatform platform, String content) {

@Override
protected String buildUrl(NotifyPlatform platform) {
String webHook = Optional.ofNullable(platform.getWebHook()).orElse(LarkNotifyConst.LARK_WEBHOOK);
return webHook + platform.getUrlKey();
if (StringUtils.isBlank(platform.getUrlKey())) {
return platform.getWebhook();
}
UrlBuilder builder = UrlBuilder.of(Optional.ofNullable(platform.getWebhook()).orElse(LarkNotifyConst.LARK_WEBHOOK));
List<String> segments = builder.getPath().getSegments();
if (StringUtils.isNotBlank(platform.getUrlKey()) && !Objects.equals(platform.getUrlKey(), segments.get(segments.size() - 1))) {
builder.addPath(platform.getUrlKey());
}
return builder.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package org.dromara.dynamictp.common.notifier;

import cn.hutool.core.net.url.UrlBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dromara.dynamictp.common.constant.WechatNotifyConst;
import org.dromara.dynamictp.common.em.NotifyPlatformEnum;
import org.dromara.dynamictp.common.entity.MarkdownReq;
Expand Down Expand Up @@ -52,7 +54,14 @@ protected String buildMsgBody(NotifyPlatform platform, String content) {

@Override
protected String buildUrl(NotifyPlatform platform) {
String webHook = Optional.ofNullable(platform.getWebHook()).orElse(WechatNotifyConst.WECHAT_WEB_HOOK);
return webHook + platform.getUrlKey();
if (StringUtils.isBlank(platform.getUrlKey())) {
return platform.getWebhook();
}
UrlBuilder builder = UrlBuilder.of(Optional.ofNullable(platform.getWebhook()).orElse(WechatNotifyConst.WECHAT_WEB_HOOK));
if (StringUtils.isNotBlank(platform.getUrlKey()) && StringUtils.isBlank(builder.getQuery().get(WechatNotifyConst.KEY_PARAM))) {
builder.addQuery(WechatNotifyConst.KEY_PARAM, platform.getUrlKey());
}
return builder.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

package org.dromara.dynamictp.extension.notify.yunzhijia;

import cn.hutool.core.net.url.UrlBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dromara.dynamictp.common.entity.NotifyPlatform;
import org.dromara.dynamictp.common.util.JsonUtil;
import org.dromara.dynamictp.common.notifier.AbstractHttpNotifier;
import org.dromara.dynamictp.common.util.JsonUtil;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -50,7 +52,16 @@ protected String buildMsgBody(NotifyPlatform platform, String content) {

@Override
protected String buildUrl(NotifyPlatform platform) {
String webHook = Optional.ofNullable(platform.getWebHook()).orElse(YunZhiJiaNotifyConst.WEB_HOOK);
return webHook + platform.getUrlKey();
if (StringUtils.isBlank(platform.getUrlKey())) {
return platform.getWebhook();
}
UrlBuilder builder = UrlBuilder.of(Optional.ofNullable(platform.getWebhook()).orElse(YunZhiJiaNotifyConst.WEB_HOOK));
if (StringUtils.isBlank(builder.getQuery().get(YunZhiJiaNotifyConst.YZJ_TYPE_PARAM))) {
builder.addQuery(YunZhiJiaNotifyConst.YZJ_TYPE_PARAM, 0);
}
if (StringUtils.isNotBlank(platform.getUrlKey()) && StringUtils.isBlank(builder.getQuery().get(YunZhiJiaNotifyConst.YZJ_TOKEN_PARAM))) {
builder.addQuery(YunZhiJiaNotifyConst.YZJ_TOKEN_PARAM, platform.getUrlKey());
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public final class YunZhiJiaNotifyConst {
private YunZhiJiaNotifyConst() {
}

public static final String WEB_HOOK = "https://www.yunzhijia.com/gateway/robot/webhook/send?yzjtype=0&yzjtoken=";
public static final String WEB_HOOK = "https://www.yunzhijia.com/gateway/robot/webhook/send";

public static final String YZJ_TYPE_PARAM = "yzjtype";

public static final String YZJ_TOKEN_PARAM = "yzjtoken";

public static final String WARNING_COLOR = "warning";

Expand Down
4 changes: 4 additions & 0 deletions test/test-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,9 @@
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.dromara.dynamictp</groupId>
<artifactId>dynamic-tp-extension-notify-yunzhijia</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.dynamictp.test.common.notifier;

import org.dromara.dynamictp.common.entity.NotifyPlatform;
import org.dromara.dynamictp.common.notifier.DingNotifier;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* @author <a href = "mailto:[email protected]">KamTo Hung</a>
*/
public class DingNotifierTest {

@Test
public void buildUrlWithWebhook() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
NotifyPlatform notifyPlatform = new NotifyPlatform();
notifyPlatform.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=123");
// notifyPlatform.setUrlKey("123");
// notifyPlatform.setSecret("456");
DingNotifier dingNotifier = new DingNotifier();
Method privateMethod = DingNotifier.class.getDeclaredMethod("getTargetUrl", String.class, String.class, String.class);
privateMethod.setAccessible(true);
String result = (String) privateMethod.invoke(dingNotifier, notifyPlatform.getSecret(), notifyPlatform.getUrlKey(), notifyPlatform.getWebhook());
Assertions.assertEquals("https://oapi.dingtalk.com/robot/send?access_token=123", result);
}

@Test
public void buildUrlWithUrlKey() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
NotifyPlatform notifyPlatform = new NotifyPlatform();
notifyPlatform.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=123");
notifyPlatform.setUrlKey("123");
// notifyPlatform.setSecret("456");
DingNotifier dingNotifier = new DingNotifier();
Method privateMethod = DingNotifier.class.getDeclaredMethod("getTargetUrl", String.class, String.class, String.class);
privateMethod.setAccessible(true);
String result = (String) privateMethod.invoke(dingNotifier, notifyPlatform.getSecret(), notifyPlatform.getUrlKey(), notifyPlatform.getWebhook());
Assertions.assertEquals("https://oapi.dingtalk.com/robot/send?access_token=123", result);
}


@Test
public void buildUrlWithSecret() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
NotifyPlatform notifyPlatform = new NotifyPlatform();
notifyPlatform.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=123");
// notifyPlatform.setUrlKey("123");
notifyPlatform.setSecret("456");
DingNotifier dingNotifier = new DingNotifier();
Method privateMethod = DingNotifier.class.getDeclaredMethod("getTargetUrl", String.class, String.class, String.class);
privateMethod.setAccessible(true);
String result = (String) privateMethod.invoke(dingNotifier, notifyPlatform.getSecret(), notifyPlatform.getUrlKey(), notifyPlatform.getWebhook());
System.out.println(result);
Assertions.assertTrue(result.contains("timestamp="));
Assertions.assertTrue(result.contains("sign="));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.dynamictp.test.common.notifier;

import org.dromara.dynamictp.common.constant.LarkNotifyConst;
import org.dromara.dynamictp.common.entity.NotifyPlatform;
import org.dromara.dynamictp.common.notifier.LarkNotifier;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* @author <a href = "mailto:[email protected]">KamTo Hung</a>
*/
public class LarkNotifierTest {

@Test
public void buildUrlWithWebhook() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
NotifyPlatform notifyPlatform = new NotifyPlatform();
notifyPlatform.setWebhook(LarkNotifyConst.LARK_WEBHOOK + "/123");
// notifyPlatform.setUrlKey("123");
// notifyPlatform.setSecret("456");
LarkNotifier larkNotifier = new LarkNotifier();
Method privateMethod = LarkNotifier.class.getDeclaredMethod("buildUrl", NotifyPlatform.class);
privateMethod.setAccessible(true);
String result = (String) privateMethod.invoke(larkNotifier, notifyPlatform);
Assertions.assertEquals("https://open.feishu.cn/open-apis/bot/v2/hook/123", result);
}

@Test
public void buildUrlWithUrlKey() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
NotifyPlatform notifyPlatform = new NotifyPlatform();
notifyPlatform.setWebhook(LarkNotifyConst.LARK_WEBHOOK + "/123");
notifyPlatform.setUrlKey("123");
// notifyPlatform.setSecret("456");
LarkNotifier larkNotifier = new LarkNotifier();
Method privateMethod = LarkNotifier.class.getDeclaredMethod("buildUrl", NotifyPlatform.class);
privateMethod.setAccessible(true);
String result = (String) privateMethod.invoke(larkNotifier, notifyPlatform);
Assertions.assertEquals("https://open.feishu.cn/open-apis/bot/v2/hook/123", result);
}

}
Loading

0 comments on commit 815c177

Please sign in to comment.