diff --git a/src/main/java/com/ullink/slack/simpleslackapi/SlackChannelCreated.java b/src/main/java/com/ullink/slack/simpleslackapi/SlackChannelCreated.java new file mode 100644 index 00000000..e12080b8 --- /dev/null +++ b/src/main/java/com/ullink/slack/simpleslackapi/SlackChannelCreated.java @@ -0,0 +1,6 @@ +package com.ullink.slack.simpleslackapi; + +public interface SlackChannelCreated +{ + SlackChannel getSlackChannel(); +} diff --git a/src/main/java/com/ullink/slack/simpleslackapi/impl/AbstractSlackSessionImpl.java b/src/main/java/com/ullink/slack/simpleslackapi/impl/AbstractSlackSessionImpl.java index dc5c8946..78f2b752 100644 --- a/src/main/java/com/ullink/slack/simpleslackapi/impl/AbstractSlackSessionImpl.java +++ b/src/main/java/com/ullink/slack/simpleslackapi/impl/AbstractSlackSessionImpl.java @@ -17,11 +17,11 @@ abstract class AbstractSlackSessionImpl implements SlackSession { - protected Map channels = new HashMap<>(); - protected Map users = new HashMap<>(); - protected Map bots = new HashMap<>(); + protected Map channels = new HashMap<>(); + protected Map users = new HashMap<>(); + protected Map bots = new HashMap<>(); - protected Set messageListeners = new HashSet<>(); + protected Set messageListeners = new HashSet<>(); static final SlackChatConfiguration DEFAULT_CONFIGURATION = SlackChatConfiguration.getConfiguration().asUser(); @@ -59,7 +59,16 @@ public SlackChannel findChannelByName(String channelName) @Override public SlackChannel findChannelById(String channelId) { - return channels.get(channelId); + SlackChannel toReturn = channels.get(channelId); + if (toReturn == null) + { + // direct channel case + if (channelId != null && channelId.startsWith("D")) + { + toReturn = new SlackChannelImpl(channelId, "", "", ""); + } + } + return toReturn; } @Override diff --git a/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackChannelCreatedImpl.java b/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackChannelCreatedImpl.java new file mode 100644 index 00000000..1b1472b6 --- /dev/null +++ b/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackChannelCreatedImpl.java @@ -0,0 +1,21 @@ +package com.ullink.slack.simpleslackapi.impl; + +import com.ullink.slack.simpleslackapi.SlackChannel; +import com.ullink.slack.simpleslackapi.SlackChannelCreated; + +public class SlackChannelCreatedImpl implements SlackChannelCreated +{ + private SlackChannel slackChannel; + + SlackChannelCreatedImpl(SlackChannel slackChannel) + { + this.slackChannel = slackChannel; + } + + @Override + public SlackChannel getSlackChannel() + { + return slackChannel; + } + +} diff --git a/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackWebSocketSessionImpl.java b/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackWebSocketSessionImpl.java index 30e61de4..dfabf713 100644 --- a/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackWebSocketSessionImpl.java +++ b/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackWebSocketSessionImpl.java @@ -36,6 +36,7 @@ import com.google.common.io.CharStreams; import com.ullink.slack.simpleslackapi.SlackAttachment; import com.ullink.slack.simpleslackapi.SlackChannel; +import com.ullink.slack.simpleslackapi.SlackChannelCreated; import com.ullink.slack.simpleslackapi.SlackGroupJoined; import com.ullink.slack.simpleslackapi.SlackMessage; import com.ullink.slack.simpleslackapi.SlackMessageHandle; @@ -402,7 +403,7 @@ else if ("message".equals(type)) } else if ("group_joined".equals(type)) { - SlackGroupJoined groupJoined = parseGroupJoined(object); + SlackGroupJoined groupJoined = new SlackGroupJoinedImpl(parseChannelDescription(object)); if (groupJoined != null) { SlackChannel channel = groupJoined.getSlackChannel(); @@ -412,14 +413,26 @@ else if ("group_joined".equals(type)) } } } + else if ("channel_created".equals(type)) + { + SlackChannelCreated channelCreated = new SlackChannelCreatedImpl(parseChannelDescription(object)); + if (channelCreated != null) + { + SlackChannel channel = channelCreated.getSlackChannel(); + if (channel != null) + { + channels.put(channel.getId(), channel); + } + } + } } } - private SlackGroupJoined parseGroupJoined(JSONObject object) + private SlackChannel parseChannelDescription(JSONObject object) { JSONObject channel = (JSONObject) object.get("channel"); SlackChannel slackChannel = SlackJSONParsingUtils.buildSlackChannel(channel, users); - return new SlackGroupJoinedImpl(slackChannel); + return slackChannel; } private JSONObject parseObject(String json)