You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by wi...@apache.org on 2020/09/13 16:50:08 UTC

[incubator-streampipes-extensions] branch rel/0.67.0 updated: [STREAMPIPES-236] Adjust telegram sink using html font format option for messages

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

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


The following commit(s) were added to refs/heads/rel/0.67.0 by this push:
     new c3737c1  [STREAMPIPES-236] Adjust telegram sink using html font format option for messages
c3737c1 is described below

commit c3737c1478c8aa87e1a8c802c7c02824048c58a1
Author: Patrick Wiener <wi...@fzi.de>
AuthorDate: Sun Sep 13 18:49:32 2020 +0200

    [STREAMPIPES-236] Adjust telegram sink using html font format option for messages
---
 .../notifications/jvm/telegram/TelegramController.java     | 10 +++++-----
 .../notifications/jvm/telegram/TelegramParameters.java     | 10 +++++-----
 .../notifications/jvm/telegram/TelegramPublisher.java      | 14 ++++++++------
 .../documentation.md                                       |  6 ++++--
 .../strings.en                                             |  4 ++--
 5 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/telegram/TelegramController.java b/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/telegram/TelegramController.java
index cf4a541..535bdc0 100644
--- a/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/telegram/TelegramController.java
+++ b/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/telegram/TelegramController.java
@@ -32,7 +32,7 @@ import org.apache.streampipes.wrapper.standalone.ConfiguredEventSink;
 import org.apache.streampipes.wrapper.standalone.declarer.StandaloneEventSinkDeclarer;
 
 public class TelegramController extends StandaloneEventSinkDeclarer<TelegramParameters> {
-    private static final String CHANNEL_NAME = "channel-name";
+    private static final String CHANNEL_NAME_OR_CHAT_ID = "channel-chat-name";
     private static final String MESSAGE_TEXT = "message-text";
     private static final String BOT_API_KEY = "api-key";
 
@@ -47,8 +47,8 @@ public class TelegramController extends StandaloneEventSinkDeclarer<TelegramPara
                         .requiredProperty(EpRequirements.anyProperty())
                         .build())
                 .requiredSecret(Labels.withId(BOT_API_KEY))
-                .requiredTextParameter(Labels.withId(CHANNEL_NAME))
-                .requiredTextParameter(Labels.withId(MESSAGE_TEXT), true, true)
+                .requiredTextParameter(Labels.withId(CHANNEL_NAME_OR_CHAT_ID))
+                .requiredTextParameter(Labels.withId(MESSAGE_TEXT), true, true, true)
                 .build();
     }
 
@@ -57,9 +57,9 @@ public class TelegramController extends StandaloneEventSinkDeclarer<TelegramPara
     public ConfiguredEventSink<TelegramParameters> onInvocation(DataSinkInvocation graph,
                                                                 DataSinkParameterExtractor extractor) {
         String apiKey = extractor.secretValue(BOT_API_KEY);
-        String channel = extractor.singleValueParameter(CHANNEL_NAME, String.class);
+        String channelOrChatId = extractor.singleValueParameter(CHANNEL_NAME_OR_CHAT_ID, String.class);
         String message = extractor.singleValueParameter(MESSAGE_TEXT, String.class);
-        TelegramParameters params = new TelegramParameters(graph, apiKey, channel, message);
+        TelegramParameters params = new TelegramParameters(graph, apiKey, channelOrChatId, message);
         return new ConfiguredEventSink<>(params, TelegramPublisher::new);
     }
 }
diff --git a/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/telegram/TelegramParameters.java b/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/telegram/TelegramParameters.java
index b792381..c5beb19 100644
--- a/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/telegram/TelegramParameters.java
+++ b/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/telegram/TelegramParameters.java
@@ -23,16 +23,16 @@ import org.apache.streampipes.wrapper.params.binding.EventSinkBindingParams;
 
 public class TelegramParameters extends EventSinkBindingParams {
     private String apiKey;
-    private String channel;
+    private String channelOrChatId;
     private String message;
 
     public TelegramParameters(DataSinkInvocation graph,
                               String apiKey,
-                              String channel,
+                              String channelOrChatId,
                               String message) {
         super(graph);
         this.apiKey = apiKey;
-        this.channel = channel;
+        this.channelOrChatId = channelOrChatId;
         this.message = message;
     }
 
@@ -40,8 +40,8 @@ public class TelegramParameters extends EventSinkBindingParams {
         return apiKey;
     }
 
-    public String getChannel() {
-        return channel;
+    public String getChannelOrChatId() {
+        return channelOrChatId;
     }
 
     public String getMessage() {
diff --git a/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/telegram/TelegramPublisher.java b/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/telegram/TelegramPublisher.java
index bdc6885..8545278 100644
--- a/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/telegram/TelegramPublisher.java
+++ b/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/telegram/TelegramPublisher.java
@@ -34,18 +34,19 @@ import okhttp3.Request;
 import okhttp3.Response;
 
 public class TelegramPublisher implements EventSink<TelegramParameters> {
-    private static final String ENDPOINT = "https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s";
+    private static final String ENDPOINT = "https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s&parse_mode=%s";
     private static final OkHttpClient HTTP_CLIENT = new OkHttpClient();
     private static final String HASH_TAG = "#";
+    private static final String HTML = "HTML";
     private String apiKey;
-    private String channel;
+    private String channelOrChatId;
     private String message;
 
     @Override
     public void onInvocation(TelegramParameters parameters,
                              EventSinkRuntimeContext runtimeContext) {
         this.apiKey = parameters.getApiKey();
-        this.channel = parameters.getChannel();
+        this.channelOrChatId = parameters.getChannelOrChatId();
         this.message = parameters.getMessage();
     }
 
@@ -55,7 +56,7 @@ public class TelegramPublisher implements EventSink<TelegramParameters> {
             String content = replacePlaceholders(event, this.message);
             content = trimHTML(content);
             content = URLEncoder.encode(content, StandardCharsets.UTF_8.toString());
-            String url = String.format(ENDPOINT, this.apiKey, this.channel, content);
+            String url = String.format(ENDPOINT, this.apiKey, this.channelOrChatId, content, HTML);
             Request request = new Request.Builder().url(url).build();
             try (Response response = HTTP_CLIENT.newCall(request).execute()) {
                 if (!response.isSuccessful()) {
@@ -85,8 +86,9 @@ public class TelegramPublisher implements EventSink<TelegramParameters> {
     }
 
     private String trimHTML(String content) {
-        content = content.replaceAll("<div><!--block-->", "");
-        content = content.replaceAll("&nbsp;</div>", "");
+        content = content.replaceAll("(</h[^>]+><h[^>]+>)|(</h[^>]+><[^>]+>)|(</p><p>)", "\n");
+        content = content.replaceAll("(<h[^>]+>)|(</p>)|(<p>)|(<span[^>]+>)|(</span>)", "");
+
         return content;
     }
 }
diff --git a/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.telegram/documentation.md b/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.telegram/documentation.md
index d33ebf4..6d1f467 100644
--- a/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.telegram/documentation.md
+++ b/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.telegram/documentation.md
@@ -30,6 +30,7 @@ Publisher to send notifications to a Telegram channel.
 
 In order to be able to do so, you will have first to:
 * Create a Telegram public [channel](https://telegram.org/tour/channels).
+> Private channels/groups: also supported.
 * Create a Telegram BOT via [@BotFather](https://core.telegram.org/bots#3-how-do-i-create-a-bot) and get an API key.
 * Set the bot as [administrator](https://www.wikihow.com/Make-Someone-an-Admin-on-Telegram) in your channel.
 
@@ -49,9 +50,10 @@ Describe the configuration parameters here.
 
 The API Key generated by `@BotFather` when you created your bot.
 
-### Channel Name
+### Channel Name or Chat Id
 
-The handle name of your channel (e.g. `@channel_name`).
+The handle name of your public channel (e.g. `@channel_name`). 
+> For private channels/groups: handle name only available for public channels. Use `chat_id` instead.
 
 ### Content
 
diff --git a/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.telegram/strings.en b/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.telegram/strings.en
index 38faafe..e909460 100644
--- a/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.telegram/strings.en
+++ b/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.telegram/strings.en
@@ -4,8 +4,8 @@ org.apache.streampipes.sinks.notifications.jvm.telegram.description=Publisher to
 api-key.title=Bot API Key
 api-key.description=The API Key generated by @BotFather when you created your bot.
 
-channel-name.title=Channel Name
-channel-name.description=The handle name of your channel (e.g. @channel_name).
+channel-chat-name.title=Channel Name or Chat Id
+channel-chat-name.description=The handle name of your public channel (e.g. @channel_name). For private channels or groups use chat id.
 
 message-text.title=Message
 message-text.description=The message to be sent.