Trio<Update, Ability, String[]> getAbility(Update update) { // Handle updates without messages // Passing through this function means that the global flags have passed Message msg = update.getMessage(); if (!update.hasMessage() || !msg.hasText()) return Trio.of(update, abilities.get(DEFAULT), new String[]{}); String[] tokens = msg.getText().split(" "); if (tokens[0].startsWith("/")) { String abilityToken = stripBotUsername(tokens[0].substring(1)).toLowerCase(); Ability ability = abilities.get(abilityToken); tokens = Arrays.copyOfRange(tokens, 1, tokens.length); return Trio.of(update, ability, tokens); } else { Ability ability = abilities.get(DEFAULT); return Trio.of(update, ability, tokens); } }
@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"); } }
/** * This method contains the stream of actions that are applied on any update. * <p> * It will correctly handle addition of users into the DB and the execution of abilities and replies. * * @param update the update received by Telegram's API */ public void onUpdateReceived(Update update) { BotLogger.info(format("New update [%s] received at %s", update.getUpdateId(), now()), format("%s - %s", TAG, botUsername)); BotLogger.info(update.toString(), TAG); long millisStarted = System.currentTimeMillis(); Stream.of(update) .filter(this::checkGlobalFlags) .filter(this::checkBlacklist) .map(this::addUser) .filter(this::filterReply) .map(this::getAbility) .filter(this::validateAbility) .filter(this::checkPrivacy) .filter(this::checkLocality) .filter(this::checkInput) .filter(this::checkMessageFlags) .map(this::getContext) .map(this::consumeUpdate) .forEach(this::postConsumption); long processingTime = System.currentTimeMillis() - millisStarted; BotLogger.info(format("Processing of update [%s] ended at %s%n---> Processing time: [%d ms] <---%n", update.getUpdateId(), now(), processingTime), format("%s - %s", TAG, botUsername)); }
/** * 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; } }
@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); } }
/** * @param msg the message to be replied to * @return a predicate that asserts that the update is a reply to the specified message. */ public static Predicate<Update> isReplyTo(String msg) { return update -> update.getMessage().getReplyToMessage().getText().equals(msg); }
lock.wait(500); } else { updates.removeIf(x -> x.getUpdateId() < lastReceivedUpdate); lastReceivedUpdate = updates.parallelStream() .map(
/** * 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; } }
private Optional<Message> send(String message, Update upd) { Long chatId = upd.getMessage().getChatId(); return silent.send(getLocalizedMessage(message, AbilityUtils.getUser(upd).getLanguageCode()), chatId); }
lock.wait(500); } else { updates.removeIf(x -> x.getUpdateId() < lastReceivedUpdate); lastReceivedUpdate = updates.parallelStream() .map(
@Override public final void onUpdateReceived(Update update) { if (update.hasMessage()) { Message message = update.getMessage(); if (message.isCommand() && !filter(message)) { if (!commandRegistry.executeCommand(this, message)) { //we have received a not registered command, handle it as invalid processInvalidCommandUpdate(update); } return; } } processNonCommandUpdate(update); }
if(update.hasMessage()){ Message message = update.getMessage(); else if(update.hasCallbackQuery()){ CallbackQuery callbackquery = update.getCallbackQuery(); String[] data = callbackquery.getData().split(":"); int index = Integer.parseInt(data[2]);
/** * 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"); } }
/** * @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)"); } }
getLocalizedMessage(ABILITY_RECOVER_MESSAGE, ctx.user().getLanguageCode()), ctx.chatId())) .reply(update -> { String replyToMsg = update.getMessage().getReplyToMessage().getText(); String recoverMessage = getLocalizedMessage(ABILITY_RECOVER_MESSAGE, AbilityUtils.getUser(update).getLanguageCode()); if (!replyToMsg.equals(recoverMessage)) return; String fileId = update.getMessage().getDocument().getFileId(); try (FileReader reader = new FileReader(downloadFileWithId(fileId))) { String backupData = IOUtils.toString(reader);
@Override public BotApiMethod onWebhookUpdateReceived(Update update) { if (update.hasMessage() && update.getMessage().hasText()) { SendMessage sendMessage = new SendMessage(); sendMessage.setChatId(update.getMessage().getChatId().toString()); sendMessage.setText("Well, all information looks like noise until you break the code."); return sendMessage; } return null; }
@Override public void onUpdateReceived(Update update) { try { Message message = update.getMessage(); if (message != null && message.hasText()) { try { handleIncomingMessage(message); } catch (InvalidObjectException e) { BotLogger.severe(LOGTAG, e); } } } catch (Exception e) { BotLogger.error(LOGTAG, e); } }
private void handleUpdate(Update update) throws InvalidObjectException, TelegramApiException { if (update.hasMessage() && update.getMessage().hasText()) { Message message = update.getMessage(); if (BuildVars.ADMINS.contains(message.getFrom().getId())) { sendTransifexFile(message); } else { sendMovedToMessage(message); } } }
private void handleFileUpdate(Update update) throws InvalidObjectException, TelegramApiException { Message message = update.getMessage(); if (message != null && message.hasText()) { if (languageMessages.contains(message.getFrom().getId())) { onLanguageReceived(message); } else { String language = DatabaseManager.getInstance().getUserLanguage(update.getMessage().getFrom().getId()); if (message.getText().startsWith(Commands.setLanguageCommand)) { onSetLanguageCommand(message, language); String language = DatabaseManager.getInstance().getUserLanguage(update.getMessage().getFrom().getId()); DatabaseManager.getInstance().addFile(message.getDocument().getFileId(), message.getFrom().getId(), message.getDocument().getFileName()); SendMessage sendMessageRequest = new SendMessage();