Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature-321][Notification] Support dingtalk notification channel #322

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!--

Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<artifactId>datavines-notification-plugins</artifactId>
<groupId>io.datavines</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>datavines-notification-plugin-dingtalk</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>io.datavines</groupId>
<artifactId>datavines-common</artifactId>
</dependency>
<dependency>
<groupId>io.datavines</groupId>
<artifactId>datavines-notification-api</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.datavines.notification.plugin.dingtalk;

public class DingTalkConstants {

private DingTalkConstants() {
throw new IllegalStateException(DingTalkConstants.class.getName());
}

static final String DING_TALK_MSG_TYPE_TEXT = "text";
static final String DING_TALK_MSG_TYPE_MARKDOWN = "markdown";


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.datavines.notification.plugin.dingtalk;

import io.datavines.common.utils.JSONUtils;
import io.datavines.notification.api.entity.SlaNotificationResultRecord;
import io.datavines.notification.api.entity.SlaSenderMessage;
import io.datavines.notification.plugin.dingtalk.entity.ReceiverConfig;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.Objects.requireNonNull;

@Slf4j
@EqualsAndHashCode
@Data
public class DingTalkSender {

private String msgType;
private String webHook;
private String keyWord;

private String mustNotNull = " must not be null";

public DingTalkSender(SlaSenderMessage senderMessage) {

String configString = senderMessage.getConfig();
Map<String, String> config = JSONUtils.toMap(configString);

msgType=config.get("msgType");

webHook=config.get("webHook");
requireNonNull(webHook, "dingtalk webHook" + mustNotNull);
keyWord=config.get("keyWord");
requireNonNull(keyWord, "dingtalk keyWord" + mustNotNull);

}

public SlaNotificationResultRecord sendCardMsg(Set<ReceiverConfig> receiverSet, String subject, String message){
SlaNotificationResultRecord result = new SlaNotificationResultRecord();
if (CollectionUtils.isEmpty(receiverSet)) {
return result;
}
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
Set<ReceiverConfig> failToReceivers = new HashSet<>();
for(ReceiverConfig receiverConfig : receiverSet){
try {
String msg = generateMsgJson(subject, message, receiverConfig);
HttpPost httpPost = constructHttpPost(webHook, msg);
CloseableHttpClient httpClient = getDefaultClient();
try {
CloseableHttpResponse response = httpClient.execute(httpPost);
String resp;
try {
HttpEntity entity = response.getEntity();
resp = EntityUtils.toString(entity, "UTF-8");
EntityUtils.consume(entity);
} finally {
response.close();
}
log.info("Ding Talk send msg :{}, resp: {}", msg, resp);
} finally {
httpClient.close();
}
} catch (Exception e) {
failToReceivers.add(receiverConfig);
log.error("dingtalk send error", e);
}
}

if (!CollectionUtils.isEmpty(failToReceivers)) {
String recordMessage = String.format("send to %s fail", String.join(",", failToReceivers.stream().map(ReceiverConfig::getAtMobiles).collect(Collectors.toList())));
result.setStatus(false);
result.setMessage(recordMessage);
}else{
result.setStatus(true);
}
return result;
}
private String generateMsgJson(String title, String content,ReceiverConfig receiverConfig) {

final String atMobiles = receiverConfig.getAtMobiles();
final String atUserIds = receiverConfig.getAtUserIds();
final Boolean isAtAll = receiverConfig.getIsAtAll();

if (org.apache.commons.lang3.StringUtils.isBlank(msgType)) {
msgType = DingTalkConstants.DING_TALK_MSG_TYPE_TEXT;
}
Map<String, Object> items = new HashMap<>();
items.put("msgtype", msgType);
Map<String, Object> text = new HashMap<>();
items.put(msgType, text);

if (DingTalkConstants.DING_TALK_MSG_TYPE_MARKDOWN.equals(msgType)) {
StringBuilder builder = new StringBuilder(content);
if (org.apache.commons.lang3.StringUtils.isNotBlank(keyWord)) {
builder.append(" ");
builder.append(keyWord);
}
builder.append("\n\n");
if (org.apache.commons.lang3.StringUtils.isNotBlank(atMobiles)) {
Arrays.stream(atMobiles.split(",")).forEach(value -> {
builder.append("@");
builder.append(value);
builder.append(" ");
});
}
if (org.apache.commons.lang3.StringUtils.isNotBlank(atUserIds)) {
Arrays.stream(atUserIds.split(",")).forEach(value -> {
builder.append("@");
builder.append(value);
builder.append(" ");
});
}
byte[] byt = StringUtils.getBytesUtf8(builder.toString());
String txt = StringUtils.newStringUtf8(byt);
text.put("title", title);
text.put("text", txt);
} else {
StringBuilder builder = new StringBuilder(title);
builder.append("\n");
builder.append(content);
if (org.apache.commons.lang3.StringUtils.isNotBlank(keyWord)) {
builder.append(" ");
builder.append(keyWord);
}
byte[] byt = StringUtils.getBytesUtf8(builder.toString());
String txt = StringUtils.newStringUtf8(byt);
text.put("content", txt);
}

Map<String, Object> at = new HashMap<>();

String[] atMobileArray =
org.apache.commons.lang3.StringUtils.isNotBlank(atMobiles) ? atMobiles.split(",")
: new String[0];
String[] atUserArray =
org.apache.commons.lang3.StringUtils.isNotBlank(atUserIds) ? atUserIds.split(",")
: new String[0];
at.put("atMobiles", atMobileArray);
at.put("atUserIds", atUserArray);
at.put("isAtAll", isAtAll);

items.put("at", at);

return JSONUtils.toJsonString(items);

}
private static HttpPost constructHttpPost(String url, String msg) {
HttpPost post = new HttpPost(url);
StringEntity entity = new StringEntity(msg, StandardCharsets.UTF_8);
post.setEntity(entity);
post.addHeader("Content-Type", "application/json; charset=utf-8");
return post;
}

private static CloseableHttpClient getDefaultClient() {
return HttpClients.createDefault();
}

}
Loading
Loading