
de.codecentric.boot.admin.server.notify.MicrosoftTeamsNotifier Maven / Gradle / Ivy
/*
* Copyright 2014-2018 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
*
* 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 de.codecentric.boot.admin.server.notify;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.MissingFormatArgumentException;
import javax.annotation.Nullable;
import lombok.Builder;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
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 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 String themeColor = "6db33f";
/**
* Message will be used as title of the Activity section of the Teams message when an
* app de-registers.
*/
private String deregisterActivitySubtitlePattern = "%s with id %s has de-registered from Spring Boot Admin";
/**
* Message will be used as title of the Activity section of the Teams message when an
* app registers
*/
private String registerActivitySubtitlePattern = "%s with id %s has registered with Spring Boot Admin";
/**
* Message will be used as title of the Activity section of the Teams message when an
* app changes status
*/
private String statusActivitySubtitlePattern = "%s with id %s changed status from %s to %s";
/**
* 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;
}
@Override
protected Mono doNotify(InstanceEvent event, Instance instance) {
Message message;
if (event instanceof InstanceRegisteredEvent) {
message = getRegisteredMessage(instance);
}
else if (event instanceof InstanceDeregisteredEvent) {
message = getDeregisteredMessage(instance);
}
else if (event instanceof InstanceStatusChangedEvent) {
InstanceStatusChangedEvent statusChangedEvent = (InstanceStatusChangedEvent) event;
message = getStatusChangedMessage(instance, getLastStatus(event.getInstance()),
statusChangedEvent.getStatusInfo().getStatus());
}
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