You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by ri...@apache.org on 2020/04/06 18:52:03 UTC

[incubator-streampipes-extensions] branch dev updated: Let Slack controller support field placeholders

This is an automated email from the ASF dual-hosted git repository.

riemer pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-streampipes-extensions.git


The following commit(s) were added to refs/heads/dev by this push:
     new 4036e85  Let Slack controller support field placeholders
     new 14c922c  Merge branch 'dev' of github.com:apache/incubator-streampipes-extensions into dev
4036e85 is described below

commit 4036e85bf45632838049599318588a04d0db2a47
Author: Dominik Riemer <ri...@fzi.de>
AuthorDate: Mon Apr 6 20:51:36 2020 +0200

    Let Slack controller support field placeholders
---
 .../notifications/jvm/slack/SlackNotification.java     | 18 ++++++++++++++----
 .../jvm/slack/SlackNotificationController.java         |  2 +-
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotification.java b/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotification.java
index eb9ba37..0cc54e2 100644
--- a/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotification.java
+++ b/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotification.java
@@ -32,6 +32,8 @@ import java.io.IOException;
 
 public class SlackNotification implements EventSink<SlackNotificationParameters> {
 
+    private static final String HASHTAG = "#";
+
     private SlackNotificationParameters params;
     private SlackSession session;
     private Boolean sendToUser;
@@ -66,13 +68,14 @@ public class SlackNotification implements EventSink<SlackNotificationParameters>
     }
 
     @Override
-    public void onEvent(Event inputEvent) {
-
+    public void onEvent(Event event) {
+        String message = replacePlaceholders(event, params.getMessage());
         if (this.sendToUser) {
-            this.session.sendMessageToUser(params.getUserChannel(), params.getMessage(), null);
+            this.session.sendMessageToUser(params.getUserChannel(),
+                    message, null);
         } else {
             SlackChannel channel = this.session.findChannelByName(params.getUserChannel());
-            this.session.sendMessage(channel, params.getMessage());
+            this.session.sendMessage(channel, message);
         }
     }
 
@@ -84,4 +87,11 @@ public class SlackNotification implements EventSink<SlackNotificationParameters>
             throw new SpRuntimeException("Could not disconnect");
         }
     }
+
+    private String replacePlaceholders(Event event, String content) {
+        for(String key: event.getRaw().keySet()) {
+            content = content.replaceAll(HASHTAG + key + HASHTAG, event.getRaw().get(key).toString());
+        }
+        return content;
+    }
 }
diff --git a/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotificationController.java b/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotificationController.java
index e67b047..5e56dcd 100644
--- a/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotificationController.java
+++ b/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotificationController.java
@@ -51,7 +51,7 @@ public class SlackNotificationController extends StandaloneEventSinkDeclarer<Sla
                     .requiredProperty(EpRequirements.anyProperty())
                     .build())
             .requiredTextParameter(Labels.withId(RECEIVER))
-            .requiredTextParameter(Labels.withId(CONTENT))
+            .requiredTextParameter(Labels.withId(CONTENT), false, true)
             .requiredSingleValueSelection(Labels.withId(CHANNEL_TYPE),
                     Options.from("User", "Channel"))
             .requiredSecret(Labels.withId(AUTH_TOKEN))