/** * @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); }
/** * Executes a command action if the command is registered. * * @note If the command is not registered and there is a default consumer, * that action will be performed * * @param absSender absSender * @param message input message * @return True if a command or default action is executed, false otherwise */ public final boolean executeCommand(AbsSender absSender, Message message) { if (message.hasText()) { String text = message.getText(); if (text.startsWith(BotCommand.COMMAND_INIT_CHARACTER)) { String commandMessage = text.substring(1); String[] commandSplit = commandMessage.split(BotCommand.COMMAND_PARAMETER_SEPARATOR_REGEXP); String command = removeUsernameFromCommandIfNeeded(commandSplit[0]); if (commandRegistryMap.containsKey(command)) { String[] parameters = Arrays.copyOfRange(commandSplit, 1, commandSplit.length); commandRegistryMap.get(command).processMessage(absSender, message, parameters); return true; } else if (defaultConsumer != null) { defaultConsumer.accept(absSender, message); return true; } } } return false; }
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); } }
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))
private void onWaitingChannelMessage(Message message) throws InvalidObjectException { try { if (message.getText().equals(CANCEL_COMMAND)) { userState.remove(message.getFrom().getId()); sendHelpMessage(message.getChatId(), message.getMessageId(), null); } else { if (message.getText().startsWith("@") && !message.getText().trim().contains(" ")) { execute(getMessageToChannelSent(message)); sendMessageToChannel(message.getText(), message); userState.remove(message.getFrom().getId()); } else { execute(getWrongUsernameMessage(message)); } } } catch (TelegramApiException e) { BotLogger.error(LOGTAG, e); } }
private static SendMessage messageOnSetting(Message message, String language) { SendMessage sendMessageRequest = null; if (message.hasText()) { if (message.getText().startsWith(getLanguagesCommand(language))) { sendMessageRequest = onLanguageCommand(message, language); } else if (message.getText().startsWith(getUnitsCommand(language))) { sendMessageRequest = onUnitsCommand(message, language); } else if (message.getText().startsWith(getAlertsCommand(language))) { sendMessageRequest = onAlertsCommand(message, language); } else if (message.getText().startsWith(getBackCommand(language))) { sendMessageRequest = sendMessageDefault(message, language); } else { sendMessageRequest = sendChooseOptionMessage(message.getChatId(), message.getMessageId(), getSettingsKeyboard(language), language); } } return sendMessageRequest; }
private static SendMessage onAlertOptionSelected(Message message, String language) { SendMessage sendMessageRequest = null; if (message.hasText()) { if (message.getText().equals(getNewCommand(language))) { sendMessageRequest = onNewAlertCommand(message, language); } else if (message.getText().equals(getDeleteCommand(language))) { sendMessageRequest = onDeleteAlertCommand(message, language); } else if (message.getText().equals(getListCommand(language))) { sendMessageRequest = onListAlertCommand(message, language); } else if (message.getText().equals(getBackCommand(language))) { sendMessageRequest = onBackAlertCommand(message, language); } else { sendMessageRequest = sendChooseOptionMessage(message.getChatId(), message.getMessageId(), getAlertsKeyboard(language), language); } } return sendMessageRequest; }
private static SendMessage messageOnLanguage(Message message, String language) { SendMessage sendMessageRequest = null; if (message.hasText()) { if (message.getText().trim().equals(getCancelCommand(language))) { sendMessageRequest = onBackLanguageCommand(message, language); } else if (LocalisationService.getLanguageByName(message.getText().trim()) != null) { sendMessageRequest = onLanguageChosen(message.getFrom().getId(), message.getChatId(), message.getMessageId(), message.getText().trim()); } else { sendMessageRequest = onLanguageError(message.getChatId(), message.getMessageId(), language); } } return sendMessageRequest; }
private static SendMessage messageOnUnits(Message message, String language) { SendMessage sendMessageRequest = null; if (message.hasText()) { if (message.getText().trim().equals(getCancelCommand(language))) { sendMessageRequest = onBackUnitsCommand(message, language); } else if (message.getText().trim().equals(LocalisationService.getString("metricSystem", language))) { sendMessageRequest = onUnitsChosen(message.getFrom().getId(), message.getChatId(), message.getMessageId(), WeatherService.METRICSYSTEM, language); } else if (message.getText().trim().equals(LocalisationService.getString("imperialSystem", language))) { sendMessageRequest = onUnitsChosen(message.getFrom().getId(), message.getChatId(), message.getMessageId(), WeatherService.IMPERIALSYSTEM, language); } else { sendMessageRequest = onUnitsError(message.getChatId(), message.getMessageId(), language); } } return sendMessageRequest; }
private void sendErrorMessage(Message message, String errorText) { SendMessage sendMessage = new SendMessage(); sendMessage.enableMarkdown(true); sendMessage.setChatId(message.getChatId()); sendMessage.setReplyToMessageId(message.getMessageId()); sendMessage.setText(String.format(ERROR_MESSAGE_TEXT, message.getText().trim(), errorText.replace("\"", "\\\""))); sendMessage.enableMarkdown(true); try { execute(sendMessage); } catch (TelegramApiException e) { BotLogger.error(LOGTAG, e); } }
@Override public void onResult(BotApiMethod<Message> method, Message sentMessage) { if (sentMessage != null) { DatabaseManager.getInstance().addUserForDirection(message.getFrom().getId(), WATING_DESTINY_STATUS, sentMessage.getMessageId(), message.getText()); } }
private static SendMessage onAlertDeleteOptionSelected(Message message, String language) { SendMessage sendMessageRequest = null; if (message.hasText()) { if (message.getText().equals(getCancelCommand(language))) { sendMessageRequest = onAlertDeleteBackOptionSelected(message, language); } else if (DatabaseManager.getInstance().getAlertCitiesNameByUser(message.getFrom().getId()).contains(message.getText())) { sendMessageRequest = onAlertDeleteCityOptionSelected(message, language); } else { sendMessageRequest = sendChooseOptionMessage(message.getChatId(), message.getMessageId(), getAlertsListKeyboard(message.getFrom().getId(), language), language); } } return sendMessageRequest; }
private static SendMessage onForecastWeather(Message message, String language) { SendMessage sendMessageRequest = null; if (message.hasText()) { if (message.getText().startsWith(getNewCommand(language))) { sendMessageRequest = onNewForecastWeatherCommand(message.getChatId(), message.getFrom().getId(), message.getMessageId(), language); } else if (message.getText().startsWith(getLocationCommand(language))) { sendMessageRequest = onLocationForecastWeatherCommand(message.getChatId(), message.getFrom().getId(), message.getMessageId(), language); } else if (message.getText().startsWith(getCancelCommand(language))) { sendMessageRequest = onCancelCommand(message.getChatId(), message.getFrom().getId(), message.getMessageId(), getMainMenuKeyboard(language), language); } else { sendMessageRequest = onForecastWeatherCityReceived(message.getChatId(), message.getFrom().getId(), message.getMessageId(), message.getText(), language); } } return sendMessageRequest; }
private static SendMessage onCurrentWeather(Message message, String language) { SendMessage sendMessageRequest = null; if (message.hasText()) { if (message.getText().startsWith(getNewCommand(language))) { sendMessageRequest = onNewCurrentWeatherCommand(message.getChatId(), message.getFrom().getId(), message.getMessageId(), language); } else if (message.getText().startsWith(getLocationCommand(language))) { sendMessageRequest = onLocationCurrentWeatherCommand(message.getChatId(), message.getFrom().getId(), message.getMessageId(), language); } else if (message.getText().startsWith(getCancelCommand(language))) { sendMessageRequest = onCancelCommand(message.getChatId(), message.getFrom().getId(), message.getMessageId(), getMainMenuKeyboard(language), language); } else { sendMessageRequest = onCurrentWeatherCityReceived(message.getChatId(), message.getFrom().getId(), message.getMessageId(), message.getText(), language); } } return sendMessageRequest; }
private static SendMessage onForecastNewWeather(Message message, String language) { if (message.isReply()) { return onForecastWeatherReceived(message.getChatId(), message.getFrom().getId(), message.getMessageId(), message.getText(), language); } else { return sendMessageDefault(message, language); } }
private static SendMessage onCurrentNewWeather(Message message, String language) { if (message.isReply()) { return onCurrentWeatherReceived(message.getChatId(), message.getFrom().getId(), message.getMessageId(), message.getText(), language); } else { return sendMessageDefault(message, language); } }
private void onLanguageReceived(Message message) throws InvalidObjectException, TelegramApiException { String[] parts = message.getText().split(Emoji.LEFT_RIGHT_ARROW.toString(), 2); SendMessage sendMessageRequest = new SendMessage(); sendMessageRequest.setChatId(message.getChatId()); if (LocalisationService.getLanguageByCode(parts[0].trim()) != null) { DatabaseManager.getInstance().putUserLanguage(message.getFrom().getId(), parts[0].trim()); sendMessageRequest.setText(LocalisationService.getString("languageModified", parts[0].trim())); } else { sendMessageRequest.setText(LocalisationService.getString("errorLanguage")); } sendMessageRequest.setReplyToMessageId(message.getMessageId()); ReplyKeyboardRemove replyKeyboardRemove = new ReplyKeyboardRemove(); replyKeyboardRemove.setSelective(true); sendMessageRequest.setReplyMarkup(replyKeyboardRemove); execute(sendMessageRequest); languageMessages.remove(message.getFrom().getId()); } }
private static SendMessage onNewAlertCityReceived(Message message, String language) { int userId = message.getFrom().getId(); Integer cityId = DatabaseManager.getInstance().getRecentWeatherIdByCity(userId, message.getText()); if (cityId != null) { DatabaseManager.getInstance().createNewWeatherAlert(userId, cityId, message.getText()); SendMessage sendMessageRequest = new SendMessage(); sendMessageRequest.enableMarkdown(true); sendMessageRequest.setReplyMarkup(getAlertsKeyboard(language)); sendMessageRequest.setReplyToMessageId(message.getMessageId()); sendMessageRequest.setText(getChooseNewAlertSetMessage(message.getText(), language)); sendMessageRequest.setChatId(message.getChatId()); DatabaseManager.getInstance().insertWeatherState(userId, message.getChatId(), ALERT); return sendMessageRequest; } else { return sendChooseOptionMessage(message.getChatId(), message.getMessageId(), getRecentsKeyboard(message.getFrom().getId(), language, false), language); } }
private static SendMessage onAlertNewOptionSelected(Message message, String language) { SendMessage sendMessageRequest = null; if (message.hasText()) { if (message.getText().equals(getCancelCommand(language))) { SendMessage sendMessage = new SendMessage(); sendMessage.enableMarkdown(true); sendMessage.setChatId(message.getChatId()); sendMessage.setReplyToMessageId(message.getMessageId()); sendMessage.setReplyMarkup(getAlertsKeyboard(language)); sendMessage.setText(LocalisationService.getString("alertsMenuMessage", language)); DatabaseManager.getInstance().insertWeatherState(message.getFrom().getId(), message.getChatId(), ALERT); sendMessageRequest = sendMessage; } else { sendMessageRequest = onNewAlertCityReceived(message, language); } } return sendMessageRequest; }
private static SendMessage onAlertDeleteCityOptionSelected(Message message, String language) { DatabaseManager.getInstance().deleteAlertCity(message.getFrom().getId(), message.getText()); SendMessage sendMessage = new SendMessage(); sendMessage.enableMarkdown(true); sendMessage.setReplyToMessageId(message.getMessageId()); sendMessage.setChatId(message.getChatId()); sendMessage.setReplyMarkup(getAlertsKeyboard(language)); sendMessage.setText(LocalisationService.getString("alertDeleted", language)); DatabaseManager.getInstance().insertWeatherState(message.getFrom().getId(), message.getChatId(), ALERT); return sendMessage; }