camel git commit: CAMEL-10889: support HTML and Markdown in camel-telegram

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

camel git commit: CAMEL-10889: support HTML and Markdown in camel-telegram

nferraro
Repository: camel
Updated Branches:
  refs/heads/master 0d096d77f -> 2341e54f5


CAMEL-10889: support HTML and Markdown in camel-telegram


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/2341e54f
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/2341e54f
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/2341e54f

Branch: refs/heads/master
Commit: 2341e54f5844f61c9c285e7f0b00495c41c0eea9
Parents: 0d096d7
Author: Nicola Ferraro <[hidden email]>
Authored: Thu Mar 16 16:57:13 2017 +0100
Committer: Nicola Ferraro <[hidden email]>
Committed: Thu Mar 16 16:58:05 2017 +0100

----------------------------------------------------------------------
 .../src/main/docs/telegram-component.adoc       |  2 +
 .../component/telegram/TelegramConstants.java   |  1 +
 .../component/telegram/TelegramParseMode.java   | 35 +++++++++++
 .../telegram/util/TelegramConverter.java        | 19 ++++++
 .../telegram/TelegramProducerMediaTest.java     | 64 +++++++++++++++++++-
 .../integration/TelegramServiceTest.java        | 25 ++++++++
 6 files changed, 144 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/2341e54f/components/camel-telegram/src/main/docs/telegram-component.adoc
----------------------------------------------------------------------
diff --git a/components/camel-telegram/src/main/docs/telegram-component.adoc b/components/camel-telegram/src/main/docs/telegram-component.adoc
index 3fc8d68..12fb213 100644
--- a/components/camel-telegram/src/main/docs/telegram-component.adoc
+++ b/components/camel-telegram/src/main/docs/telegram-component.adoc
@@ -124,6 +124,8 @@ belonging to the `org.apache.camel.component.telegram.TelegramMediaType` enumera
 |`CamelTelegramMediaTitleCaption` |This header is used to provide a caption or title
 for outgoing binary messages.
 
+|`CamelTelegramParseMode` |This header is used to format text messages using HTML or Markdown (see `org.apache.camel.component.telegram.TelegramParseMode`).
+
 |=======================================================================
 
 ### Usage

http://git-wip-us.apache.org/repos/asf/camel/blob/2341e54f/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramConstants.java
----------------------------------------------------------------------
diff --git a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramConstants.java b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramConstants.java
index ad3932b..f94836f 100644
--- a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramConstants.java
+++ b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramConstants.java
@@ -27,6 +27,7 @@ public final class TelegramConstants {
 
     public static final String TELEGRAM_MEDIA_TITLE_CAPTION = "CamelTelegramMediaTitleCaption";
 
+    public static final String TELEGRAM_PARSE_MODE = "CamelTelegramParseMode";
 
     private TelegramConstants() {
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/2341e54f/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramParseMode.java
----------------------------------------------------------------------
diff --git a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramParseMode.java b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramParseMode.java
new file mode 100644
index 0000000..38dc87a
--- /dev/null
+++ b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramParseMode.java
@@ -0,0 +1,35 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.camel.component.telegram;
+
+/**
+ * A collection of supported parse modes for text messages.
+ */
+public enum TelegramParseMode {
+
+    HTML("HTML"), MARKDOWN("Markdown");
+
+    private String code;
+
+    TelegramParseMode(String code) {
+        this.code = code;
+    }
+
+    public String getCode() {
+        return code;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/2341e54f/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/util/TelegramConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/util/TelegramConverter.java b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/util/TelegramConverter.java
index 8ff0832..5e4c5b4 100644
--- a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/util/TelegramConverter.java
+++ b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/util/TelegramConverter.java
@@ -20,6 +20,7 @@ import org.apache.camel.Converter;
 import org.apache.camel.Exchange;
 import org.apache.camel.component.telegram.TelegramConstants;
 import org.apache.camel.component.telegram.TelegramMediaType;
+import org.apache.camel.component.telegram.TelegramParseMode;
 import org.apache.camel.component.telegram.model.IncomingMessage;
 import org.apache.camel.component.telegram.model.OutgoingAudioMessage;
 import org.apache.camel.component.telegram.model.OutgoingDocumentMessage;
@@ -83,6 +84,12 @@ public final class TelegramConverter {
         case TEXT: {
             OutgoingTextMessage txt = new OutgoingTextMessage();
             txt.setText(message);
+
+            TelegramParseMode parseMode = getParseMode(exchange);
+            if (parseMode != null) {
+                txt.setParseMode(parseMode.getCode());
+            }
+
             result = txt;
             break;
         }
@@ -174,5 +181,17 @@ public final class TelegramConverter {
         return result;
     }
 
+    private static TelegramParseMode getParseMode(Exchange exchange) {
+        TelegramParseMode mode = null;
+        Object parseMode = exchange.getIn().getHeader(TelegramConstants.TELEGRAM_PARSE_MODE);
+        if (parseMode instanceof String) {
+            mode = TelegramParseMode.valueOf((String) parseMode);
+        } else {
+            mode = (TelegramParseMode) parseMode;
+        }
+
+        return mode;
+    }
+
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/2341e54f/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramProducerMediaTest.java
----------------------------------------------------------------------
diff --git a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramProducerMediaTest.java b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramProducerMediaTest.java
index f4a6c02..bc72826 100644
--- a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramProducerMediaTest.java
+++ b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramProducerMediaTest.java
@@ -24,6 +24,7 @@ import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.telegram.model.OutgoingAudioMessage;
 import org.apache.camel.component.telegram.model.OutgoingDocumentMessage;
 import org.apache.camel.component.telegram.model.OutgoingPhotoMessage;
+import org.apache.camel.component.telegram.model.OutgoingTextMessage;
 import org.apache.camel.component.telegram.model.OutgoingVideoMessage;
 import org.apache.camel.component.telegram.util.TelegramTestSupport;
 import org.apache.camel.component.telegram.util.TelegramTestUtil;
@@ -92,7 +93,7 @@ public class TelegramProducerMediaTest extends TelegramTestSupport {
 
         Exchange ex = endpoint.createExchange();
         ex.getIn().setHeader(TelegramConstants.TELEGRAM_MEDIA_TITLE_CAPTION, "Audio");
-        ex.getIn().setHeader(TelegramConstants.TELEGRAM_MEDIA_TYPE, TelegramMediaType.AUDIO.name());
+        ex.getIn().setHeader(TelegramConstants.TELEGRAM_MEDIA_TYPE, TelegramMediaType.AUDIO);
         byte[] audio = TelegramTestUtil.createSampleAudio();
         ex.getIn().setBody(audio);
 
@@ -136,7 +137,7 @@ public class TelegramProducerMediaTest extends TelegramTestSupport {
 
         Exchange ex = endpoint.createExchange();
         ex.getIn().setHeader(TelegramConstants.TELEGRAM_MEDIA_TITLE_CAPTION, "Document");
-        ex.getIn().setHeader(TelegramConstants.TELEGRAM_MEDIA_TYPE, TelegramMediaType.DOCUMENT.name());
+        ex.getIn().setHeader(TelegramConstants.TELEGRAM_MEDIA_TYPE, TelegramMediaType.DOCUMENT);
         byte[] document = TelegramTestUtil.createSampleDocument();
         ex.getIn().setBody(document);
 
@@ -151,6 +152,65 @@ public class TelegramProducerMediaTest extends TelegramTestSupport {
         assertEquals("Document", captor.getValue().getCaption());
     }
 
+    @Test
+    public void testRouteWithText() throws Exception {
+
+        TelegramService service = mockTelegramService();
+
+        Exchange ex = endpoint.createExchange();
+        ex.getIn().setHeader(TelegramConstants.TELEGRAM_MEDIA_TYPE, TelegramMediaType.TEXT.name());
+        ex.getIn().setBody("Hello");
+
+        context().createProducerTemplate().send(endpoint, ex);
+
+        ArgumentCaptor<OutgoingTextMessage> captor = ArgumentCaptor.forClass(OutgoingTextMessage.class);
+
+        Mockito.verify(service).sendMessage(eq("mock-token"), captor.capture());
+        assertEquals("my-id", captor.getValue().getChatId());
+        assertEquals("Hello", captor.getValue().getText());
+        assertNull(captor.getValue().getParseMode());
+    }
+
+    @Test
+    public void testRouteWithTextHtml() throws Exception {
+
+        TelegramService service = mockTelegramService();
+
+        Exchange ex = endpoint.createExchange();
+        ex.getIn().setHeader(TelegramConstants.TELEGRAM_MEDIA_TYPE, TelegramMediaType.TEXT.name());
+        ex.getIn().setHeader(TelegramConstants.TELEGRAM_PARSE_MODE, TelegramParseMode.HTML.name());
+        ex.getIn().setBody("Hello");
+
+        context().createProducerTemplate().send(endpoint, ex);
+
+        ArgumentCaptor<OutgoingTextMessage> captor = ArgumentCaptor.forClass(OutgoingTextMessage.class);
+
+        Mockito.verify(service).sendMessage(eq("mock-token"), captor.capture());
+        assertEquals("my-id", captor.getValue().getChatId());
+        assertEquals("Hello", captor.getValue().getText());
+        assertEquals("HTML", captor.getValue().getParseMode());
+    }
+
+    @Test
+    public void testRouteWithTextMarkdown() throws Exception {
+
+        TelegramService service = mockTelegramService();
+
+        Exchange ex = endpoint.createExchange();
+        ex.getIn().setHeader(TelegramConstants.TELEGRAM_MEDIA_TYPE, TelegramMediaType.TEXT.name());
+        ex.getIn().setHeader(TelegramConstants.TELEGRAM_PARSE_MODE, TelegramParseMode.MARKDOWN);
+        ex.getIn().setBody("Hello");
+
+        context().createProducerTemplate().send(endpoint, ex);
+
+        ArgumentCaptor<OutgoingTextMessage> captor = ArgumentCaptor.forClass(OutgoingTextMessage.class);
+
+        Mockito.verify(service).sendMessage(eq("mock-token"), captor.capture());
+        assertEquals("my-id", captor.getValue().getChatId());
+        assertEquals("Hello", captor.getValue().getText());
+        assertEquals("Markdown", captor.getValue().getParseMode());
+    }
+
     @Override
     protected RoutesBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {

http://git-wip-us.apache.org/repos/asf/camel/blob/2341e54f/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/integration/TelegramServiceTest.java
----------------------------------------------------------------------
diff --git a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/integration/TelegramServiceTest.java b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/integration/TelegramServiceTest.java
index 376a3ef..9209cb5 100644
--- a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/integration/TelegramServiceTest.java
+++ b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/integration/TelegramServiceTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.telegram.integration;
 
 import java.io.IOException;
 
+import org.apache.camel.component.telegram.TelegramParseMode;
 import org.apache.camel.component.telegram.TelegramService;
 import org.apache.camel.component.telegram.TelegramServiceProvider;
 import org.apache.camel.component.telegram.model.OutgoingAudioMessage;
@@ -68,6 +69,30 @@ public class TelegramServiceTest {
     }
 
     @Test
+    public void testSendMessageHtml() {
+        TelegramService service = TelegramServiceProvider.get().getService();
+
+        OutgoingTextMessage msg = new OutgoingTextMessage();
+        msg.setChatId(chatId);
+        msg.setText("This is a <b>HTML</b> <i>auto-generated</i> message from the Bot");
+        msg.setParseMode(TelegramParseMode.HTML.getCode());
+
+        service.sendMessage(authorizationToken, msg);
+    }
+
+    @Test
+    public void testSendMessageMarkdown() {
+        TelegramService service = TelegramServiceProvider.get().getService();
+
+        OutgoingTextMessage msg = new OutgoingTextMessage();
+        msg.setChatId(chatId);
+        msg.setText("This is a *Markdown* _auto-generated_ message from the Bot");
+        msg.setParseMode(TelegramParseMode.MARKDOWN.getCode());
+
+        service.sendMessage(authorizationToken, msg);
+    }
+
+    @Test
     public void testSendFull() {
         TelegramService service = TelegramServiceProvider.get().getService();