refactor(sj_map_reduce): 重构钉钉消息发送工具类
This commit is contained in:
		
							parent
							
								
									fa635db300
								
							
						
					
					
						commit
						eb6e60447e
					
				
							
								
								
									
										8
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								pom.xml
									
									
									
									
									
								
							| @ -22,11 +22,9 @@ | |||||||
|         <maven.compiler.source>17</maven.compiler.source> |         <maven.compiler.source>17</maven.compiler.source> | ||||||
|         <maven.compiler.target>17</maven.compiler.target> |         <maven.compiler.target>17</maven.compiler.target> | ||||||
|         <revision>1.1.0-beta2</revision> |         <revision>1.1.0-beta2</revision> | ||||||
|         <dingding-talk.version>1.0.0</dingding-talk.version> |  | ||||||
|         <netty-all.version>4.1.94.Final</netty-all.version> |         <netty-all.version>4.1.94.Final</netty-all.version> | ||||||
|         <hutool-all.version>5.8.25</hutool-all.version> |         <hutool-all.version>5.8.25</hutool-all.version> | ||||||
|         <mybatis-plus.version>3.5.7</mybatis-plus.version> |         <mybatis-plus.version>3.5.7</mybatis-plus.version> | ||||||
|         <alibaba-dingtalk.version>2.0.0</alibaba-dingtalk.version> |  | ||||||
|         <guava-retrying.version>2.0.0</guava-retrying.version> |         <guava-retrying.version>2.0.0</guava-retrying.version> | ||||||
|         <tinylog.version>1.3.6</tinylog.version> |         <tinylog.version>1.3.6</tinylog.version> | ||||||
|         <tinylog2.version>2.6.2</tinylog2.version> |         <tinylog2.version>2.6.2</tinylog2.version> | ||||||
| @ -136,12 +134,6 @@ | |||||||
|                 <artifactId>snail-job-client-job-core</artifactId> |                 <artifactId>snail-job-client-job-core</artifactId> | ||||||
|                 <version>${revision}</version> |                 <version>${revision}</version> | ||||||
|             </dependency> |             </dependency> | ||||||
|             <!-- dingding talk sdk --> |  | ||||||
|             <dependency> |  | ||||||
|                 <groupId>com.aliyun</groupId> |  | ||||||
|                 <artifactId>alibaba-dingtalk-service-sdk</artifactId> |  | ||||||
|                 <version>${alibaba-dingtalk.version}</version> |  | ||||||
|             </dependency> |  | ||||||
|             <dependency> |             <dependency> | ||||||
|                 <groupId>com.baomidou</groupId> |                 <groupId>com.baomidou</groupId> | ||||||
|                 <artifactId>mybatis-plus-boot-starter</artifactId> |                 <artifactId>mybatis-plus-boot-starter</artifactId> | ||||||
|  | |||||||
| @ -31,20 +31,6 @@ | |||||||
|             <artifactId>lombok</artifactId> |             <artifactId>lombok</artifactId> | ||||||
|             <optional>true</optional> |             <optional>true</optional> | ||||||
|         </dependency> |         </dependency> | ||||||
|         <dependency> |  | ||||||
|             <groupId>com.aliyun</groupId> |  | ||||||
|             <artifactId>alibaba-dingtalk-service-sdk</artifactId> |  | ||||||
|             <exclusions> |  | ||||||
|                 <exclusion> |  | ||||||
|                     <artifactId>log4j</artifactId> |  | ||||||
|                     <groupId>log4j</groupId> |  | ||||||
|                 </exclusion> |  | ||||||
|                 <exclusion> |  | ||||||
|                     <artifactId>commons-logging</artifactId> |  | ||||||
|                     <groupId>commons-logging</groupId> |  | ||||||
|                 </exclusion> |  | ||||||
|             </exclusions> |  | ||||||
|         </dependency> |  | ||||||
|         <dependency> |         <dependency> | ||||||
|             <groupId>commons-configuration</groupId> |             <groupId>commons-configuration</groupId> | ||||||
|             <artifactId>commons-configuration</artifactId> |             <artifactId>commons-configuration</artifactId> | ||||||
|  | |||||||
| @ -2,15 +2,17 @@ package com.aizuda.snailjob.common.core.util; | |||||||
| 
 | 
 | ||||||
| import cn.hutool.core.collection.CollUtil; | import cn.hutool.core.collection.CollUtil; | ||||||
| import cn.hutool.core.util.StrUtil; | import cn.hutool.core.util.StrUtil; | ||||||
|  | import cn.hutool.http.HttpRequest; | ||||||
|  | import cn.hutool.http.HttpResponse; | ||||||
| import com.aizuda.snailjob.common.core.constant.SystemConstants; | import com.aizuda.snailjob.common.core.constant.SystemConstants; | ||||||
| import com.aizuda.snailjob.common.log.SnailJobLog; | import com.aizuda.snailjob.common.log.SnailJobLog; | ||||||
| import com.dingtalk.api.DefaultDingTalkClient; | import com.fasterxml.jackson.databind.JsonNode; | ||||||
| import com.dingtalk.api.DingTalkClient; |  | ||||||
| import com.dingtalk.api.request.OapiRobotSendRequest; |  | ||||||
| import lombok.extern.slf4j.Slf4j; | import lombok.extern.slf4j.Slf4j; | ||||||
| 
 | 
 | ||||||
| import java.text.MessageFormat; | import java.text.MessageFormat; | ||||||
|  | import java.util.HashMap; | ||||||
| import java.util.List; | import java.util.List; | ||||||
|  | import java.util.Map; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * @author: opensnail |  * @author: opensnail | ||||||
| @ -22,28 +24,29 @@ public class DingDingUtils { | |||||||
|     public static final String atLabel = "@{0}"; |     public static final String atLabel = "@{0}"; | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * 组装OapiRobotSendRequest |      * 组装 DingTalkRequest | ||||||
|      * |      * | ||||||
|      * @param title |      * @param title 标题 | ||||||
|      * @param text |      * @param text  内容 | ||||||
|      * @return |      * @return DingTalkRequest | ||||||
|      */ |      */ | ||||||
|     public static OapiRobotSendRequest buildSendRequest(String title, String text, List<String> ats) { |     public static String buildSendRequest(String title, String text, List<String> ats) { | ||||||
|         OapiRobotSendRequest request = new OapiRobotSendRequest(); |         Map<String, Object> message = new HashMap<>(); | ||||||
|         request.setMsgtype("markdown"); |         message.put("msgtype", "markdown"); | ||||||
|         OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown(); |         Map<String, Object> markdown = new HashMap<>(); | ||||||
|         markdown.setTitle(title); |         markdown.put("text", StrUtil.sub(getAtText(ats, text, atLabel), 0, 4000)); | ||||||
|         // 防止文本过长钉钉限流,目前最大为4000 |         markdown.put("title", title); | ||||||
|         markdown.setText(StrUtil.sub(getAtText(ats, text, atLabel), 0, 4000)); |         message.put("markdown", markdown); | ||||||
|         request.setMarkdown(markdown); |  | ||||||
| 
 | 
 | ||||||
|         OapiRobotSendRequest.At at = new OapiRobotSendRequest.At(); |         // 处理提到的人 | ||||||
|         at.setAtMobiles(ats); |         Map<String, Object> at = new HashMap<>(); | ||||||
|         request.setAt(at); |         at.put("atMobiles", ats); | ||||||
|  |         at.put("isAtAll", true); | ||||||
|         if (CollUtil.isNotEmpty(ats)) { |         if (CollUtil.isNotEmpty(ats)) { | ||||||
|             at.setIsAtAll(ats.stream().map(String::toLowerCase).anyMatch(SystemConstants.AT_ALL::equals)); |             at.put("isAtAll", ats.stream().map(String::toLowerCase).anyMatch(SystemConstants.AT_ALL::equals)); | ||||||
|         } |         } | ||||||
|         return request; |         message.put("at", at); | ||||||
|  |         return JsonUtil.toJsonString(message); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public static String getAtText(List<String> ats, String text, String atLabel) { |     public static String getAtText(List<String> ats, String text, String atLabel) { | ||||||
| @ -57,18 +60,28 @@ public class DingDingUtils { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * @param request |      * @param request DingTalkRequest | ||||||
|      */ |      */ | ||||||
|     public static boolean sendMessage(OapiRobotSendRequest request, String url) { |     public static boolean sendMessage(String request, String url) { | ||||||
| 
 | 
 | ||||||
|         try { |         try { | ||||||
|             if (StrUtil.isBlank(url)) { |             if (StrUtil.isBlank(url)) { | ||||||
|                 return false; |                 return false; | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             DingTalkClient client = new DefaultDingTalkClient(url); |             // 发送POST请求 | ||||||
|             client.execute(request); |             HttpResponse response = HttpRequest.post(url) | ||||||
|  |                     .headerMap(getHeaders(), true) | ||||||
|  |                     .body(request) | ||||||
|  |                     .execute(); | ||||||
| 
 | 
 | ||||||
|  |             String body = response.body(); | ||||||
|  |             JsonNode bodyJson = JsonUtil.toJson(body); | ||||||
|  |             int errCode = bodyJson.get("errcode").asInt(); | ||||||
|  |             if (errCode != 0) { | ||||||
|  |                 SnailJobLog.LOCAL.error("dingDingProcessNotify: 钉钉发送失败, 错误码:{}, 错误信息:{}", errCode, bodyJson.get("errmsg").asText()); | ||||||
|  |                 return false; | ||||||
|  |             } | ||||||
|             return true; |             return true; | ||||||
|         } catch (Exception e) { |         } catch (Exception e) { | ||||||
|             SnailJobLog.LOCAL.error("dingDingProcessNotify", e); |             SnailJobLog.LOCAL.error("dingDingProcessNotify", e); | ||||||
| @ -77,4 +90,10 @@ public class DingDingUtils { | |||||||
|         return false; |         return false; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     public static Map<String, String> getHeaders() { | ||||||
|  |         Map<String, String> headers = new HashMap<>(); | ||||||
|  |         headers.put("Content-Type", "application/json"); | ||||||
|  |         return headers; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 xlsea
						xlsea