Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.slack.api.rtm.message;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.Instant;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PingMessage implements RTMMessage {

public static final String TYPE_NAME = "ping";

private Long id;
private final String type = TYPE_NAME;
private Instant time;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import com.slack.api.model.User;
import com.slack.api.model.event.HelloEvent;
import com.slack.api.model.event.MessageBotEvent;
import com.slack.api.model.event.PongEvent;
import com.slack.api.model.event.UserTypingEvent;
import com.slack.api.rtm.*;
import com.slack.api.rtm.message.Message;
import com.slack.api.rtm.message.PingMessage;
import com.slack.api.rtm.message.PresenceQuery;
import com.slack.api.rtm.message.PresenceSub;
import com.slack.api.rtm.message.Typing;
Expand All @@ -30,7 +32,9 @@
import javax.websocket.DeploymentException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.time.Instant;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

import static org.hamcrest.CoreMatchers.*;
Expand Down Expand Up @@ -301,4 +305,49 @@ public void handle(String message) {
assertThat(counter.get(), is(greaterThanOrEqualTo(5)));
}


@Test
public void givenRTMClient_whenPing_ensureReceivesPong() throws Exception {

// given
SlackConfig config = new SlackConfig();
config.setLibraryMaintainerMode(false);
config.getHttpClientResponseHandlers().add(new JsonDataRecordingListener());
Slack slack = Slack.getInstance(config);

final Instant now = Instant.now();
final long pingId = now.toEpochMilli();

class PongReceived { PongEvent event = null; }
final PongReceived pongReceived = new PongReceived();

RTMEventsDispatcher dispatcher = RTMEventsDispatcherFactory.getInstance();
dispatcher.register(new RTMEventHandler<PongEvent>() {
@Override public void handle(PongEvent event) {
if (Objects.equals(event.getReply_to(), pingId)) {
synchronized(pongReceived) {
pongReceived.event = event;
pongReceived.notifyAll();
}
}
}
});

try (RTMClient rtm = slack.rtmStart(classicAppBotToken)) {
rtm.connect();
rtm.addMessageHandler(dispatcher.toMessageHandler());

// when
rtm.sendMessage(PingMessage.builder().id(pingId).time(now).build().toJSONString());

// ensure
synchronized(pongReceived) {
pongReceived.wait(5000L);
}
assertThat(pongReceived.event, notNullValue());
assertThat(pongReceived.event.getReply_to(), equalTo(pingId));
assertThat(pongReceived.event.getTime(), equalTo(now));
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.slack.api.model.event;

import lombok.Data;

import java.time.Instant;

/**
* The pong event is sent in response to a 'ping' message previously sent. The id and
* other fields will match that of the ping message.
* <p>
* https://api.slack.com/rtm
*/
@Data
public class PongEvent implements Event
{
public static final String TYPE_NAME = "pong";
private final String type = TYPE_NAME;

private final Long reply_to;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you define this field as private Long replyTo (camelCase + no need to have final for this) instead? Lombok generates Long getReplyTo() and void setReplyTo(Long) for you.

Regarding the conversion from snake_cased JSON keys, Gson in this project automatically translates snake_case to camelCase. https://github.com/slackapi/java-slack-sdk/blob/v1.0.7/slack-api-client/src/main/java/com/slack/api/rtm/RTMEventsDispatcherImpl.java#L53

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks - good to know!

private final Instant time;
}