You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2019/01/07 14:14:08 UTC

[camel] 01/03: CAMEL-13035 update from telegram channel

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

acosentino pushed a commit to branch camel-2.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 2becd62a9f2930f81a22f9be5a7f4fe2f155170a
Author: Gandhi <vg...@LAMU02W82E9HTD6.uhc.com>
AuthorDate: Fri Jan 4 17:22:31 2019 +0530

    CAMEL-13035 update from telegram channel
---
 .../camel/component/telegram/TelegramEndpoint.java |  6 ++
 .../camel/component/telegram/model/Update.java     | 18 +++++-
 .../telegram/TelegramConsumerChannelPostTest.java  | 71 ++++++++++++++++++++++
 .../resources/messages/updates-channelMessage.json | 19 ++++++
 4 files changed, 113 insertions(+), 1 deletion(-)

diff --git a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramEndpoint.java b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramEndpoint.java
index f98394c..d90702d 100644
--- a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramEndpoint.java
+++ b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramEndpoint.java
@@ -61,6 +61,12 @@ public class TelegramEndpoint extends ScheduledPollEndpoint {
             if (update.getMessage().getChat() != null) {
                 exchange.getIn().setHeader(TelegramConstants.TELEGRAM_CHAT_ID, update.getMessage().getChat().getId());
             }
+        }else if(update.getChannelPost() != null) {
+        	exchange.getIn().setBody(update.getChannelPost());
+
+            if (update.getChannelPost().getChat() != null) {
+                exchange.getIn().setHeader(TelegramConstants.TELEGRAM_CHAT_ID, update.getChannelPost().getChat().getId());
+            }
         }
 
         return exchange;
diff --git a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/Update.java b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/Update.java
index 01b0a99..e7c814a 100644
--- a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/Update.java
+++ b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/Update.java
@@ -32,9 +32,15 @@ public class Update implements Serializable {
     @JsonProperty("update_id")
     private Long updateId;
 
+
     private IncomingMessage message;
+    
+    
+    @JsonProperty("channel_post")
+    private IncomingMessage channelpost;
 
-    public Update() {
+   
+	public Update() {
     }
 
     public Long getUpdateId() {
@@ -52,12 +58,22 @@ public class Update implements Serializable {
     public void setMessage(IncomingMessage message) {
         this.message = message;
     }
+    
+    public IncomingMessage getChannelPost() {
+		return channelpost;
+	}
+
+	public void setChannelpost(IncomingMessage channelpost) {
+		this.channelpost = channelpost;
+	}
+
 
     @Override
     public String toString() {
         final StringBuilder sb = new StringBuilder("Update{");
         sb.append("updateId=").append(updateId);
         sb.append(", message=").append(message);
+        sb.append(", channel_post=").append(channelpost);
         sb.append('}');
         return sb.toString();
     }
diff --git a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramConsumerChannelPostTest.java b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramConsumerChannelPostTest.java
new file mode 100644
index 0000000..22d15e7
--- /dev/null
+++ b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramConsumerChannelPostTest.java
@@ -0,0 +1,71 @@
+package org.apache.camel.component.telegram;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+import java.time.Instant;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.telegram.model.Chat;
+import org.apache.camel.component.telegram.model.IncomingMessage;
+import org.apache.camel.component.telegram.model.UpdateResult;
+import org.apache.camel.component.telegram.util.TelegramTestSupport;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TelegramConsumerChannelPostTest extends TelegramTestSupport{
+
+	@EndpointInject(uri = "mock:telegram")
+    private MockEndpoint endpoint;
+
+    @Before
+    public void mockAPIs() {
+        TelegramService api = mockTelegramService();
+
+        UpdateResult res1 = getJSONResource("messages/updates-channelMessage.json", UpdateResult.class);
+
+        UpdateResult defaultRes = getJSONResource("messages/updates-empty.json", UpdateResult.class);
+
+        when(api.getUpdates(any(), any(), any(), any())).thenReturn(res1).thenAnswer((i) -> defaultRes);
+    }
+    
+    @Test
+    public void testReceptionOfMessageWithAMessage() throws Exception {
+        endpoint.expectedMinimumMessageCount(1);
+        endpoint.assertIsSatisfied();
+
+        Exchange mediaExchange = endpoint.getExchanges().get(0);
+        IncomingMessage msg = mediaExchange.getIn().getBody(IncomingMessage.class);
+        
+        assertEquals("-1001245756934", mediaExchange.getIn().getHeader(TelegramConstants.TELEGRAM_CHAT_ID));
+        
+        //checking body
+        assertNotNull(msg);
+        assertEquals("test", msg.getText());
+        assertEquals(Long.valueOf(67L), msg.getMessageId());
+        assertEquals(Instant.ofEpochSecond(1546505413L), msg.getDate());
+        
+        // checking chat
+        Chat chat = msg.getChat();
+        assertNotNull(chat);
+        assertEquals("-1001245756934", chat.getId());
+        assertEquals("cameltemp", chat.getTitle());
+        assertEquals("channel", chat.getType());
+
+    }
+    
+    @Override
+    protected RoutesBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("telegram:bots/mock-token")
+                        .to("mock:telegram");
+            }
+        };
+    }
+}
diff --git a/components/camel-telegram/src/test/resources/messages/updates-channelMessage.json b/components/camel-telegram/src/test/resources/messages/updates-channelMessage.json
new file mode 100644
index 0000000..c1d3118
--- /dev/null
+++ b/components/camel-telegram/src/test/resources/messages/updates-channelMessage.json
@@ -0,0 +1,19 @@
+{
+    "ok": true,
+    "result": [
+        {
+            "update_id": 219398823,
+            "channel_post": {
+                "message_id": 67,
+                "chat": {
+                    "id": -1001245756934,
+                    "title": "cameltemp",
+                    "username": "cameltelegram",
+                    "type": "channel"
+                },
+                "date": 1546505413,
+                "text": "test"
+            }
+        }
+    ]
+}
\ No newline at end of file