1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
| package pers.niaonao.dingtalkrobot.util;
import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.request.OapiRobotSendRequest ; import com.dingtalk.api.response.OapiRobotSendResponse; import com.taobao.api.ApiException; import lombok.extern.slf4j.Slf4j; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils;
import java.util.Arrays; import java.util.List;
@Slf4j public class RobotHelperUtil {
private static final String ACCESS_TOKEN = "https://oapi.dingtalk.com/robot/send?access_token=36ba0cc82d41d3c6aaef7d2c09c9f14de0727069edbc498b5c9d88edb72db227";
private static final String MSG_TYPE_TEXT = "text"; private static final String MSG_TYPE_LINK = "link"; private static final String MSG_TYPE_MARKDOWN = "markdown"; private static final String MSG_TYPE_ACTION_CARD = "actionCard"; private static final String MSG_TYPE_FEED_CARD = "feedCard";
public static DingTalkClient client = new DefaultDingTalkClient(ACCESS_TOKEN);
public static void sdkDemoJava() { DingTalkClient client = RobotHelperUtil.client; OapiRobotSendRequest request = new OapiRobotSendRequest(); request.setMsgtype("text"); OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text(); text.setContent("测试文本消息"); request.setText(text); OapiRobotSendRequest.At at = new OapiRobotSendRequest.At(); at.setAtMobiles(Arrays.asList("13261303345")); request.setAt(at);
request.setMsgtype("link"); OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link(); link.setMessageUrl("https://www.dingtalk.com/"); link.setPicUrl(""); link.setTitle("时代的火车向前开"); link.setText("这个即将发布的新版本,创始人陈航(花名“无招”)称它为“红树林”。\n" + "而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是“红树林"); request.setLink(link);
request.setMsgtype("markdown"); OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown(); markdown.setTitle("杭州天气"); markdown.setText("#### 杭州天气 @156xxxx8827\n" + "> 9度,西北风1级,空气良89,相对温度73%\n\n" + "> ![screenshot](https://gw.alipayobjects.com/zos/skylark-tools/public/files/84111bbeba74743d2771ed4f062d1f25.png)\n" + "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n"); request.setMarkdown(markdown); try { client.execute(request); } catch (ApiException e) { log.error("[ApiException]: 消息发送演示示例, 异常捕获{}", e.getMessage()); } }
public static OapiRobotSendResponse sendMessageByText(String content, List<String> mobileList, boolean isAtAll) { if (StringUtils.isEmpty(content)) { return null; }
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text(); text.setContent(content); OapiRobotSendRequest request = new OapiRobotSendRequest(); if (!CollectionUtils.isEmpty(mobileList)) { OapiRobotSendRequest.At at = new OapiRobotSendRequest.At(); at.setAtMobiles(mobileList); at.setIsAtAll(isAtAll ? "true" : "false"); request.setAt(at); } request.setMsgtype(RobotHelperUtil.MSG_TYPE_TEXT); request.setText(text);
OapiRobotSendResponse response = new OapiRobotSendResponse(); try { response = RobotHelperUtil.client.execute(request); } catch (ApiException e) { log.error("[发送普通文本消息]: 发送消息失败, 异常捕获{}", e.getMessage()); } return response; }
public static OapiRobotSendResponse sendMessageByLink(String title, String text, String messageUrl, String picUrl) { if (!DataValidUtil.checkNotEmpty(title, text, messageUrl)) { return null; } OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link(); link.setTitle(title); link.setText(text); link.setMessageUrl(messageUrl); link.setPicUrl(picUrl);
OapiRobotSendRequest request = new OapiRobotSendRequest(); request.setMsgtype(RobotHelperUtil.MSG_TYPE_LINK); request.setLink(link);
OapiRobotSendResponse response = new OapiRobotSendResponse(); try { response = RobotHelperUtil.client.execute(request); } catch (ApiException e) { log.error("[发送link 类型消息]: 发送消息失败, 异常捕获{}", e.getMessage()); } return response; }
public static OapiRobotSendResponse sendMessageByMarkdown(String title, String markdownText, List<String> mobileList, boolean isAtAll) { if (!DataValidUtil.checkNotEmpty(title, markdownText)) { return null; } OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown(); markdown.setTitle(title); markdown.setText(markdownText);
OapiRobotSendRequest request = new OapiRobotSendRequest(); request.setMsgtype(RobotHelperUtil.MSG_TYPE_MARKDOWN); request.setMarkdown(markdown); if (!CollectionUtils.isEmpty(mobileList)) { OapiRobotSendRequest.At at = new OapiRobotSendRequest.At(); at.setIsAtAll(isAtAll ? "true" : "false"); at.setAtMobiles(mobileList); request.setAt(at); }
OapiRobotSendResponse response = new OapiRobotSendResponse(); try { response = RobotHelperUtil.client.execute(request); } catch (ApiException e) { log.error("[发送link 类型消息]: 发送消息失败, 异常捕获{}", e.getMessage()); } return response; }
public static OapiRobotSendResponse sendMessageByActionCardSingle(String title, String markdownText, String singleTitle, String singleURL, boolean btnOrientation, boolean hideAvatar) { if (!DataValidUtil.checkNotEmpty(title, markdownText)) { return null; } OapiRobotSendRequest.Actioncard actionCard = new OapiRobotSendRequest.Actioncard(); actionCard.setTitle(title); actionCard.setText(markdownText); actionCard.setSingleTitle(singleTitle); actionCard.setSingleURL(singleURL); actionCard.setBtnOrientation(btnOrientation ? "1" : "0"); actionCard.setHideAvatar(hideAvatar ? "1" : "0");
OapiRobotSendRequest request = new OapiRobotSendRequest(); request.setMsgtype(RobotHelperUtil.MSG_TYPE_ACTION_CARD); request.setActionCard(actionCard); OapiRobotSendResponse response = new OapiRobotSendResponse(); try { response = RobotHelperUtil.client.execute(request); } catch (ApiException e) { log.error("[发送ActionCard 类型消息]: 整体跳转ActionCard类型的发送消息失败, 异常捕获{}", e.getMessage()); } return response; }
public static OapiRobotSendResponse sendMessageByActionCardMulti(String title, String markdownText, List<OapiRobotSendRequest.Btns> btns, boolean btnOrientation, boolean hideAvatar) { if (!DataValidUtil.checkNotEmpty(title, markdownText) || CollectionUtils.isEmpty(btns)) { return null; } OapiRobotSendRequest.Actioncard actionCard = new OapiRobotSendRequest.Actioncard(); actionCard.setTitle(title); actionCard.setText(markdownText); actionCard.setBtnOrientation(btnOrientation ? "1" : "0"); actionCard.setHideAvatar(hideAvatar ? "1" : "0");
actionCard.setBtns(btns);
OapiRobotSendRequest request = new OapiRobotSendRequest(); request.setMsgtype(RobotHelperUtil.MSG_TYPE_ACTION_CARD); request.setActionCard(actionCard); OapiRobotSendResponse response = new OapiRobotSendResponse(); try { response = RobotHelperUtil.client.execute(request); } catch (ApiException e) { log.error("[发送ActionCard 类型消息]: 独立跳转ActionCard类型发送消息失败, 异常捕获{}", e.getMessage()); } return response; }
public static OapiRobotSendResponse sendMessageByFeedCard(List<OapiRobotSendRequest.Links> links) { if (CollectionUtils.isEmpty(links)) { return null; }
OapiRobotSendRequest.Feedcard feedcard = new OapiRobotSendRequest.Feedcard(); feedcard.setLinks(links); OapiRobotSendRequest request = new OapiRobotSendRequest(); request.setMsgtype(RobotHelperUtil.MSG_TYPE_FEED_CARD); request.setFeedCard(feedcard); OapiRobotSendResponse response = new OapiRobotSendResponse(); try { response = RobotHelperUtil.client.execute(request); } catch (ApiException e) { log.error("[发送ActionCard 类型消息]: 独立跳转ActionCard类型发送消息失败, 异常捕获{}", e.getMessage()); } return response; }
}
|