|
| 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 | +} |
0 commit comments