Skip to content

Commit

Permalink
Merge pull request #293 from moults31/master
Browse files Browse the repository at this point in the history
Fix Auth plus various issues
  • Loading branch information
bcorne authored Jan 6, 2022
2 parents 9705580 + 485bd40 commit 19f826a
Show file tree
Hide file tree
Showing 18 changed files with 497 additions and 146 deletions.
9 changes: 7 additions & 2 deletions samples/actions/src/main/java/actions/SendingMessages.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package actions;

import com.google.gson.JsonParser;
import com.ullink.slack.simpleslackapi.*;
import com.ullink.slack.simpleslackapi.replies.GenericSlackReply;
import com.ullink.slack.simpleslackapi.replies.SlackChannelReply;

/**
Expand Down Expand Up @@ -45,10 +47,13 @@ public void sendDirectMessageToAUserTheHardWay(SlackSession session)
SlackUser user = session.findUserByUserName("killroy");

//get its direct message channel
SlackMessageHandle<SlackChannelReply> reply = session.openDirectMessageChannel(user);
SlackMessageHandle<GenericSlackReply> reply = session.openDirectMessageChannel(user);
String channelAnswer = reply.getReply().getPlainAnswer();

//get the channel
SlackChannel channel = reply.getReply().getSlackChannel();
JsonParser parser = new JsonParser();
String channelId = parser.parse(channelAnswer).getAsJsonObject().get("channel").getAsJsonObject().get("id").getAsString();
SlackChannel channel = session.findChannelById(channelId);

//send the message to this channel
session.sendMessage(channel, "Hi, how are you", null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class SlackCustomConnection
{
public static void main(String[] args) throws IOException
{
SlackSession session = SlackSessionFactory.getSlackSessionBuilder("my-bot-auth-token")
SlackSession session = SlackSessionFactory.getSlackSessionBuilder("my-bot-auth-token", "my-bot-app-level-token")
.withProxy(Proxy.Type.HTTP, "my.proxy.address", 1234)
.withAutoreconnectOnDisconnection(false)
.withConnectionHeartbeat(10, TimeUnit.SECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class SlackDirectConnection
{
public static void main(String[] args) throws IOException
{
SlackSession session = SlackSessionFactory.createWebSocketSlackSession("my-bot-auth-token");
SlackSession session = SlackSessionFactory.createWebSocketSlackSession("my-bot-auth-token", "my-bot-app-level-token");
session.connect();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class SlackDirectConnectionWithBuilder
{
public static void main(String[] args) throws IOException
{
SlackSession session = SlackSessionFactory.getSlackSessionBuilder("my-bot-auth-token").build();
SlackSession session = SlackSessionFactory.getSlackSessionBuilder("my-bot-auth-token", "my-bot-app-level-token").build();
session.connect();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class SlackProxyConnection
{
public static void main(String[] args) throws IOException
{
SlackSession session = SlackSessionFactory.getSlackSessionBuilder("my-bot-auth-token")
SlackSession session = SlackSessionFactory.getSlackSessionBuilder("my-bot-auth-token", "my-bot-app-level-token")
.withProxy(Proxy.Type.HTTP, "my.proxy.address", 1234)
.build();
session.connect();
Expand Down
188 changes: 118 additions & 70 deletions simpleslackapi.ipr

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ public interface SlackPersona {
String getUserSkype();
String getUserPhone();
String getUserTitle();

/**
* To get the user's status
* @return The status of users
*/
String getStatusText();
/**
* To get the users statuses's emoji text
* @return The emoji text of users' statuses
*/
String getStatusEmoji();
boolean isDeleted();
boolean isAdmin();
boolean isOwner();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,13 @@ public interface SlackSession {
SlackMessageHandle<ParsedSlackReply> archiveChannel(String channelId);

SlackMessageHandle<ParsedSlackReply> unarchiveChannel(String channelId);
SlackMessageHandle<SlackChannelReply> openDirectMessageChannel(SlackUser user);

/**
* SlackChannelReply type does not work for subsequent operations, change it to GenericSlackReply type
* @param user: current slack user
* @return the replies in current channel
*/
SlackMessageHandle<GenericSlackReply> openDirectMessageChannel(SlackUser user);

SlackMessageHandle<SlackChannelReply> openMultipartyDirectMessageChannel(SlackUser... users);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,12 @@ public SlackMessageHandle<ParsedSlackReply> archiveChannel(String channelId) {
public SlackMessageHandle<ParsedSlackReply> unarchiveChannel(String channelId) {
return delegate.unarchiveChannel(channelId);
}

@Override public SlackMessageHandle<SlackChannelReply> openDirectMessageChannel(SlackUser user)
/**
* SlackChannelReply type does not work for subsequent operations, change it to GenericSlackReply type
* @param user: current slack user
* @return the replies in current channel
*/
@Override public SlackMessageHandle<GenericSlackReply> openDirectMessageChannel(SlackUser user)
{
return delegate.openDirectMessageChannel(user);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
public class ChannelHistoryModuleImpl implements ChannelHistoryModule {

private final SlackSession session;
private static final String FETCH_CHANNEL_HISTORY_COMMAND = "channels.history";
private static final String FETCH_GROUP_HISTORY_COMMAND = "groups.history";
private static final String FETCH_IM_HISTORY_COMMAND = "im.history";
/**
* Slack change its API call methods. Now the corresponding history is retrieved by conversation...
*/
private static final String FETCH_CHANNEL_HISTORY_COMMAND = "conversations.history";
private static final String FETCH_GROUP_HISTORY_COMMAND = "conversations.history";
private static final String FETCH_IM_HISTORY_COMMAND = "conversations.history";
private static final int DEFAULT_HISTORY_FETCH_SIZE = 1000;

public ChannelHistoryModuleImpl(SlackSession session) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ public String getUserTitle()
{
return null;
}
/**
* To get the user's status
* @return The status of users
* CS427 Issue Link: https://github.com/Itiviti/simple-slack-api/issues/196
*/
@Override
public String getStatusText()
{
return null;
}
/**
* To get the users statuses's emoji text
* @return The emoji text of users' statuses
* CS427 Issue Link: https://github.com/Itiviti/simple-slack-api/issues/196
*/
@Override
public String getStatusEmoji()
{
return null;
}

@Override
public String getName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ private SlackJSONParsingUtils() {
// Helper class
}

/**
* Add the status and emoji
* CS427 Issue Link: https://github.com/Itiviti/simple-slack-api/issues/196
* @param jsonUser
* @return
*/
static SlackUser buildSlackUser(JsonObject jsonUser)
{
String id = GsonHelper.getStringOrNull(jsonUser.get("id"));
Expand All @@ -39,13 +45,17 @@ static SlackUser buildSlackUser(JsonObject jsonUser)
String title = "";
String phone = "";
String presence = "";
String statusText = "";
String statusEmoji = "";
if (profileJSON !=null && !profileJSON.isJsonNull())
{
email = GsonHelper.getStringOrNull(profileJSON.get("email"));
skype = GsonHelper.getStringOrNull(profileJSON.get("skype"));
title = GsonHelper.getStringOrNull(profileJSON.get("title"));
phone = GsonHelper.getStringOrNull(profileJSON.get("phone"));
presence = GsonHelper.getStringOrNull(profileJSON.get("presence"));
statusText = GsonHelper.getStringOrNull(profileJSON.get("status_text"));
statusEmoji = GsonHelper.getStringOrNull(profileJSON.get("status_emoji"));
}
SlackPresence slackPresence = SlackPresence.UNKNOWN;
if ("active".equals(presence))
Expand All @@ -63,6 +73,8 @@ static SlackUser buildSlackUser(JsonObject jsonUser)
.phone(phone)
.skype(skype)
.title(title)
.statusText(statusText)
.statusEmoji(statusEmoji)
.build();

return SlackPersonaImpl.builder()
Expand All @@ -82,7 +94,12 @@ static SlackUser buildSlackUser(JsonObject jsonUser)
.build();
}

static SlackChannel buildSlackChannel(JsonObject jsonChannel, Map<String, SlackUser> knownUsersById) {
/**
* No need to add user to members, others are the same
* @param jsonChannel
* @return
*/
static SlackChannel buildSlackChannel(JsonObject jsonChannel) {
String id = GsonHelper.getStringOrNull(jsonChannel.get("id"));
String name = GsonHelper.getStringOrNull(jsonChannel.get("name"));

Expand All @@ -106,16 +123,7 @@ static SlackChannel buildSlackChannel(JsonObject jsonChannel, Map<String, SlackU
isArchived = jsonChannel.get("is_archived").getAsBoolean();
}

SlackChannel toReturn = new SlackChannel(id, name, topic, purpose, false, isMember, isArchived);
JsonArray membersJson = GsonHelper.getJsonArrayOrNull(jsonChannel.get("members"));
if (membersJson != null) {
for (JsonElement jsonMembersObject : membersJson) {
String memberId = jsonMembersObject.getAsString();
SlackUser user = knownUsersById.get(memberId);
toReturn.addUser(user);
}
}
return toReturn;
return new SlackChannel(id, name, topic, purpose, false, isMember, isArchived);
}

static SlackChannel buildSlackImChannel(JsonObject jsonChannel, Map<String, SlackUser> knownUsersById)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,10 @@ void parse()
}

JsonArray channelsJson = jsonResponse.get("channels").getAsJsonArray();

for (JsonElement jsonObject : channelsJson)
{
JsonObject jsonChannel = jsonObject.getAsJsonObject();
SlackChannel channel = SlackJSONParsingUtils.buildSlackChannel(jsonChannel, users);
SlackChannel channel = SlackJSONParsingUtils.buildSlackChannel(jsonChannel);
LOGGER.debug("slack public channel found : " + channel.getId());
channels.put(channel.getId(), channel);
}
Expand All @@ -105,7 +104,7 @@ void parse()
for (JsonElement jsonObject : groupsJson)
{
JsonObject jsonChannel = jsonObject.getAsJsonObject();
SlackChannel channel = SlackJSONParsingUtils.buildSlackChannel(jsonChannel, users);
SlackChannel channel = SlackJSONParsingUtils.buildSlackChannel(jsonChannel);
LOGGER.debug("slack private group found : " + channel.getId());
channels.put(channel.getId(), channel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ public String getUserPhone() {
public String getUserTitle() {
return profile.getTitle();
}
/**
* To get the user's status
* CS427 Issue Link: https://github.com/Itiviti/simple-slack-api/issues/279
* @return The status of users
*/
@Override
public String getStatusText()
{
return profile.getStatusText();
}
/**
* To get the users status's emoji text
* CS427 Issue Link: https://github.com/Itiviti/simple-slack-api/issues/279
* @return The emoji text of users' statuses
*/
@Override
public String getStatusEmoji()
{
return profile.getStatusEmoji();
}

@Override
public SlackPresence getPresence() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,34 @@
import com.ullink.slack.simpleslackapi.WebSocketContainerProvider;

public class SlackSessionFactory {
public static SlackSession createWebSocketSlackSession(String authToken)
/**
* Add a new variable because Slack Api change the way to be called. Now it requires auth token and app level token
* @param authToken
* @param appLevelToken
* @return
*/
public static SlackSession createWebSocketSlackSession(String authToken, String appLevelToken)
{
return new SlackWebSocketSessionImpl(null, authToken, null, true, true, 0, null);
return new SlackWebSocketSessionImpl(null, authToken, appLevelToken, null, true, true, 0, null);
}

public static SlackSessionFactoryBuilder getSlackSessionBuilder(String authToken) {
return new SlackSessionFactoryBuilder(authToken);
/**
* Add a new variable because Slack Api change the way to be called. Now it requires auth token and app level token
* CS427 Issue Link: https://github.com/Itiviti/simple-slack-api/issues/283
* @param authToken
* @param appLevelToken
* @return
*/
public static SlackSessionFactoryBuilder getSlackSessionBuilder(String authToken, String appLevelToken) {
return new SlackSessionFactoryBuilder(authToken, appLevelToken);
}

public static class SlackSessionFactoryBuilder {

private String authToken;
/**
* Add new variable -appLevelToken
*/
private String appLevelToken;
private String slackBaseApi;
private Proxy.Type proxyType;
private String proxyAddress;
Expand All @@ -31,8 +47,15 @@ public static class SlackSessionFactoryBuilder {
private boolean autoreconnection;
private boolean rateLimitSupport = true;

private SlackSessionFactoryBuilder(String authToken) {
/**
* Add new variable -appLevelToken
* CS427 Issue Link: https://github.com/Itiviti/simple-slack-api/issues/283
* @param authToken
* @param appLevelToken
*/
private SlackSessionFactoryBuilder(String authToken, String appLevelToken) {
this.authToken = authToken;
this.appLevelToken = appLevelToken;
}

public SlackSessionFactoryBuilder withBaseApiUrl(String slackBaseApi) {
Expand Down Expand Up @@ -78,7 +101,7 @@ public SlackSessionFactoryBuilder withRateLimitSupport(boolean rateLimitSupport)
}

public SlackSession build() {
return new SlackWebSocketSessionImpl(provider, authToken, slackBaseApi, proxyType, proxyAddress, proxyPort, proxyUser, proxyPassword, autoreconnection, rateLimitSupport, heartbeat, unit);
return new SlackWebSocketSessionImpl(provider, authToken, appLevelToken, slackBaseApi, proxyType, proxyAddress, proxyPort, proxyUser, proxyPassword, autoreconnection, rateLimitSupport, heartbeat, unit);
}
}
}
Loading

0 comments on commit 19f826a

Please sign in to comment.