
de.codecentric.boot.admin.server.notify.MicrosoftTeamsNotifier Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2014-2023 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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.Builder;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.expression.MapAccessor;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.DataBindingPropertyAccessor;
import org.springframework.expression.spel.support.SimpleEvaluationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
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.InstanceDeregisteredEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceRegisteredEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent;
import static java.util.Collections.singletonList;
public class MicrosoftTeamsNotifier extends AbstractStatusChangeNotifier {
private static final Logger LOGGER = LoggerFactory.getLogger(MicrosoftTeamsNotifier.class);
private static final String STATUS_KEY = "Status";
private static final String SERVICE_URL_KEY = "Service URL";
private static final String HEALTH_URL_KEY = "Health URL";
private static final String MANAGEMENT_URL_KEY = "Management URL";
private static final String SOURCE_KEY = "Source";
private static final String DEFAULT_THEME_COLOR_EXPRESSION = "#{event.type == 'STATUS_CHANGED' ? (event.statusInfo.status=='UP' ? '6db33f' : 'b32d36') : '439fe0'}";
private static final String DEFAULT_DEREGISTER_ACTIVITY_SUBTITLE_EXPRESSION = "#{instance.registration.name} with id #{instance.id} has de-registered from Spring Boot Admin";
private static final String DEFAULT_REGISTER_ACTIVITY_SUBTITLE_EXPRESSION = "#{instance.registration.name} with id #{instance.id} has registered with Spring Boot Admin";
private static final String DEFAULT_STATUS_ACTIVITY_SUBTITLE_EXPRESSION = "#{instance.registration.name} with id #{instance.id} changed status from #{lastStatus} to #{event.statusInfo.status}";
private final SpelExpressionParser parser = new SpelExpressionParser();
private RestTemplate restTemplate;
/**
* Webhook url for Microsoft Teams Channel Webhook connector (i.e.
* https://outlook.office.com/webhook/{webhook-id})
*/
@Nullable
private URI webhookUrl;
/**
* Theme Color is the color of the accent on the message that appears in Microsoft
* Teams. Default is Spring Green
*/
private Expression themeColor;
/**
* Message will be used as title of the Activity section of the Teams message when an
* app de-registers.
*/
private Expression deregisterActivitySubtitle;
/**
* Message will be used as title of the Activity section of the Teams message when an
* app registers
*/
private Expression registerActivitySubtitle;
/**
* Message will be used as title of the Activity section of the Teams message when an
* app changes status
*/
private Expression statusActivitySubtitle;
/**
* Title of the Teams message when an app de-registers
*/
private String deRegisteredTitle = "De-Registered";
/**
* Title of the Teams message when an app registers
*/
private String registeredTitle = "Registered";
/**
* Title of the Teams message when an app changes status
*/
private String statusChangedTitle = "Status Changed";
/**
* Summary section of every Teams message originating from Spring Boot Admin
*/
private String messageSummary = "Spring Boot Admin Notification";
public MicrosoftTeamsNotifier(InstanceRepository repository, RestTemplate restTemplate) {
super(repository);
this.restTemplate = restTemplate;
this.themeColor = parser.parseExpression(DEFAULT_THEME_COLOR_EXPRESSION, ParserContext.TEMPLATE_EXPRESSION);
this.deregisterActivitySubtitle = parser.parseExpression(DEFAULT_DEREGISTER_ACTIVITY_SUBTITLE_EXPRESSION,
ParserContext.TEMPLATE_EXPRESSION);
this.registerActivitySubtitle = parser.parseExpression(DEFAULT_REGISTER_ACTIVITY_SUBTITLE_EXPRESSION,
ParserContext.TEMPLATE_EXPRESSION);
this.statusActivitySubtitle = parser.parseExpression(DEFAULT_STATUS_ACTIVITY_SUBTITLE_EXPRESSION,
ParserContext.TEMPLATE_EXPRESSION);
}
@Override
protected Mono doNotify(InstanceEvent event, Instance instance) {
Message message;
EvaluationContext context = createEvaluationContext(event, instance);
if (event instanceof InstanceRegisteredEvent) {
message = getRegisteredMessage(instance, context);
}
else if (event instanceof InstanceDeregisteredEvent) {
message = getDeregisteredMessage(instance, context);
}
else if (event instanceof InstanceStatusChangedEvent) {
message = getStatusChangedMessage(instance, context);
}
else {
return Mono.empty();
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
if (webhookUrl == null) {
return Mono.error(new IllegalStateException("'webhookUrl' must not be null."));
}
return Mono.fromRunnable(() -> this.restTemplate.postForEntity(webhookUrl,
new HttpEntity
© 2015 - 2025 Weber Informatics LLC | Privacy Policy