From ac7014a1746417698694d7a94c28623725e45a21 Mon Sep 17 00:00:00 2001 From: Yevhen Vasyliev Date: Wed, 8 Nov 2023 17:27:49 +0200 Subject: [PATCH] optimized RedTelBot#getCommand --- .../yvasyliev/bots/telegram/RedTelBot.java | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/github/yvasyliev/bots/telegram/RedTelBot.java b/src/main/java/com/github/yvasyliev/bots/telegram/RedTelBot.java index d9d5b7b..7c4db98 100644 --- a/src/main/java/com/github/yvasyliev/bots/telegram/RedTelBot.java +++ b/src/main/java/com/github/yvasyliev/bots/telegram/RedTelBot.java @@ -162,24 +162,15 @@ public CompletableFuture executeDelayed(SendPhoto sendPhoto) { return delayedBlockingExecutor.submit(() -> execute(sendPhoto)); } - // TODO: 11/8/2023 message.isCommand private Optional getCommand(Message message) { - long userId = message.getFrom().getId(); - if (message.hasText()) { - var text = message.getText().trim(); - if (looksLikeCommand(text)) { - removeUserCommand(userId); - return Optional.of(text); - } + var userId = message.getFrom().getId(); + if (message.isCommand()) { + return Optional.ofNullable(removeUserCommand(userId)) + .or(() -> Optional.of(message.getText().trim())); } - return Optional.ofNullable(userCommands.get(userId)); } - private boolean looksLikeCommand(String text) { - return text.matches("/\\w+"); - } - public String getChatId() { return chatId; }