Skip to content

Commit

Permalink
重构钉钉群聊机器人
Browse files Browse the repository at this point in the history
  • Loading branch information
andanyoung committed May 16, 2024
1 parent d2d4ef1 commit af9309a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 67 deletions.
44 changes: 31 additions & 13 deletions .github/workflows/maven-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 的密码
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<!-- </parent>-->
<groupId>com.admin4j</groupId>
<artifactId>chat-bot</artifactId>
<version>0.1.0</version>
<version>0.10.0</version>
<name>chat-bot</name>
<description>chat-bot 群聊机器人支持钉钉等</description>
<url>https://github.com/admin4j</url>
Expand All @@ -24,7 +24,7 @@
<dependency>
<groupId>io.github.admin4j</groupId>
<artifactId>http</artifactId>
<version>0.9.1</version>
<version>0.9.2</version>
</dependency>

<dependency>
Expand Down
82 changes: 35 additions & 47 deletions src/main/java/com/admin4j/chatbot/dingtalk/DingRobot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -14,21 +15,23 @@
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
* @since 2022/5/11 11:43
*/
@Data
@Slf4j
public class DingRobot extends ApiJsonClient {
public class DingRobot {

private Map<String, Object> queryMap;
//private Map<String, Object> queryMap;
private String accessToken;
private String secret;
@Setter
private ApiJsonClient apiJsonClient;

/**
* 初始化
Expand All @@ -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<String, Object> 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);
}

/**
* // 消息内容中不包含任何关键词
Expand Down Expand Up @@ -115,9 +83,29 @@ public String buildUrl(String path, Pair<?>[] queryParams, Map<String, Object> q
*/
public void send(AbstractRobotRequest request) {

String url;
if (StringUtils.isNotBlank(secret)) {
try {
//https://oapi.dingtalk.com/robot/send?access_token=XXXXXX&timestamp=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 + "&timestamp=" + 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());
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/admin4j/chatbot/qwx/QyWeiXinChatBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* 企业微信群聊机器人
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/admin4j/chatbot/dingtalk/DingRobotTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit af9309a

Please sign in to comment.