/** * @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); }
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); } }
private Optional<Message> send(String message, Update upd) { Long chatId = upd.getMessage().getChatId(); return silent.send(getLocalizedMessage(message, AbilityUtils.getUser(upd).getLanguageCode()), chatId); }
@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); }
@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); }
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);
/** * @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)"); } }
/** * 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"); } }
/** * 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"); } }
@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; }
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); } } }
@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); } }
@Override public void onUpdateReceived(Update update) { try { if (update.hasMessage()) { Message message = update.getMessage(); if (message.hasText() || message.hasLocation()) { handleIncomingMessage(message); } } } catch (Exception e) { BotLogger.error(LOGTAG, e); } }
@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); } }
@Override public void onUpdateReceived(Update update) { try { if (update.hasMessage()) { try { handleFileUpdate(update); } catch (TelegramApiRequestException e) { if (e.getApiResponse().contains("Bot was blocked by the user")) { if (update.getMessage().getFrom() != null) { DatabaseManager.getInstance().deleteUserForFile(update.getMessage().getFrom().getId()); } } } catch (Exception e) { BotLogger.severe(LOGTAG, e); } } } catch (Exception e) { BotLogger.error(LOGTAG, e); } }
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();
@Override public void processNonCommandUpdate(Update update) { if (update.hasMessage()) { Message message = update.getMessage(); if (!DatabaseManager.getInstance().getUserStateForCommandsBot(message.getFrom().getId())) { return; } if (message.hasText()) { SendMessage echoMessage = new SendMessage(); echoMessage.setChatId(message.getChatId()); echoMessage.setText("Hey heres your message:\n" + message.getText()); try { execute(echoMessage); } catch (TelegramApiException e) { BotLogger.error(LOGTAG, e); } } } }
private void handleDirections(Update update) throws InvalidObjectException { Message message = update.getMessage(); if (message != null && message.hasText()) { if (languageMessages.contains(message.getFrom().getId())) { onLanguageSelected(message); } else { String language = DatabaseManager.getInstance().getUserLanguage(update.getMessage().getFrom().getId()); if (message.getText().startsWith(Commands.setLanguageCommand)) { onSetLanguageCommand(message, language);