/** * Fetches the user who caused the update. * * @param update a Telegram {@link Update} * @return the originating user * @throws IllegalStateException if the user could not be found */ public static User getUser(Update update) { if (MESSAGE.test(update)) { return update.getMessage().getFrom(); } else if (CALLBACK_QUERY.test(update)) { return update.getCallbackQuery().getFrom(); } else if (INLINE_QUERY.test(update)) { return update.getInlineQuery().getFrom(); } else if (CHANNEL_POST.test(update)) { return update.getChannelPost().getFrom(); } else if (EDITED_CHANNEL_POST.test(update)) { return update.getEditedChannelPost().getFrom(); } else if (EDITED_MESSAGE.test(update)) { return update.getEditedMessage().getFrom(); } else if (CHOSEN_INLINE_QUERY.test(update)) { return update.getChosenInlineQuery().getFrom(); } else { throw new IllegalStateException("Could not retrieve originating user from update"); } }
/** * Fetches the direct chat ID of the specified update. * * @param update a Telegram {@link Update} * @return the originating chat ID * @throws IllegalStateException if the chat ID could not be found */ public static Long getChatId(Update update) { if (MESSAGE.test(update)) { return update.getMessage().getChatId(); } else if (CALLBACK_QUERY.test(update)) { return update.getCallbackQuery().getMessage().getChatId(); } else if (INLINE_QUERY.test(update)) { return (long) update.getInlineQuery().getFrom().getId(); } else if (CHANNEL_POST.test(update)) { return update.getChannelPost().getChatId(); } else if (EDITED_CHANNEL_POST.test(update)) { return update.getEditedChannelPost().getChatId(); } else if (EDITED_MESSAGE.test(update)) { return update.getEditedMessage().getChatId(); } else if (CHOSEN_INLINE_QUERY.test(update)) { return (long) update.getChosenInlineQuery().getFrom().getId(); } else { throw new IllegalStateException("Could not retrieve originating chat ID from update"); } }
@Override public void onUpdateReceived(Update update) { try { if (update.hasInlineQuery()) { handleIncomingInlineQuery(update.getInlineQuery()); } else if (update.hasMessage() && update.getMessage().isUserMessage()) { try { execute(getHelpMessage(update.getMessage())); } catch (TelegramApiException e) { BotLogger.error(LOGTAG, e); } } } catch (Exception e) { BotLogger.error(LOGTAG, e); } }