Skip to content

Commit

Permalink
MarkdownV2.java
Browse files Browse the repository at this point in the history
  • Loading branch information
yvasyliev committed Oct 16, 2023
1 parent 0d4d5f4 commit 943ae06
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ public Optional<String> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("-", "\\-");
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/github/yvasyliev/utils/MarkdownV2.java
Original file line number Diff line number Diff line change
@@ -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("!", "\\!");
}
}

0 comments on commit 943ae06

Please sign in to comment.