Skip to content

Commit

Permalink
[ISSUE #4590]WeCom/DingTalk connector extension supports spring envir…
Browse files Browse the repository at this point in the history
…onment variables. (#4591)
  • Loading branch information
yanrongzhen authored Dec 7, 2023
1 parent 4df4803 commit a170a79
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
public interface ConnectRecordExtensionKeys {

String DINGTALK_TEMPLATE_TYPE_KEY = "dingtalktemplatetype";
String DINGTALK_TEMPLATE_TYPE = "dingtalktemplatetype";

String DINGTALK_MARKDOWN_MESSAGE_TITLE = "dingtalkmarkdownmessagetitle";
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,30 @@

public enum DingDingMessageTemplateType {

PLAIN_TEXT("sampleText"),
MARKDOWN("sampleMarkdown");
PLAIN_TEXT("text", "sampleText"),
MARKDOWN("markdown", "sampleMarkdown");

private final String templateType;

private final String templateKey;

DingDingMessageTemplateType(String templateKey) {
DingDingMessageTemplateType(String templateType, String templateKey) {
this.templateType = templateType;
this.templateKey = templateKey;
}

public String getTemplateType() {
return templateType;
}

public String getTemplateKey() {
return templateKey;
}

public static DingDingMessageTemplateType of(String templateKey) {
public static DingDingMessageTemplateType of(String templateType) {
return Arrays.stream(values())
.filter(v -> v.getTemplateKey().equals(templateKey))
.filter(v -> v.getTemplateType().equals(templateType))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("TemplateKey: " + templateKey + " not found."));
.orElseThrow(() -> new IllegalArgumentException("TemplateType: " + templateType + " not found."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ public void put(List<ConnectRecord> sinkRecords) {
new OrgGroupSendHeaders();
orgGroupSendHeaders.xAcsDingtalkAccessToken = accessToken;

String templateTypeKey = record.getExtension(ConnectRecordExtensionKeys.DINGTALK_TEMPLATE_TYPE_KEY);
String templateTypeKey = record.getExtension(ConnectRecordExtensionKeys.DINGTALK_TEMPLATE_TYPE);
if (null == templateTypeKey || "null".equals(templateTypeKey)) {
templateTypeKey = DingDingMessageTemplateType.PLAIN_TEXT.getTemplateKey();
templateTypeKey = DingDingMessageTemplateType.PLAIN_TEXT.getTemplateType();
}
DingDingMessageTemplateType templateType = DingDingMessageTemplateType.of(templateTypeKey);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public void testSendMessageToDingDing() throws Exception {
RecordOffset offset = new RecordOffset();
ConnectRecord connectRecord = new ConnectRecord(partition, offset,
System.currentTimeMillis(), "Hello, EventMesh!".getBytes(StandardCharsets.UTF_8));
connectRecord.addExtension(ConnectRecordExtensionKeys.DINGTALK_TEMPLATE_TYPE_KEY,
DingDingMessageTemplateType.PLAIN_TEXT.getTemplateKey());
connectRecord.addExtension(ConnectRecordExtensionKeys.DINGTALK_TEMPLATE_TYPE,
DingDingMessageTemplateType.PLAIN_TEXT.getTemplateType());
records.add(connectRecord);
}
connector.put(records);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,30 @@

public enum WeComMessageTemplateType {

PLAIN_TEXT("text"),
MARKDOWN("markdown");
PLAIN_TEXT("text", "text"),
MARKDOWN("markdown", "markdown");

private final String templateType;

private final String templateKey;

WeComMessageTemplateType(String templateKey) {
WeComMessageTemplateType(String templateType, String templateKey) {
this.templateType = templateType;
this.templateKey = templateKey;
}

public String getTemplateType() {
return templateType;
}

public String getTemplateKey() {
return templateKey;
}

public static WeComMessageTemplateType of(String templateKey) {
public static WeComMessageTemplateType of(String templateType) {
return Arrays.stream(values())
.filter(v -> v.getTemplateKey().equals(templateKey))
.filter(v -> v.getTemplateType().equals(templateType))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("TemplateKey: " + templateKey + " not found."));
.orElseThrow(() -> new IllegalArgumentException("TemplateType: " + templateType + " not found."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
*/
public interface ConnectRecordExtensionKeys {

String WECOM_MESSAGE_TEMPLATE_TYPE_KEY = "weCom:MessageTemplateTypeKey";
String WECOM_MESSAGE_TEMPLATE_TYPE = "wecomtemplatetype";

}
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ private void sendMessage(ConnectRecord record) {
HttpPost httpPost = new HttpPost(target);
httpPost.addHeader("Content-Type", EventMeshDataContentType.JSON.getCode());
WeComMessageTemplateType templateType = WeComMessageTemplateType.of(
Optional.ofNullable(record.getExtension(ConnectRecordExtensionKeys.WECOM_MESSAGE_TEMPLATE_TYPE_KEY))
.orElse(WeComMessageTemplateType.PLAIN_TEXT.getTemplateKey()));
Optional.ofNullable(record.getExtension(ConnectRecordExtensionKeys.WECOM_MESSAGE_TEMPLATE_TYPE))
.orElse(WeComMessageTemplateType.PLAIN_TEXT.getTemplateType()));
Map<String, Object> contentMap = new HashMap<>();
if (WeComMessageTemplateType.PLAIN_TEXT == templateType) {
contentMap.put("content", new String((byte[]) record.getData()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public void testSendMessageToWeCom() throws IOException {
RecordOffset offset = new RecordOffset();
ConnectRecord connectRecord = new ConnectRecord(partition, offset,
System.currentTimeMillis(), "Hello, EventMesh!".getBytes(StandardCharsets.UTF_8));
connectRecord.addExtension(ConnectRecordExtensionKeys.WECOM_MESSAGE_TEMPLATE_TYPE_KEY,
WeComMessageTemplateType.PLAIN_TEXT.getTemplateKey());
connectRecord.addExtension(ConnectRecordExtensionKeys.WECOM_MESSAGE_TEMPLATE_TYPE,
WeComMessageTemplateType.PLAIN_TEXT.getTemplateType());
records.add(connectRecord);
}
connector.put(records);
Expand Down
3 changes: 2 additions & 1 deletion eventmesh-examples/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ eventmesh.selector.type=nacos
eventmesh.selector.nacos.address=127.0.0.1:8848
eventmesh.catalog.name=EVENTMESH-catalog
eventmesh.workflow.name=EVENTMESH-workflow
eventmesh.connector.dingtalkTemplateType=sampleText
eventmesh.connector.dingtalkTemplateType=text
eventmesh.connector.wecomTemplateType=text

0 comments on commit a170a79

Please sign in to comment.