Skip to content

Commit

Permalink
feat: 支持其他格式消息类型
Browse files Browse the repository at this point in the history
  • Loading branch information
AnswerAIL committed Jan 10, 2021
1 parent 571cd15 commit 5af1205
Show file tree
Hide file tree
Showing 27 changed files with 933 additions and 118 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Maven Central](https://img.shields.io/maven-central/v/com.github.answerail/dinger-spring-boot-starter)](https://mvnrepository.com/artifact/com.github.answerail/dingtalk-spring-boot-starter)
[![GitHub stars](https://img.shields.io/github/stars/AnswerAIL/dingtalk-spring-boot-starter.svg?style=social)](https://github.com/AnswerAIL/dingtalk-spring-boot-starter)
[![Gitee stars](https://gitee.com/jaemon/dingtalk-spring-boot-starter/badge/star.svg?theme=dark)](https://gitee.com/jaemon/dingtalk-spring-boot-starter)
![JDK](https://img.shields.io/badge/JDK-%3E=1.8-green?logo=appveyor)
![JDK](https://img.shields.io/badge/JDK-1.8+-green?logo=appveyor)
![SpringBoot](https://img.shields.io/badge/springboot-1.x%20&%202.x-green?logo=appveyor)


Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.github.answerail</groupId>
<artifactId>dinger-spring-boot-starter</artifactId>
<packaging>jar</packaging>
<version>1.0.1</version>
<version>1.1.0</version>

<name>dinger-spring-boot-starter</name>
<description>Dinger-SpringBoot集成钉钉/企业微信群机器人实现消息通知中间件</description>
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/github/jaemon/dinger/DingerSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public interface DingerSender {


/**
* 发送预警消息到钉钉
* 发送消息到指定群
*
* <pre>
* 使用配置的默认钉钉机器人, {@link DingerProperties#getDefaultDinger()}
Expand All @@ -47,7 +47,7 @@ public interface DingerSender {
DingerResponse send(MessageSubType messageSubType, DingerRequest request);

/**
* 发送预警消息到钉钉
* 发送消息到指定群
*
* @param dingerType
* Dinger类型 {@link DingerType}
Expand All @@ -59,5 +59,4 @@ public interface DingerSender {
* 响应报文
* */
DingerResponse send(DingerType dingerType, MessageSubType messageSubType, DingerRequest request);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.github.jaemon.dinger.core;

import com.github.jaemon.dinger.constant.DingerConstant;
import com.github.jaemon.dinger.core.annatations.DingerImageText;
import com.github.jaemon.dinger.core.annatations.DingerLink;
import com.github.jaemon.dinger.core.annatations.DingerScan;
import com.github.jaemon.dinger.core.annatations.Parameter;
import com.github.jaemon.dinger.core.entity.enums.DingerDefinitionType;
Expand All @@ -36,8 +38,9 @@
import java.util.List;

import static com.github.jaemon.dinger.core.AbstractDingerDefinitionResolver.Container.INSTANCE;
import static com.github.jaemon.dinger.core.entity.enums.ExceptionEnum.DINGER_REPEATED_EXCEPTION;
import static com.github.jaemon.dinger.core.entity.enums.ExceptionEnum.MULTI_DINGER_SCAN_ERROR;
import static com.github.jaemon.dinger.core.entity.enums.ExceptionEnum.*;
import static com.github.jaemon.dinger.utils.DingerUtils.methodParamsGenericType;
import static com.github.jaemon.dinger.utils.DingerUtils.methodParamsType;

/**
* AbstractDingerDefinitionResolver
Expand Down Expand Up @@ -183,21 +186,22 @@ protected List<Class<?>> dingerAnnotationResolver() throws Exception {
* dingerDefinitionKey
* @param dingerConfiguration
* Dinger层配置DingerConfig
* @param methodParams
* 方法参数信息
* @param dingerMethod
* 方法参数信息和方法中的泛型信息
*
*/
void registerDingerDefinition(
String dingerName, Object source,
String dingerDefinitionKey,
DingerConfig dingerConfiguration,
String[] methodParams
DingerMethod dingerMethod
) {
boolean debugEnabled = log.isDebugEnabled();
for (DingerType dingerType : enabledDingerTypes) {
DingerConfig defaultDingerConfig = defaultDingerConfigs.get(dingerType);
if (dingerConfiguration == null) {
if (debugEnabled) {
log.info("dinger={} not open and skip the corresponding dinger registration.", dingerType);
log.debug("dinger={} not open and skip the corresponding dinger registration.", dingerType);
}
continue;
}
Expand All @@ -206,7 +210,11 @@ void registerDingerDefinition(
Class<? extends DingerDefinitionGenerator> dingerDefinitionGeneratorClass =
dingerDefinitionGeneratorMap.get(key);
if (dingerDefinitionGeneratorClass == null) {
throw new DingerException(ExceptionEnum.DINGERDEFINITIONTYPE_UNDEFINED_KEY, key);
// throw new DingerException(ExceptionEnum.DINGERDEFINITIONTYPE_UNDEFINED_KEY, key);
if (debugEnabled) {
log.debug("当前key=%s在DingerDefinitionType中没定义", key);
}
continue;
}

DingerDefinitionGenerator dingerDefinitionGenerator = DingerDefinitionGeneratorFactory.get(
Expand All @@ -225,7 +233,12 @@ void registerDingerDefinition(
if (INSTANCE.contains(keyName)) {
throw new DingerException(DINGER_REPEATED_EXCEPTION, keyName);
}
dingerDefinition.setMethodParams(methodParams);

if (dingerMethod.check()) {
throw new DingerException(METHOD_DEFINITION_EXCEPTION, dingerMethod.methodName);
}
dingerDefinition.setMethodParams(dingerMethod.methodParams);
dingerDefinition.setGenericIndex(dingerMethod.paramTypes);

// DingerConfig Priority: `@DingerText | @DingerMarkdown | XML` > `@DingerConfiguration` > `***.yml | ***.properties`
dingerDefinition.dingerConfig()
Expand All @@ -245,15 +258,28 @@ void registerDingerDefinition(
* @param dingerClass
* Dinger接口层类
* @return
* 当前Dinger接口定义的方法的参数信息
* 当前Dinger接口定义的方法的参数信息和泛型信息
*/
protected Map<String, String[]> dingerClassMethods(Class<?> dingerClass) {
Method[] declaredMethods = dingerClass.getDeclaredMethods();
Map<String, String[]> dingerMethodParams = new HashMap<>();
for (Method declaredMethod : declaredMethods) {
String methodName = declaredMethod.getName();
String[] methodParams = methodParams(declaredMethod);
dingerMethodParams.put(methodName, methodParams);
protected Map<String, DingerMethod> dingerClassMethods(Class<?> dingerClass) {
Method[] methods = dingerClass.getMethods();
Map<String, DingerMethod> dingerMethodParams = new HashMap<>();
for (Method method : methods) {
String methodName = method.getName();
String methodAllName = dingerClass.getSimpleName() + DingerConstant.SPOT_SEPERATOR + methodName;
int[] paramTypes = null;
if (method.isAnnotationPresent(DingerImageText.class)) {
paramTypes = methodParamsGenericType(method, DingerImageText.clazz);
if (paramTypes.length != 1) {
throw new DingerException(IMAGETEXT_METHOD_PARAM_EXCEPTION, methodAllName);
}
} else if (method.isAnnotationPresent(DingerLink.class)) {
paramTypes = methodParamsType(method, DingerLink.clazz);
if (paramTypes.length != 1) {
throw new DingerException(LINK_METHOD_PARAM_EXCEPTION, methodAllName);
}
}
String[] methodParams = methodParams(method);
dingerMethodParams.put(methodName, new DingerMethod(methodAllName, methodParams, paramTypes));
}
return dingerMethodParams;
}
Expand Down Expand Up @@ -285,6 +311,33 @@ protected String[] methodParams(Method method) {
return params;
}

static class DingerMethod {
String methodName;
String[] methodParams;
int[] paramTypes;

public DingerMethod(String methodName, String[] methodParams, int[] paramTypes) {
this.methodName = methodName;
this.methodParams = methodParams;
this.paramTypes = paramTypes;
}

boolean check() {
if (paramTypes == null) {
return false;
}

int length = this.methodParams.length;
for (int index : paramTypes) {
if (index >= length) {
return true;
}
}
return false;
}

}

/**
* Container for DingerDefinition
*/
Expand All @@ -293,7 +346,7 @@ protected enum Container {
private Map<String, DingerDefinition> container;

Container() {
this.container = new HashMap<>(256);
this.container = new HashMap<>(128);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public class DefaultDingerDefinition implements DingerDefinition {
private DingerType dingerType;
private MessageMainType messageMainType;
private MessageSubType messageSubType;
/** dinger方法的参数信息 */
private String[] methodParams;
/** dinger方法的泛型信息 */
private int[] genericIndex;

@Override
public String dingerName() {
Expand Down Expand Up @@ -110,11 +113,29 @@ public void setMessageSubType(MessageSubType messageSubType) {

@Override
public String[] methodParams() {
return methodParams == null ? new String[0] : methodParams;
return methodParams;
}

@Override
public void setMethodParams(String[] methodParams) {
if (methodParams == null) {
this.methodParams = new String[0];
return;
}
this.methodParams = methodParams;
}

@Override
public int[] genericIndex() {
return genericIndex;
}

@Override
public void setGenericIndex(int[] genericIndex) {
if (genericIndex == null) {
this.genericIndex = new int[0];
return;
}
this.genericIndex = genericIndex;
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/github/jaemon/dinger/core/DingerDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,20 @@ public interface DingerDefinition {
*/
void setMethodParams(String[] methodParams);

/**
* genericIndex
*
* @return
* genericIndex
*/
int[] genericIndex();

/**
* setGenericIndex
*
* @param genericIndex
* genericIndex
*/
void setGenericIndex(int[] genericIndex);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
*/
package com.github.jaemon.dinger.core;

import com.github.jaemon.dinger.core.annatations.DingerTokenId;
import com.github.jaemon.dinger.core.annatations.DingerMarkdown;
import com.github.jaemon.dinger.core.annatations.DingerText;
import com.github.jaemon.dinger.core.annatations.*;
import com.github.jaemon.dinger.core.entity.DingerRequest;
import com.github.jaemon.dinger.core.entity.MsgType;
import com.github.jaemon.dinger.core.entity.enums.*;
Expand Down Expand Up @@ -105,6 +103,52 @@ protected static DingerDefinition dingerMarkdownHandler(DingerType dingerType, D
return dingerDefinition;
}

/**
* 处理注解-ImageText定义的Dinger消息
*
* @param dingerType
* Dinger类型 {@link DingerType}
* @param context
* Dinger定义源
* @return
* dingerDefinition {@link DingerDefinition}
*/
protected static DingerDefinition dingerImageTextHandler(DingerType dingerType, DingerDefinitionGeneratorContext<DingerImageText> context) {
String keyName = context.getKeyName();
DingerImageText dinger = context.getSource();
DingerDefinition dingerDefinition = annotationDingerDefition(keyName, dinger.tokenId(), dinger.asyncExecute());
dingerDefinition.setDingerType(dingerType);
dingerDefinition.setMessageSubType(MessageSubType.IMAGETEXT);

MsgType msgType = dingerDefinition.messageSubType().msgType(dingerType, null);
dingerDefinition.setMessage(msgType);

return dingerDefinition;
}

/**
* 处理注解-Link定义的Dinger消息
*
* @param dingerType
* Dinger类型 {@link DingerType}
* @param context
* Dinger定义源
* @return
* dingerDefinition {@link DingerDefinition}
*/
protected static DingerDefinition dingerLinkHandler(DingerType dingerType, DingerDefinitionGeneratorContext<DingerLink> context) {
String keyName = context.getKeyName();
DingerLink dinger = context.getSource();
DingerDefinition dingerDefinition = annotationDingerDefition(keyName, dinger.tokenId(), dinger.asyncExecute());
dingerDefinition.setDingerType(dingerType);
dingerDefinition.setMessageSubType(MessageSubType.LINK);

MsgType msgType = dingerDefinition.messageSubType().msgType(dingerType, null);
dingerDefinition.setMessage(msgType);

return dingerDefinition;
}


/**
* 处理Xml定义的Dinger消息
Expand Down Expand Up @@ -141,30 +185,32 @@ protected static DingerDefinition xmlHandler(
Optional<ConfigurationTag> configuration = Optional.ofNullable(messageTag.getConfiguration());
dingerConfig(dingerConfig, configuration);

Optional<BodyTag> body = Optional.ofNullable(messageTag.getBody());
DingerRequest request = null;
if (dingerDefinitionType.messageSubType().isSupport()) {
Optional<BodyTag> body = Optional.ofNullable(messageTag.getBody());

// 处理@成员逻辑
Optional<PhonesTag> phonesTag = body.map(e -> e.getPhones());
Boolean atAll = phonesTag.map(e -> e.getAtAll()).orElse(false);
// 处理@成员逻辑
Optional<PhonesTag> phonesTag = body.map(e -> e.getPhones());
Boolean atAll = phonesTag.map(e -> e.getAtAll()).orElse(false);

List<PhoneTag> phoneTags = phonesTag.map(e -> e.getPhones()).orElse(null);
List<String> phones;
if (phoneTags != null) {
phones = phoneTags.stream().map(PhoneTag::getValue).collect(Collectors.toList());
} else {
phones = new ArrayList<>();
}
List<PhoneTag> phoneTags = phonesTag.map(e -> e.getPhones()).orElse(null);
List<String> phones;
if (phoneTags != null) {
phones = phoneTags.stream().map(PhoneTag::getValue).collect(Collectors.toList());
} else {
phones = new ArrayList<>();
}

// 处理消息体逻辑
Optional<ContentTag> contentTag = body.map(e -> e.getContent());
String content = contentTag.map(e -> e.getContent()).orElse("");
String title = contentTag.map(e -> e.getTitle()).orElse("Dinger Title");
// 处理消息体逻辑
Optional<ContentTag> contentTag = body.map(e -> e.getContent());
String content = contentTag.map(e -> e.getContent()).orElse("");
String title = contentTag.map(e -> e.getTitle()).orElse("Dinger Title");

DingerRequest request;
if (atAll) {
request = DingerRequest.request(content, title, true);
} else {
request = DingerRequest.request(content, title, phones);
if (atAll) {
request = DingerRequest.request(content, title, true);
} else {
request = DingerRequest.request(content, title, phones);
}
}

MsgType message = dingerDefinitionType.messageSubType().msgType(dingerDefinitionType.dingerType(), request);
Expand Down
Loading

0 comments on commit 5af1205

Please sign in to comment.