
de.codecentric.boot.admin.server.notify.FeiShuNotifier Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2014-2022 the original author or authors.
*
* Licensed 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
*
* https://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 de.codecentric.boot.admin.server.notify;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.expression.MapAccessor;
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import reactor.core.publisher.Mono;
import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
/**
* Notifier submitting events to FeiShu by webhooks.
*
* @author sweeter
* @see https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN
*
*/
public class FeiShuNotifier extends AbstractStatusChangeNotifier {
private static final String DEFAULT_MESSAGE = "ServiceName: #{instance.registration.name}(#{instance.id}) \nServiceUrl: #{instance.registration.serviceUrl} \nStatus: changed status from [#{lastStatus}] to [#{event.statusInfo.status}]";
private final Logger log = LoggerFactory.getLogger(this.getClass());
private final SpelExpressionParser parser = new SpelExpressionParser();
private RestTemplate restTemplate;
private Expression message;
/**
* Webhook URL for the FeiShu(飞书) chat group API (i.e.
* https://open.feishu.cn/open-apis/bot/v2/hook/xxx).
*/
private URI webhookUrl;
/**
* @ all.
*/
private boolean atAll = true;
/**
* The secret of the chat group robot from the FeiShu setup.
*/
private String secret;
/**
* FeiShu message type: text(文本) interactive(消息卡片)
*/
private MessageType messageType = MessageType.interactive;
/**
* Card theme message
*/
private Card card = new Card();
public FeiShuNotifier(InstanceRepository repository, RestTemplate restTemplate) {
super(repository);
this.restTemplate = restTemplate;
this.message = this.parser.parseExpression(DEFAULT_MESSAGE, ParserContext.TEMPLATE_EXPRESSION);
}
@Override
protected Mono doNotify(InstanceEvent event, Instance instance) {
if (webhookUrl == null) {
return Mono.error(new IllegalStateException("'webhookUrl' must not be null."));
}
return Mono.fromRunnable(() -> {
ResponseEntity responseEntity = this.restTemplate.postForEntity(this.webhookUrl,
this.createNotification(event, instance), String.class);
log.debug("Send a notification message to the FeiShu group,returns the parameter:{}",
responseEntity.getBody());
});
}
private String generateSign(String secret, long timestamp) {
try {
String stringToSign = timestamp + "\n" + secret;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(stringToSign.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
byte[] signData = mac.doFinal(new byte[] {});
return new String(Base64.getEncoder().encode(signData));
}
catch (Exception ex) {
log.error("Description Failed to generate the Webhook signature of the FeiShu:{}", ex.getMessage());
}
return null;
}
protected HttpEntity
© 2015 - 2025 Weber Informatics LLC | Privacy Policy