Skip to content

Commit

Permalink
feat(new-rocket-api): Implement message sending (see #JENKINS-40595)
Browse files Browse the repository at this point in the history
  • Loading branch information
hypery2k committed Dec 27, 2016
1 parent 52c6316 commit 162812b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ public class Message {

private String msg;

public Message() {
}

public Message(String messsage) {
this.msg = messsage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public interface RocketChatClient {

void send(Room room, String message) throws ValidatorException, IOException;

void send(String room, String message) throws ValidatorException, IOException;
/**
*
* @param channelName
* @param message
* @throws ValidatorException
* @throws IOException
*/
void send(String channelName, String message) throws ValidatorException, IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import sun.security.validator.ValidatorException;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
* Client for Rocket.Chat which relies on the REST API v1.
Expand Down Expand Up @@ -69,11 +71,18 @@ public Room[] getChannels() throws IOException {

@Override
public void send(Room room, String message) throws ValidatorException, IOException {

this.send(room.getName(), message);
}

@Override
public void send(String room, String message) throws ValidatorException, IOException {

public void send(String channelName, String message) throws ValidatorException, IOException {
Map body = new HashMap<String, String>();
body.put("channel", "#" + channelName);
body.put("text", message);
Response res = this.callBuilder.buildCall(RocketChatRestApiV1.PostMessage,
null, body);
if (!res.isSuccessful()) {
throw new IOException("The send of the message was unsuccessful.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public enum RocketChatRestApiV1 {
/**
* Retrieves a list of all the channels in the server.
*/
ChannelsInfo("channels.info", HttpMethods.GET, true);
ChannelsInfo("channels.info", HttpMethods.GET, true),
/**
* Retrieves a list of all the channels in the server.
*/
PostMessage("chat.postMessage", HttpMethods.POST, true);

private String methodName;
private HttpMethods httpMethod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ public void shouldReadRooms() throws Exception {
Room[] rooms = this.client.getChannels();
assertThat(rooms.length, is(1));
}

@Test
public void shouldSendMessage() throws Exception {
this.client.send("general", "test");
}


}

0 comments on commit 162812b

Please sign in to comment.