diff --git a/app-stream-api/pom.xml b/app-stream-api/pom.xml index 9558aac..99fd628 100644 --- a/app-stream-api/pom.xml +++ b/app-stream-api/pom.xml @@ -5,7 +5,7 @@ open-app-stream-client com.dingtalk.open - 1.1.0 + 1.2.0 ../pom.xml 4.0.0 diff --git a/app-stream-api/src/main/java/com/dingtalk/open/app/api/chatbot/BotReplier.java b/app-stream-api/src/main/java/com/dingtalk/open/app/api/chatbot/BotReplier.java new file mode 100644 index 0000000..73ed55b --- /dev/null +++ b/app-stream-api/src/main/java/com/dingtalk/open/app/api/chatbot/BotReplier.java @@ -0,0 +1,113 @@ +package com.dingtalk.open.app.api.chatbot; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.serializer.SerializerFeature; +import com.dingtalk.open.app.api.open.http.HttpConstants; +import com.dingtalk.open.app.api.util.IoUtils; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class BotReplier { + private final String webhook; + private final int timeout = 60000; + + public BotReplier(String webhook) { + this.webhook = webhook; + } + + public static BotReplier fromWebhook(String webhook) { + return new BotReplier(webhook); + } + + public String replyText(String text) throws IOException { + return replyText(text, null); + } + public String replyMarkdown(String title, String text) throws IOException { + return replyMarkdown(title, text, null); + } + + public String replyText(String text, List atUserIds) throws IOException { + final HttpURLConnection connection = getHttpURLConnection(); + + Map textContent = new HashMap<>(); + textContent.put("content", text); + Map request = new HashMap<>(); + request.put("msgtype", "text"); + request.put("text", textContent); + if (atUserIds != null && atUserIds.size() > 0) { + Map atContent = new HashMap<>(); + atContent.put("atUserIds", Collections.singletonList("")); + request.put("at", atContent); + } + connection.getOutputStream().write(JSON.toJSONBytes(request, SerializerFeature.WriteEnumUsingToString)); + connection.getOutputStream().flush(); + if (connection.getResponseCode() == HttpConstants.STATUS_OK) { + try { + byte[] bytes = IoUtils.readAll(connection.getInputStream()); + return new String(bytes, StandardCharsets.UTF_8); + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } else { + try { + byte[] bytes = IoUtils.readAll(connection.getErrorStream()); + throw new IOException(String.format("status=%s, msg=%s", connection.getResponseCode(), bytes != null ? new String(bytes) : "")); + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } + } + + + public String replyMarkdown(String title, String text, List atUserIds) throws IOException { + final HttpURLConnection connection = getHttpURLConnection(); + Map markdownContent = new HashMap<>(); + markdownContent.put("title", title); + markdownContent.put("text", text); + Map request = new HashMap<>(); + request.put("msgtype", "markdown"); + request.put("markdown", markdownContent); + if (atUserIds != null && atUserIds.size() > 0) { + Map atContent = new HashMap<>(); + atContent.put("atUserIds", Collections.singletonList("")); + request.put("at", atContent); + } + connection.getOutputStream().write(JSON.toJSONBytes(request, SerializerFeature.WriteEnumUsingToString)); + connection.getOutputStream().flush(); + if (connection.getResponseCode() == HttpConstants.STATUS_OK) { + try { + byte[] bytes = IoUtils.readAll(connection.getInputStream()); + return new String(bytes, StandardCharsets.UTF_8); + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } else { + try { + byte[] bytes = IoUtils.readAll(connection.getErrorStream()); + throw new IOException(String.format("status=%s, msg=%s", connection.getResponseCode(), bytes != null ? new String(bytes) : "")); + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } + } + + private HttpURLConnection getHttpURLConnection() throws IOException { + final HttpURLConnection connection = (HttpURLConnection) new URL(webhook).openConnection(); + connection.setRequestMethod(HttpConstants.METHOD_POST); + connection.setReadTimeout(this.timeout); + connection.setConnectTimeout(this.timeout); + connection.setRequestProperty(HttpConstants.HEADER_CONTENT_TYPE, HttpConstants.CONTENT_TYPE_APPLICATION_JSON); + connection.setRequestProperty(HttpConstants.HEADER_ACCEPT, "application/json"); + connection.setDoInput(true); + connection.setDoOutput(true); + connection.connect(); + return connection; + } +} diff --git a/app-stream-client/pom.xml b/app-stream-client/pom.xml index 597fe03..d070c74 100644 --- a/app-stream-client/pom.xml +++ b/app-stream-client/pom.xml @@ -4,13 +4,13 @@ com.dingtalk.open open-app-stream-client - 1.1.0 + 1.2.0 ../pom.xml app-stream-client jar - 1.1.0 + 1.2.0 app-stream-client diff --git a/app-stream-network/app-stream-network-api/pom.xml b/app-stream-network/app-stream-network-api/pom.xml index 3f717e6..ecda205 100644 --- a/app-stream-network/app-stream-network-api/pom.xml +++ b/app-stream-network/app-stream-network-api/pom.xml @@ -4,12 +4,12 @@ com.dingtalk.open app-stream-network - 1.1.0 + 1.2.0 ../pom.xml app-stream-network-api - 1.1.0 + 1.2.0 jar app-stream-network-api diff --git a/app-stream-network/app-stream-network-core/pom.xml b/app-stream-network/app-stream-network-core/pom.xml index 6340eea..89fbb0d 100644 --- a/app-stream-network/app-stream-network-core/pom.xml +++ b/app-stream-network/app-stream-network-core/pom.xml @@ -4,7 +4,7 @@ com.dingtalk.open app-stream-network - 1.1.0 + 1.2.0 ../pom.xml diff --git a/app-stream-network/app-stream-network-rsocket/pom.xml b/app-stream-network/app-stream-network-rsocket/pom.xml index dd9f850..465aa81 100644 --- a/app-stream-network/app-stream-network-rsocket/pom.xml +++ b/app-stream-network/app-stream-network-rsocket/pom.xml @@ -4,7 +4,7 @@ com.dingtalk.open app-stream-network - 1.1.0 + 1.2.0 ../pom.xml diff --git a/app-stream-network/app-stream-network-ws/pom.xml b/app-stream-network/app-stream-network-ws/pom.xml index 981527e..fac5b3d 100644 --- a/app-stream-network/app-stream-network-ws/pom.xml +++ b/app-stream-network/app-stream-network-ws/pom.xml @@ -4,7 +4,7 @@ com.dingtalk.open app-stream-network - 1.1.0 + 1.2.0 ../pom.xml diff --git a/app-stream-network/pom.xml b/app-stream-network/pom.xml index 136bf1e..7f72e23 100644 --- a/app-stream-network/pom.xml +++ b/app-stream-network/pom.xml @@ -5,7 +5,7 @@ open-app-stream-client com.dingtalk.open - 1.1.0 + 1.2.0 ../pom.xml pom diff --git a/app-stream-protocol/pom.xml b/app-stream-protocol/pom.xml index 84468dd..2b2e3c4 100644 --- a/app-stream-protocol/pom.xml +++ b/app-stream-protocol/pom.xml @@ -6,7 +6,7 @@ com.dingtalk.open open-app-stream-client - 1.1.0 + 1.2.0 app-stream-protocol diff --git a/dingtalk-stream/pom.xml b/dingtalk-stream/pom.xml index bcffbc8..1d499ad 100644 --- a/dingtalk-stream/pom.xml +++ b/dingtalk-stream/pom.xml @@ -4,13 +4,13 @@ com.dingtalk.open open-app-stream-client - 1.1.0 + 1.2.0 ../pom.xml dingtalk-stream jar - 1.1.0 + 1.2.0 app-stream-client diff --git a/pom.xml b/pom.xml index de9cdb0..1508295 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.dingtalk.open open-app-stream-client pom - 1.1.0 + 1.2.0 app-stream-client app-stream-api diff --git a/version.sh b/version.sh index f2c3f5d..69b1fca 100755 --- a/version.sh +++ b/version.sh @@ -1,3 +1,3 @@ #!/usr/bin/env bash -mvn versions:set -DnewVersion=1.1.0 +mvn versions:set -DnewVersion=1.2.0