diff --git a/src/main/java/com/github/yvasyliev/service/telegram/commands/AddBlockedAuthor.java b/src/main/java/com/github/yvasyliev/service/telegram/commands/AddBlockedAuthor.java index b5627ab..f96ab44 100644 --- a/src/main/java/com/github/yvasyliev/service/telegram/commands/AddBlockedAuthor.java +++ b/src/main/java/com/github/yvasyliev/service/telegram/commands/AddBlockedAuthor.java @@ -2,6 +2,7 @@ import com.github.yvasyliev.service.state.StateManager; import com.github.yvasyliev.service.telegram.factory.UsernameParser; +import com.github.yvasyliev.utils.MarkdownV2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.telegram.telegrambots.meta.api.objects.Message; @@ -24,6 +25,7 @@ public void acceptWithException(Message message) throws IOException, TelegramApi if (optionalUsername.isPresent()) { var username = optionalUsername.get(); stateManager.addBlockedAuthor(username); + username = MarkdownV2.escaped(username); reply(message, "responses/addblockedauthor.md", username, username); } else { reply(message, "responses/usernamenotrecognized.md"); diff --git a/src/main/java/com/github/yvasyliev/service/telegram/commands/RemoveBlockedAuthor.java b/src/main/java/com/github/yvasyliev/service/telegram/commands/RemoveBlockedAuthor.java index 8159ee8..2f6747e 100644 --- a/src/main/java/com/github/yvasyliev/service/telegram/commands/RemoveBlockedAuthor.java +++ b/src/main/java/com/github/yvasyliev/service/telegram/commands/RemoveBlockedAuthor.java @@ -2,6 +2,7 @@ import com.github.yvasyliev.service.state.StateManager; import com.github.yvasyliev.service.telegram.factory.UsernameParser; +import com.github.yvasyliev.utils.MarkdownV2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.telegram.telegrambots.meta.api.objects.Message; @@ -21,7 +22,7 @@ public void acceptWithException(Message message) throws Exception { if (optionalUsername.isPresent()) { var username = optionalUsername.get(); stateManager.removeBlockedAuthor(username); - reply(message, "responses/removeblockedauthor.md", username); + reply(message, "responses/removeblockedauthor.md", MarkdownV2.escaped(username)); } else { reply(message, "responses/usernamenotrecognized.md"); } diff --git a/src/main/java/com/github/yvasyliev/service/telegram/commands/ReplySent.java b/src/main/java/com/github/yvasyliev/service/telegram/commands/ReplySent.java index e4b5613..49e91aa 100644 --- a/src/main/java/com/github/yvasyliev/service/telegram/commands/ReplySent.java +++ b/src/main/java/com/github/yvasyliev/service/telegram/commands/ReplySent.java @@ -1,5 +1,6 @@ package com.github.yvasyliev.service.telegram.commands; +import com.github.yvasyliev.utils.MarkdownV2; import org.springframework.stereotype.Service; import org.telegram.telegrambots.meta.api.methods.ParseMode; import org.telegram.telegrambots.meta.api.methods.send.SendMessage; @@ -20,7 +21,7 @@ public void acceptWithException(Message message) throws URISyntaxException, IOEx var replyMessage = SendMessage.builder() .chatId(awaitingReply.fromChatId()) .replyToMessageId(awaitingReply.messageId()) - .text(responseReader.applyWithException("responses/replysent/reply_template.md").formatted(message.getText())) + .text(responseReader.applyWithException("responses/replysent/reply_template.md").formatted(MarkdownV2.escaped(message.getText()))) .parseMode(ParseMode.MARKDOWNV2) .build(); redTelBot.execute(replyMessage); diff --git a/src/main/java/com/github/yvasyliev/service/telegram/factory/UsernameParser.java b/src/main/java/com/github/yvasyliev/service/telegram/factory/UsernameParser.java index 98610a7..f97c85e 100644 --- a/src/main/java/com/github/yvasyliev/service/telegram/factory/UsernameParser.java +++ b/src/main/java/com/github/yvasyliev/service/telegram/factory/UsernameParser.java @@ -18,20 +18,20 @@ public Optional apply(Message message) { var username = message.getText().trim(); var matcher = Pattern - .compile("https://www\\.reddit\\.com/user/(\\w+)") + .compile("https://www\\.reddit\\.com/user/([\\w-]+)") .matcher(username); if (matcher.find()) { username = matcher.group(1); } matcher = Pattern - .compile("u/(\\w+)") + .compile("u/([\\w-]+)") .matcher(username); if (matcher.matches()) { username = matcher.group(1); } - return username.matches("\\w+") + return username.matches("[\\w-]+") ? Optional.of(username) : Optional.empty(); } diff --git a/src/main/java/com/github/yvasyliev/service/telegram/readers/BotResponseReader.java b/src/main/java/com/github/yvasyliev/service/telegram/readers/BotResponseReader.java index 9129943..a73a5f3 100644 --- a/src/main/java/com/github/yvasyliev/service/telegram/readers/BotResponseReader.java +++ b/src/main/java/com/github/yvasyliev/service/telegram/readers/BotResponseReader.java @@ -18,6 +18,7 @@ public String applyWithException(String file) throws URISyntaxException, IOExcep Files.readAllLines(Paths.get(Objects.requireNonNull(BotResponseReader.class.getClassLoader().getResource(file)).toURI())) ) .replace(".", "\\.") - .replace("!", "\\!"); + .replace("!", "\\!") + .replace("-", "\\-"); } } diff --git a/src/main/java/com/github/yvasyliev/utils/MarkdownV2.java b/src/main/java/com/github/yvasyliev/utils/MarkdownV2.java new file mode 100644 index 0000000..75d9ead --- /dev/null +++ b/src/main/java/com/github/yvasyliev/utils/MarkdownV2.java @@ -0,0 +1,25 @@ +package com.github.yvasyliev.utils; + +public class MarkdownV2 { + public static String escaped(String text) { + return text + .replace("_", "\\_") + .replace("*", "\\*") + .replace("[", "\\[") + .replace("]", "\\]") + .replace("(", "\\(") + .replace(")", "\\)") + .replace("~", "\\~") + .replace("`", "\\`") + .replace(">", "\\>") + .replace("#", "\\#") + .replace("+", "\\+") + .replace("-", "\\-") + .replace("=", "\\=") + .replace("|", "\\|") + .replace("{", "\\{") + .replace("}", "\\}") + .replace(".", "\\.") + .replace("!", "\\!"); + } +}