I'm using Telegram API and I've different methods which returns different type of information like this:
public SendMessage getTextMessage(Long chatId, String text) {
return new SendMessage()
.enableMarkdown(false)
.setChatId(chatId)
.setText(text);
}
public AnswerCallbackQuery getPopUpAnswer(String callbackId, String text) {
return new AnswerCallbackQuery()
.setCallbackQueryId(callbackId)
.setText(text)
.setShowAlert(true);
}
public EditMessageText getEditedMessage(Long chatId, Integer messageId, String text){
return new EditMessageText()
.setChatId(chatId)
.setMessageId(messageId)
.setText(text);
}
public SendPhoto getPhotoMessage(Long chatId, GooglePlayGame game) {
return new SendPhoto().setChatId(chatId)
.setPhoto(game.getPictureURL())
.setCaption(game.toString());
}
They have one parent PartialBotApiMethod
.
In main method I handle and execute (send) them:
PartialBotApiMethod<?> responseToUser = updateReceiver.handleUpdate(update);
try {
execute(responseToUser); //error
}
catch (TelegramApiException e){
e.printStackTrace();
}
But I can't execute PartialBotApiMethod object, only BotApiMethod and their subs. SendPhoto
is not in one hierarchy with others, that's why I can't put SendPhoto result to BotApiMethod type variable.
And I can't cast like this:
try {
execute((BotApiMethod)responseToUser);
}
catch (TelegramApiException e){
e.printStackTrace();
}
Because SendPhoto doesn't extends BotApiMethod and I'll give a ClassCastEx.
So I gonna use instanceof and cast "personally":
PartialBotApiMethod<?> responseToUser = updateReceiver.handleUpdate(update);
if (responseToUser instanceof SendMessage) {
try {
execute((SendMessage)responseToUser);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
else if (responseToUser instanceof SendPhoto) {
try {
execute((SendPhoto) responseToUser);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
else if (responseToUser instanceof AnswerCallbackQuery) {
try {
execute((AnswerCallbackQuery)responseToUser);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
else if (responseToUser instanceof EditMessageText) {
try {
execute((EditMessageText)responseToUser);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
Do I've better options or just stay with instanceof?
Can I replace instanceof if I cannot use a common interface (third pary API)?
UPDATE
updateReceiver.handleUpdate()
public PartialBotApiMethod handleUpdate(Update update) {
if (updateHasTextMessage(update)){
try {
return processInputMessage(update.getMessage());
} catch (NotSupportedChatCommandException e) {
return replyMessageService.getTextMessage(update.getMessage().getChatId(), "Not available");
}
}
else if (updateHasCallbackQuery(update)) {
return callbackQueryHandler.handleCallbackQuery(update.getCallbackQuery());
}
else {
return replyMessageService.getTextMessage(update.getMessage().getChatId(),
"Can't handle");
}
}
updateReceiver.handleUpdate
. Is it an API method or one you wrote? \$\endgroup\$