simple-slack-api
simple-slack-api copied to clipboard
ClassCastException: SlackReplyImpl cannot be cast to SlackMessageReply
I get ClassCastException: SlackReplyImpl cannot be cast to SlackMessageReply when calling slackSession.sendMessage() on an archived channel.
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!?
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());
}
Hi, on which version of the library did you had the issue?
Both on 0.5.1 and 0.6.0
Ok thanks, I'll have a look at that
FWIW: still persists on 1.0.0
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.
+1
+1
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