simple-slack-api icon indicating copy to clipboard operation
simple-slack-api copied to clipboard

ClassCastException: SlackReplyImpl cannot be cast to SlackMessageReply

Open sixcorners opened this issue 8 years ago • 10 comments

I get ClassCastException: SlackReplyImpl cannot be cast to SlackMessageReply when calling slackSession.sendMessage() on an archived channel.

sixcorners avatar Jan 04 '17 20:01 sixcorners

Same here, seems to happen when the response from Slack is not ok and thus there is no full SlackMessageReply, only a SlackReply.

            final SlackMessageHandle<SlackMessageReply> handler = session.sendMessage(channel, null, attachment);
            handler.waitForReply(1, TimeUnit.MINUTES);

            if(!handler.getReply().isOk()) {
                 ....

To be honest, the strange use of generics in the API is quite confusing. I need to instantiate it with SlackMessasgeReply as return-value, but I get back some other class when I actually access it!?

centic9 avatar Jan 23 '17 13:01 centic9

FYI, I worked around the issue as follows:

            ParsedSlackReply reply = ((SlackMessageHandle<? extends ParsedSlackReply>)handler).getReply();
            if (!reply.isOk()) {
                throw new IOException("Failed to send message to slack: " + reply.getErrorMessage());
            }

centic9 avatar Jan 23 '17 16:01 centic9

Hi, on which version of the library did you had the issue?

bcorne avatar Jan 23 '17 16:01 bcorne

Both on 0.5.1 and 0.6.0

centic9 avatar Jan 23 '17 17:01 centic9

Ok thanks, I'll have a look at that

bcorne avatar Jan 23 '17 17:01 bcorne

FWIW: still persists on 1.0.0

Yuki-Inoue avatar Sep 13 '17 03:09 Yuki-Inoue

If you look at SlackJSONReplyParser.decode(), you can see where it defaults to creating SlackReplyImpl object in the error case. Methods like isChannel() return false in the error case because the calling details have been lost.

dcrall avatar Oct 23 '17 19:10 dcrall

+1

Akuka avatar Aug 24 '18 12:08 Akuka

+1

victoriaaa234 avatar Jan 23 '20 22:01 victoriaaa234

Hello @bcorne

Occurring on Itiviti/Ullink Ops bot as well

Basically when the bot is not present on a channel which is however flagged in the DB as a channel listening to the project https://github.com/Itiviti/slack4gerrit/blob/master/src/main/java/jobs/PublishMessageJob.java#L98 (might happen if the bot was removed from the channel...), then it does not post anything to Slack and logs this error "java.lang.ClassCastException: com.ullink.slack.simpleslackapi.replies.SlackReplyImpl cannot be cast to com.ullink.slack.simpleslackapi.replies.SlackMessageReply", which is meaningless indeed, when calling line https://github.com/Itiviti/slack4gerrit/blob/master/src/main/java/jobs/PublishMessageJob.java#L109

I did a local workaround inspired from the comments here (many thanks @centic9 ) but we would probably need a more robust way of handling this pb (with some explicit logging / slack msg): ReviewRequest previousRequest = reviewRequestService.getReviewRequest(channel.getId(), changeId); // Workaround for https://github.com/Itiviti/simple-slack-api/issues/152 ParsedSlackReply reply = handle.getReply(); if (!reply.isOk()) { if ("not_in_channel".equals(reply.getErrorMessage())) { throw new IOException("Failed to send message to slack (" + reply.getErrorMessage() + "): please add bot to channel *" + (channel.getName() != null ? channel.getName() : channel) + "* and try again"); } throw new IOException("Failed to send message to slack: " + reply.getErrorMessage()); } // End of workaround for https://github.com/Itiviti/simple-slack-api/issues/152 ReviewRequest newRequest = new ReviewRequest(handle.getReply().getTimestamp(), changeId, channel.getId()); ... if (e.getMessage() != null && e.getMessage().contains("please add bot to channel")) { // Workaround for https://github.com/Itiviti/simple-slack-api/issues/152 session.sendMessage(fromChannel, e.getMessage(), null, SlackChatConfiguration.getConfiguration().asUser()); // End of workaround for https://github.com/Itiviti/simple-slack-api/issues/152 } else { session.sendMessage(fromChannel, "Could not find change id *" + changeId + "*, check that the change id is valid and does not correspond to a draft", null, SlackChatConfiguration.getConfiguration().asUser()); } LOGGER.error("Could not publish review for change id " + changeId, e);

Many thx

vguiot avatar Apr 15 '20 09:04 vguiot