Skip to content

Commit

Permalink
optimized RedTelBot#getCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
yvasyliev committed Nov 8, 2023
1 parent 46cdc7c commit ac7014a
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions src/main/java/com/github/yvasyliev/bots/telegram/RedTelBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,24 +162,15 @@ public CompletableFuture<Message> executeDelayed(SendPhoto sendPhoto) {
return delayedBlockingExecutor.submit(() -> execute(sendPhoto));
}

// TODO: 11/8/2023 message.isCommand
private Optional<String> 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;
}
Expand Down

0 comments on commit ac7014a

Please sign in to comment.