Skip to content

Commit cbdddfc

Browse files
chzealot飞隐
andauthored
feat: 支持快速回复文本消息和 Markdown 消息 (#6)
* feat: 支持快速回复文本消息和 Markdown 消息 (#6) --------- Signed-off-by: Ke Jie <[email protected]> Co-authored-by: 飞隐 <[email protected]>
1 parent 3c7b409 commit cbdddfc

File tree

12 files changed

+127
-14
lines changed

12 files changed

+127
-14
lines changed

app-stream-api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>open-app-stream-client</artifactId>
77
<groupId>com.dingtalk.open</groupId>
8-
<version>1.1.0</version>
8+
<version>1.2.0</version>
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111
<modelVersion>4.0.0</modelVersion>
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.dingtalk.open.app.api.chatbot;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.alibaba.fastjson.serializer.SerializerFeature;
5+
import com.dingtalk.open.app.api.open.http.HttpConstants;
6+
import com.dingtalk.open.app.api.util.IoUtils;
7+
8+
import java.io.IOException;
9+
import java.net.HttpURLConnection;
10+
import java.net.URL;
11+
import java.nio.charset.StandardCharsets;
12+
import java.util.Collections;
13+
import java.util.HashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
public class BotReplier {
18+
private final String webhook;
19+
private final int timeout = 60000;
20+
21+
public BotReplier(String webhook) {
22+
this.webhook = webhook;
23+
}
24+
25+
public static BotReplier fromWebhook(String webhook) {
26+
return new BotReplier(webhook);
27+
}
28+
29+
public String replyText(String text) throws IOException {
30+
return replyText(text, null);
31+
}
32+
public String replyMarkdown(String title, String text) throws IOException {
33+
return replyMarkdown(title, text, null);
34+
}
35+
36+
public String replyText(String text, List<String> atUserIds) throws IOException {
37+
final HttpURLConnection connection = getHttpURLConnection();
38+
39+
Map<String, Object> textContent = new HashMap<>();
40+
textContent.put("content", text);
41+
Map<String, Object> request = new HashMap<>();
42+
request.put("msgtype", "text");
43+
request.put("text", textContent);
44+
if (atUserIds != null && atUserIds.size() > 0) {
45+
Map<String, Object> atContent = new HashMap<>();
46+
atContent.put("atUserIds", Collections.singletonList(""));
47+
request.put("at", atContent);
48+
}
49+
connection.getOutputStream().write(JSON.toJSONBytes(request, SerializerFeature.WriteEnumUsingToString));
50+
connection.getOutputStream().flush();
51+
if (connection.getResponseCode() == HttpConstants.STATUS_OK) {
52+
try {
53+
byte[] bytes = IoUtils.readAll(connection.getInputStream());
54+
return new String(bytes, StandardCharsets.UTF_8);
55+
} catch (Exception e) {
56+
throw new IOException(e.getMessage());
57+
}
58+
} else {
59+
try {
60+
byte[] bytes = IoUtils.readAll(connection.getErrorStream());
61+
throw new IOException(String.format("status=%s, msg=%s", connection.getResponseCode(), bytes != null ? new String(bytes) : ""));
62+
} catch (Exception e) {
63+
throw new IOException(e.getMessage());
64+
}
65+
}
66+
}
67+
68+
69+
public String replyMarkdown(String title, String text, List<String> atUserIds) throws IOException {
70+
final HttpURLConnection connection = getHttpURLConnection();
71+
Map<String, Object> markdownContent = new HashMap<>();
72+
markdownContent.put("title", title);
73+
markdownContent.put("text", text);
74+
Map<String, Object> request = new HashMap<>();
75+
request.put("msgtype", "markdown");
76+
request.put("markdown", markdownContent);
77+
if (atUserIds != null && atUserIds.size() > 0) {
78+
Map<String, Object> atContent = new HashMap<>();
79+
atContent.put("atUserIds", Collections.singletonList(""));
80+
request.put("at", atContent);
81+
}
82+
connection.getOutputStream().write(JSON.toJSONBytes(request, SerializerFeature.WriteEnumUsingToString));
83+
connection.getOutputStream().flush();
84+
if (connection.getResponseCode() == HttpConstants.STATUS_OK) {
85+
try {
86+
byte[] bytes = IoUtils.readAll(connection.getInputStream());
87+
return new String(bytes, StandardCharsets.UTF_8);
88+
} catch (Exception e) {
89+
throw new IOException(e.getMessage());
90+
}
91+
} else {
92+
try {
93+
byte[] bytes = IoUtils.readAll(connection.getErrorStream());
94+
throw new IOException(String.format("status=%s, msg=%s", connection.getResponseCode(), bytes != null ? new String(bytes) : ""));
95+
} catch (Exception e) {
96+
throw new IOException(e.getMessage());
97+
}
98+
}
99+
}
100+
101+
private HttpURLConnection getHttpURLConnection() throws IOException {
102+
final HttpURLConnection connection = (HttpURLConnection) new URL(webhook).openConnection();
103+
connection.setRequestMethod(HttpConstants.METHOD_POST);
104+
connection.setReadTimeout(this.timeout);
105+
connection.setConnectTimeout(this.timeout);
106+
connection.setRequestProperty(HttpConstants.HEADER_CONTENT_TYPE, HttpConstants.CONTENT_TYPE_APPLICATION_JSON);
107+
connection.setRequestProperty(HttpConstants.HEADER_ACCEPT, "application/json");
108+
connection.setDoInput(true);
109+
connection.setDoOutput(true);
110+
connection.connect();
111+
return connection;
112+
}
113+
}

app-stream-client/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
<parent>
55
<groupId>com.dingtalk.open</groupId>
66
<artifactId>open-app-stream-client</artifactId>
7-
<version>1.1.0</version>
7+
<version>1.2.0</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010

1111
<artifactId>app-stream-client</artifactId>
1212
<packaging>jar</packaging>
13-
<version>1.1.0</version>
13+
<version>1.2.0</version>
1414
<name>app-stream-client</name>
1515

1616
<dependencies>

app-stream-network/app-stream-network-api/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
<parent>
55
<groupId>com.dingtalk.open</groupId>
66
<artifactId>app-stream-network</artifactId>
7-
<version>1.1.0</version>
7+
<version>1.2.0</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010

1111
<artifactId>app-stream-network-api</artifactId>
12-
<version>1.1.0</version>
12+
<version>1.2.0</version>
1313
<packaging>jar</packaging>
1414

1515
<name>app-stream-network-api</name>

app-stream-network/app-stream-network-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>com.dingtalk.open</groupId>
66
<artifactId>app-stream-network</artifactId>
7-
<version>1.1.0</version>
7+
<version>1.2.0</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010

app-stream-network/app-stream-network-rsocket/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>com.dingtalk.open</groupId>
66
<artifactId>app-stream-network</artifactId>
7-
<version>1.1.0</version>
7+
<version>1.2.0</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010

app-stream-network/app-stream-network-ws/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>com.dingtalk.open</groupId>
66
<artifactId>app-stream-network</artifactId>
7-
<version>1.1.0</version>
7+
<version>1.2.0</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010

app-stream-network/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>open-app-stream-client</artifactId>
77
<groupId>com.dingtalk.open</groupId>
8-
<version>1.1.0</version>
8+
<version>1.2.0</version>
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111
<packaging>pom</packaging>

app-stream-protocol/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.dingtalk.open</groupId>
88
<artifactId>open-app-stream-client</artifactId>
9-
<version>1.1.0</version>
9+
<version>1.2.0</version>
1010
</parent>
1111

1212
<artifactId>app-stream-protocol</artifactId>

dingtalk-stream/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
<parent>
55
<groupId>com.dingtalk.open</groupId>
66
<artifactId>open-app-stream-client</artifactId>
7-
<version>1.1.0</version>
7+
<version>1.2.0</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010

1111
<artifactId>dingtalk-stream</artifactId>
1212
<packaging>jar</packaging>
13-
<version>1.1.0</version>
13+
<version>1.2.0</version>
1414
<name>app-stream-client</name>
1515

1616
<dependencies>

0 commit comments

Comments
 (0)