diff --git a/.github/workflows/maven-publish.yml b/.github/workflows/maven-publish.yml index d933779..cad1f1c 100644 --- a/.github/workflows/maven-publish.yml +++ b/.github/workflows/maven-publish.yml @@ -5,25 +5,43 @@ name: Maven Package on: release: - types: [created] + types: [ created ] + workflow_dispatch: jobs: build: - runs-on: ubuntu-latest permissions: contents: read packages: write steps: - - uses: actions/checkout@v3 - - name: Set up JDK 8 - uses: actions/setup-java@v3 - with: - java-version: '8' - distribution: 'temurin' - server-id: github # Value of the distributionManagement/repository/id field of the pom.xml - settings-path: ${{ github.workspace }} # location for the settings.xml file - - - name: Build with Maven - run: mvn -B clear package --file pom.xml + - uses: actions/checkout@v3 + - name: Set up JDK 8 + uses: actions/setup-java@v3 + with: + java-version: 8 + distribution: temurin + server-id: ossrh + server-username: MAVEN_USERNAME + server-password: MAVEN_PASSWORD + gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }} + gpg-passphrase: MAVEN_GPG_PASSPHRASE + + - name: Build with Maven + run: mvn -B clean install -Dmaven.test.skip=true --file pom.xml + + - name: deploy Apache Maven Central + run: mvn deploy -Dmaven.test.skip=true --file pom.xml + env: + MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_PASSWORD }} + MAVEN_USERNAME: ${{ secrets.OSSRH_USER }} + MAVEN_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} + GITHUB_TOKEN: ${{ github.token }} +# GPG_PRIVATE_KEY: + +# -----BEGIN PGP PRIVATE KEY BLOCK----- +# *** +# -----END PGP PRIVATE KEY BLOCK----- + +# GPG_PASSWORD GPG_PRIVATE_KEY 的密码 \ No newline at end of file diff --git a/pom.xml b/pom.xml index 2d6e921..e45de66 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.admin4j chat-bot - 0.1.0 + 0.10.0 chat-bot chat-bot 群聊机器人支持钉钉等 https://github.com/admin4j @@ -24,7 +24,7 @@ io.github.admin4j http - 0.9.1 + 0.9.2 diff --git a/src/main/java/com/admin4j/chatbot/dingtalk/DingRobot.java b/src/main/java/com/admin4j/chatbot/dingtalk/DingRobot.java index 3198efb..0a635f9 100644 --- a/src/main/java/com/admin4j/chatbot/dingtalk/DingRobot.java +++ b/src/main/java/com/admin4j/chatbot/dingtalk/DingRobot.java @@ -3,9 +3,10 @@ import com.admin4j.chatbot.dingtalk.core.RobotResponse; import com.admin4j.chatbot.dingtalk.request.base.AbstractRobotRequest; import io.github.admin4j.http.ApiJsonClient; -import io.github.admin4j.http.core.Pair; +import io.github.admin4j.http.core.HttpDefaultConfig; import io.github.admin4j.http.exception.HttpException; import lombok.Data; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -14,10 +15,9 @@ import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.Base64; -import java.util.HashMap; -import java.util.Map; /** + * https://open.dingtalk.com/document/orgapp/robot-overview#e744a9d0adhpm * https://open.dingtalk.com/document/group/custom-robot-access * * @author andanyang @@ -25,10 +25,13 @@ */ @Data @Slf4j -public class DingRobot extends ApiJsonClient { +public class DingRobot { - private Map queryMap; + //private Map queryMap; + private String accessToken; private String secret; + @Setter + private ApiJsonClient apiJsonClient; /** * 初始化 @@ -38,53 +41,18 @@ public class DingRobot extends ApiJsonClient { */ public DingRobot(String accessToken, String secret) { super(); - queryMap = new HashMap<>(4); - this.queryMap.put("access_token", accessToken); + if (!accessToken.contains("https://oapi.dingtalk.com")) { + accessToken = "https://oapi.dingtalk.com/robot/send?access_token=" + accessToken; + } this.secret = secret; + this.accessToken = accessToken; + apiJsonClient = new ApiJsonClient(HttpDefaultConfig.get()); } public DingRobot(String accessToken) { this(accessToken, null); } - public DingRobot() { - super(); - } - - public void setAccessToken(String accessToken) { - this.queryMap.put("access_token", accessToken); - } - - - @Override - protected void init() { - super.init(); - baseUrl = "https://oapi.dingtalk.com/robot/send"; - } - - - @Override - public String buildUrl(String path, Pair[] queryParams, Map queryMap) { - - queryMap = this.queryMap; - if (StringUtils.isNotBlank(secret)) { - try { - long timestamp = System.currentTimeMillis(); - String stringToSign = timestamp + "\n" + secret; - Mac mac = Mac.getInstance("HmacSHA256"); - mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256")); - byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8")); - String sign = URLEncoder.encode(Base64.getEncoder().encodeToString(signData), "UTF-8"); - queryMap.put("timestamp", timestamp); - queryMap.put("sign", sign); - } catch (Exception e) { - log.error("DingRobot sign error", e); - } - - } - - return super.buildUrl(path, queryParams, this.queryMap); - } /** * // 消息内容中不包含任何关键词 @@ -115,9 +83,29 @@ public String buildUrl(String path, Pair[] queryParams, Map q */ public void send(AbstractRobotRequest request) { + String url; + if (StringUtils.isNotBlank(secret)) { + try { + //https://oapi.dingtalk.com/robot/send?access_token=XXXXXX×tamp=XXX&sign=XXX + long timestamp = System.currentTimeMillis(); + String stringToSign = timestamp + "\n" + secret; + Mac mac = Mac.getInstance("HmacSHA256"); + mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256")); + byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8")); + String sign = URLEncoder.encode(Base64.getEncoder().encodeToString(signData), "UTF-8"); + + url = accessToken + "×tamp=" + timestamp + "&sign=" + sign; + } catch (Exception e) { + log.error("DingRobot sign error", e); + throw new HttpException("DingRobot sign error"); + } + } else { + url = accessToken; + } + //response {"errcode":0,"errmsg":"ok"} - RobotResponse response = post("", request, RobotResponse.class); - if (response == null || response.getErrcode() != 0) { + RobotResponse response = apiJsonClient.post(url, request, RobotResponse.class); + if (response != null && response.getErrcode() != 0) { throw new HttpException(response.getErrmsg(), response.getErrcode()); } } diff --git a/src/main/java/com/admin4j/chatbot/qwx/QyWeiXinChatBot.java b/src/main/java/com/admin4j/chatbot/qwx/QyWeiXinChatBot.java index e6d50b4..d01c866 100644 --- a/src/main/java/com/admin4j/chatbot/qwx/QyWeiXinChatBot.java +++ b/src/main/java/com/admin4j/chatbot/qwx/QyWeiXinChatBot.java @@ -2,7 +2,9 @@ import com.admin4j.chatbot.qwx.core.BotMsg; import com.admin4j.chatbot.qwx.core.msg.TextBotMsg; -import io.github.admin4j.http.util.HttpUtil; +import io.github.admin4j.http.ApiJsonClient; +import io.github.admin4j.http.core.HttpDefaultConfig; +import lombok.Setter; /** * 企业微信群聊机器人 @@ -16,22 +18,25 @@ public class QyWeiXinChatBot { private static final String BASE_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="; private String webhookUrl = ""; + @Setter + private ApiJsonClient apiJsonClient; public QyWeiXinChatBot(String webhookUrl) { setWebhookUrl(webhookUrl); + apiJsonClient = new ApiJsonClient(HttpDefaultConfig.get()); } public void setWebhookUrl(String webhookUrl) { if (webhookUrl.contains("http")) { this.webhookUrl = webhookUrl; } else { - webhookUrl = BASE_URL + webhookUrl; + this.webhookUrl = BASE_URL + webhookUrl; } } public void sendMsg(BotMsg msg) { - HttpUtil.post(webhookUrl, msg); + apiJsonClient.post(webhookUrl, msg); } public void sendTextMsg(String msg) { diff --git a/src/test/java/com/admin4j/chatbot/dingtalk/DingRobotTest.java b/src/test/java/com/admin4j/chatbot/dingtalk/DingRobotTest.java index 9f5ea8b..9fb3ced 100644 --- a/src/test/java/com/admin4j/chatbot/dingtalk/DingRobotTest.java +++ b/src/test/java/com/admin4j/chatbot/dingtalk/DingRobotTest.java @@ -14,8 +14,8 @@ class DingRobotTest { @BeforeAll static void init() { - String assessToken = "d54bd0d41decb444d8e716fa661c03233d10f75559a0041c60148a1de8bb8028"; - String secret = "SEC7f5baedc3bc637942041077a6e5aeb4275cf403ad3c057e75b0ed8921dc26625"; + String assessToken = "61122ec063a414ffc7b91be27e08a207f36601118ff5832cdcb1b6fd5axxx"; + String secret = "SEC3dcd903e34b40ebd5ea87d3846e2fafd4aaff817e069b1b360aca74451fxx"; dingRobot = new DingRobot(assessToken, secret); }