From 1a93db75e07258889195791c6eeeb8a3c4f76e40 Mon Sep 17 00:00:00 2001 From: ViacheslavKlimov Date: Fri, 4 Aug 2023 17:33:11 +0300 Subject: [PATCH] Improvements for Microsoft Teams notifications --- .../MicrosoftTeamsNotificationChannel.java | 58 ++++++++++++++----- .../system/DefaultSystemSecurityService.java | 2 +- .../notification/NotificationApiTest.java | 5 +- ...amsDeliveryMethodNotificationTemplate.java | 36 ++++++++---- 4 files changed, 73 insertions(+), 28 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/service/notification/channels/MicrosoftTeamsNotificationChannel.java b/application/src/main/java/org/thingsboard/server/service/notification/channels/MicrosoftTeamsNotificationChannel.java index c628bf11f6..ddb827c26e 100644 --- a/application/src/main/java/org/thingsboard/server/service/notification/channels/MicrosoftTeamsNotificationChannel.java +++ b/application/src/main/java/org/thingsboard/server/service/notification/channels/MicrosoftTeamsNotificationChannel.java @@ -17,28 +17,37 @@ package org.thingsboard.server.service.notification.channels; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.common.base.Strings; import lombok.Data; import lombok.RequiredArgsConstructor; import lombok.Setter; +import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.StringUtils; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; +import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod; +import org.thingsboard.server.common.data.notification.info.NotificationInfo; import org.thingsboard.server.common.data.notification.targets.MicrosoftTeamsNotificationTargetConfig; import org.thingsboard.server.common.data.notification.template.MicrosoftTeamsDeliveryMethodNotificationTemplate; +import org.thingsboard.server.common.data.notification.template.MicrosoftTeamsDeliveryMethodNotificationTemplate.Button.LinkType; import org.thingsboard.server.service.notification.NotificationProcessingContext; +import org.thingsboard.server.service.security.system.SystemSecurityService; import java.time.Duration; import java.time.temporal.ChronoUnit; import java.util.List; +import java.util.Optional; @Component @RequiredArgsConstructor public class MicrosoftTeamsNotificationChannel implements NotificationChannel { + private final SystemSecurityService systemSecurityService; + @Setter private RestTemplate restTemplate = new RestTemplateBuilder() .setConnectTimeout(Duration.of(15, ChronoUnit.SECONDS)) @@ -47,11 +56,6 @@ public class MicrosoftTeamsNotificationChannel implements NotificationChannel templatableValues = List.of( TemplatableValue.of(this::getBody, this::setBody), TemplatableValue.of(this::getSubject, this::setSubject), - TemplatableValue.of(() -> button != null ? button.getName() : null, - processed -> {if (button != null) button.setName(processed);}), - TemplatableValue.of(() -> button != null ? button.getUri() : null, - processed -> {if (button != null) button.setUri(processed);}), - TemplatableValue.of(this::getCustomMessageCardJson, this::setCustomMessageCardJson) + TemplatableValue.of(() -> button != null ? button.getText() : null, + processed -> { if (button != null) button.setText(processed); }), + TemplatableValue.of(() -> button != null ? button.getLink() : null, + processed -> { if (button != null) button.setLink(processed); }) ); public MicrosoftTeamsDeliveryMethodNotificationTemplate(MicrosoftTeamsDeliveryMethodNotificationTemplate other) { @@ -50,7 +48,6 @@ public class MicrosoftTeamsDeliveryMethodNotificationTemplate extends DeliveryMe this.subject = other.subject; this.themeColor = other.themeColor; this.button = other.button != null ? new Button(other.button) : null; - this.customMessageCardJson = other.customMessageCardJson; } @Override @@ -66,12 +63,27 @@ public class MicrosoftTeamsDeliveryMethodNotificationTemplate extends DeliveryMe @Data @NoArgsConstructor public static class Button { - private String name; - private String uri; + private boolean enabled; + private String text; + private LinkType linkType; + private String link; + + private UUID dashboardId; + private String dashboardState; + private boolean setEntityIdInState; public Button(Button other) { - this.name = other.name; - this.uri = other.uri; + this.enabled = other.enabled; + this.text = other.text; + this.linkType = other.linkType; + this.link = other.link; + this.dashboardId = other.dashboardId; + this.dashboardState = other.dashboardState; + this.setEntityIdInState = other.setEntityIdInState; + } + + public enum LinkType { + LINK, DASHBOARD } }