public Optional<Session> getSession(Message message){ try { return Optional.of(sessionManager.getSession(chatIdConverter)); } catch (UnknownSessionException e) { SessionContext botSession = new DefaultChatSessionContext(message.getChatId(), message.getFrom().getUserName()); return Optional.of(sessionManager.start(botSession)); } }
/** * 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; }
/** * Process the message and execute the command * * @param absSender absSender to send messages over * @param message the message to process * @param arguments passed arguments */ @Override public void processMessage(AbsSender absSender, Message message, String[] arguments) { execute(absSender, message.getFrom(), message.getChat(), message.getMessageId(), arguments); }
/** * Process the message and execute the command * * @param absSender absSender to send messages over * @param message the message to process * @param arguments passed arguments */ public void processMessage(AbsSender absSender, Message message, String[] arguments) { execute(absSender, message.getFrom(), message.getChat(), arguments); }
/** * @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); }
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 void handleIncomingMessage(Message message) throws InvalidObjectException { int state = userState.getOrDefault(message.getFrom().getId(), 0); switch(state) { case WAITINGCHANNEL: onWaitingChannelMessage(message); break; default: sendHelpMessage(message.getChatId(), message.getMessageId(), null); userState.put(message.getFrom().getId(), WAITINGCHANNEL); break; } }
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); } else if (message.getText().startsWith(Commands.startDirectionCommand)) { onStartdirectionsCommand(message, language); } else if ((message.getText().startsWith(Commands.help) || (message.getText().startsWith(Commands.startCommand) || !message.isGroupMessage())) && DatabaseManager.getInstance().getUserDestinationStatus(message.getFrom().getId()) == -1) { sendHelpMessage(message, language); } else if (!message.getText().startsWith("/")) { if (DatabaseManager.getInstance().getUserDestinationStatus(message.getFrom().getId()) == WATING_ORIGIN_STATUS && message.isReply() && DatabaseManager.getInstance().getUserDestinationMessageId(message.getFrom().getId()) == message.getReplyToMessage().getMessageId()) { onOriginReceived(message, language); } else if (DatabaseManager.getInstance().getUserDestinationStatus(message.getFrom().getId()) == WATING_DESTINY_STATUS && message.isReply() && DatabaseManager.getInstance().getUserDestinationMessageId(message.getFrom().getId()) == message.getReplyToMessage().getMessageId()) { onDestinationReceived(message, language); } else if (!message.isReply()) { if (DatabaseManager.getInstance().getUserDestinationStatus(message.getFrom().getId()) == -1) { sendHelpMessage(message, language); } else { SendMessage sendMessageRequest = new SendMessage();
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); } else { String[] parts = message.getText().split(" ", 2); if (parts[0].startsWith(Commands.startCommand)) { if (parts.length == 2) { sendHelpMessage(message, language); } else if (!message.isGroupMessage()) { if (parts[0].startsWith(Commands.uploadCommand)) { // Open upload for user onUploadCommand(message, language); } else if (message != null && message.hasDocument() && DatabaseManager.getInstance().getUserStatusForFile(message.getFrom().getId()) == INITIAL_UPLOAD_STATUS) { 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(); sendMessageRequest.setText(LocalisationService.getString("fileUploaded", language) + LocalisationService.getString("uploadedFileURL", language) + message.getDocument().getFileId()); sendMessageRequest.setChatId(message.getChatId()); execute(sendMessageRequest);
private static SendMessage onForecastWeatherLocationReceived(Message message, String language) { String unitsSystem = DatabaseManager.getInstance().getUserWeatherOptions(message.getFrom().getId())[1]; String weather = WeatherService.getInstance().fetchWeatherForecastByLocation(message.getLocation().getLongitude(), message.getLocation().getLatitude(), message.getFrom().getId(), language, unitsSystem); SendMessage sendMessageRequest = new SendMessage(); sendMessageRequest.enableMarkdown(true); sendMessageRequest.setReplyMarkup(getMainMenuKeyboard(language)); sendMessageRequest.setReplyToMessageId(message.getMessageId()); sendMessageRequest.setText(weather); sendMessageRequest.setChatId(message.getChatId()); DatabaseManager.getInstance().insertWeatherState(message.getFrom().getId(), message.getChatId(), MAINMENU); return sendMessageRequest; }
private void sendTransifexFile(Message message) throws InvalidObjectException { String language = DatabaseManager.getInstance().getUserLanguage(message.getFrom().getId()); String text = message.getText(); String[] parts = text.split(" ", 2); SendDocument sendDocument = null; Commands.transifexAndroidSupportCommand); sendMessageRequest.setText(helpFormated); sendMessageRequest.setChatId(message.getChatId()); try { execute(sendMessageRequest); sendDocument.setChatId(message.getChatId()); try { execute(sendDocument); (message.getText().startsWith(Commands.startCommand) || !message.isGroupMessage())) { SendMessage sendMessageRequest = new SendMessage(); String helpFormated = String.format( Commands.transifexAndroidSupportCommand); sendMessageRequest.setText(helpFormated); sendMessageRequest.setChatId(message.getChatId()); try { execute(sendMessageRequest);
/** * Constructor. */ public CommandsHandler(String botUsername) { super(botUsername); register(new HelloCommand()); register(new StartCommand()); register(new StopCommand()); HelpCommand helpCommand = new HelpCommand(this); register(helpCommand); registerDefaultAction((absSender, message) -> { SendMessage commandUnknownMessage = new SendMessage(); commandUnknownMessage.setChatId(message.getChatId()); commandUnknownMessage.setText("The command '" + message.getText() + "' is not known by this bot. Here comes some help " + Emoji.AMBULANCE); try { absSender.execute(commandUnknownMessage); } catch (TelegramApiException e) { BotLogger.error(LOGTAG, e); } helpCommand.execute(absSender, message.getFrom(), message.getChat(), new String[] {}); }); }
private static SendMessage getMessageToChannelSent(Message message) { SendMessage sendMessage = new SendMessage(); sendMessage.enableMarkdown(true); sendMessage.setChatId(message.getChatId()); sendMessage.setReplyToMessageId(message.getMessageId()); sendMessage.setText(AFTER_CHANNEL_TEXT); return sendMessage; }
@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()); } }
/** * 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"); } }
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); } }
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 Optional<Message> send(String message, Update upd) { Long chatId = upd.getMessage().getChatId(); return silent.send(getLocalizedMessage(message, AbilityUtils.getUser(upd).getLanguageCode()), chatId); }
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; }