Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAMEL-13035 update from telegram channel #2698

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -61,6 +61,12 @@ public Exchange createExchange(Update update) {
if (update.getMessage().getChat() != null) {
exchange.getIn().setHeader(TelegramConstants.TELEGRAM_CHAT_ID, update.getMessage().getChat().getId());
}
}else if(update.getChannelPost() != null) {
Copy link
Member

@dmvolod dmvolod Jan 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still code style issues, please run mvn with -Psoursecheck option and squash commits after fix and before push (use -f git option) them again.

exchange.getIn().setBody(update.getChannelPost());

if (update.getChannelPost().getChat() != null) {
exchange.getIn().setHeader(TelegramConstants.TELEGRAM_CHAT_ID, update.getChannelPost().getChat().getId());
}
}

return exchange;
Expand Down
Expand Up @@ -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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possible checkstyle error

}

public Long getUpdateId() {
Expand All @@ -52,12 +58,22 @@ public IncomingMessage getMessage() {
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();
}
Expand Down
@@ -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");
}
};
}
}
@@ -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"
}
}
]
}