@Override public void onUpdateReceived(Update update) { Optional<Session> chatSession; Message message; if (update.hasMessage()) { message = update.getMessage(); } else if (update.hasCallbackQuery()) { message = update.getCallbackQuery().getMessage(); } else { chatSession = Optional.empty(); onUpdateReceived(update, chatSession); return; } chatIdConverter.setSessionId(message.getChatId()); chatSession = this.getSession(message); onUpdateReceived(update, chatSession); }
/** * 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"); } }
String[] data = callbackquery.getData().split(":"); int index = Integer.parseInt(data[2]); editMarkup.setChatId(callbackquery.getMessage().getChatId().toString()); editMarkup.setInlineMessageId(callbackquery.getInlineMessageId()); editMarkup.setText("[](" + this.urls.get(index)[1] + ")"); editMarkup.enableMarkdown(true); editMarkup.setMessageId(callbackquery.getMessage().getMessageId()); editMarkup.setReplyMarkup(markup); try {
/** * * @param text The text that should be shown * @param alert If the text should be shown as a alert or not * @param callbackquery * @throws TelegramApiException */ private void sendAnswerCallbackQuery(String text, boolean alert, CallbackQuery callbackquery) throws TelegramApiException{ AnswerCallbackQuery answerCallbackQuery = new AnswerCallbackQuery(); answerCallbackQuery.setCallbackQueryId(callbackquery.getId()); answerCallbackQuery.setShowAlert(alert); answerCallbackQuery.setText(text); execute(answerCallbackQuery); }
/** * @param update a Telegram {@link Update} * @return <tt>true</tt> if the update contains contains a private user message */ public static boolean isUserMessage(Update update) { if (MESSAGE.test(update)) { return update.getMessage().isUserMessage(); } else if (CALLBACK_QUERY.test(update)) { return update.getCallbackQuery().getMessage().isUserMessage(); } else if (CHANNEL_POST.test(update)) { return update.getChannelPost().isUserMessage(); } else if (EDITED_CHANNEL_POST.test(update)) { return update.getEditedChannelPost().isUserMessage(); } else if (EDITED_MESSAGE.test(update)) { return update.getEditedMessage().isUserMessage(); } else if (CHOSEN_INLINE_QUERY.test(update) || INLINE_QUERY.test(update)) { return true; } else { throw new IllegalStateException("Could not retrieve update context origin (user/group)"); } }
/** * A "best-effort" boolean stating whether the update is a group message or not. * * @param update a Telegram {@link Update} * @return whether the update is linked to a group */ public static boolean isGroupUpdate(Update update) { if (MESSAGE.test(update)) { return update.getMessage().isGroupMessage(); } else if (CALLBACK_QUERY.test(update)) { return update.getCallbackQuery().getMessage().isGroupMessage(); } else if (CHANNEL_POST.test(update)) { return update.getChannelPost().isGroupMessage(); } else if (EDITED_CHANNEL_POST.test(update)) { return update.getEditedChannelPost().isGroupMessage(); } else if (EDITED_MESSAGE.test(update)) { return update.getEditedMessage().isGroupMessage(); } else { return false; } }
/** * A "best-effort" boolean stating whether the update is a super-group message or not. * * @param update a Telegram {@link Update} * @return whether the update is linked to a group */ public static boolean isSuperGroupUpdate(Update update) { if (MESSAGE.test(update)) { return update.getMessage().isSuperGroupMessage(); } else if (CALLBACK_QUERY.test(update)) { return update.getCallbackQuery().getMessage().isSuperGroupMessage(); } else if (CHANNEL_POST.test(update)) { return update.getChannelPost().isSuperGroupMessage(); } else if (EDITED_CHANNEL_POST.test(update)) { return update.getEditedChannelPost().isSuperGroupMessage(); } else if (EDITED_MESSAGE.test(update)) { return update.getEditedMessage().isSuperGroupMessage(); } else { return false; } }
/** * 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"); } }