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 e70589f..4234063 100644 --- a/src/main/java/com/github/yvasyliev/bots/telegram/RedTelBot.java +++ b/src/main/java/com/github/yvasyliev/bots/telegram/RedTelBot.java @@ -93,7 +93,7 @@ public void onCommandReceived(String command, Message message) { public void onCallbackQueryReceived(CallbackQuery callbackQuery) { var message = callbackQuery.getMessage(); - if (message.isUserMessage() && isFromAdmin(callbackQuery)) { + if (message.isUserMessage() && isAdmin(callbackQuery.getFrom())) { try { var callbackData = objectMapper.readValue(callbackQuery.getData(), CallbackData.class); context.getBean(callbackData.action(), Callback.class).acceptWithException(callbackQuery); @@ -121,10 +121,6 @@ public ExternalMessageData getAwaitingReply(Long userId) { return awaitingReplies.remove(userId); } - public boolean isFromAdmin(CallbackQuery callbackQuery) { - return isAdmin(callbackQuery.getFrom()); - } - public boolean isAdmin(User user) { return user.getId().toString().equals(getAdminId()); } 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 f96ab44..49a4cb1 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 @@ -12,7 +12,7 @@ import java.net.URISyntaxException; @Service("/addblockedauthor") -public class AddBlockedAuthor extends Command { +public class AddBlockedAuthor extends AdminCommand { @Autowired private UsernameParser usernameParser; @@ -20,7 +20,7 @@ public class AddBlockedAuthor extends Command { private StateManager stateManager; @Override - public void acceptWithException(Message message) throws IOException, TelegramApiException, URISyntaxException { + public void execute(Message message) throws IOException, TelegramApiException, URISyntaxException { var optionalUsername = usernameParser.apply(message); if (optionalUsername.isPresent()) { var username = optionalUsername.get(); diff --git a/src/main/java/com/github/yvasyliev/service/telegram/commands/AdminCommand.java b/src/main/java/com/github/yvasyliev/service/telegram/commands/AdminCommand.java new file mode 100644 index 0000000..98b5cb0 --- /dev/null +++ b/src/main/java/com/github/yvasyliev/service/telegram/commands/AdminCommand.java @@ -0,0 +1,10 @@ +package com.github.yvasyliev.service.telegram.commands; + +import org.telegram.telegrambots.meta.api.objects.Message; + +public abstract class AdminCommand extends Command { + @Override + protected boolean hasPermission(Message message) { + return redTelBot.isAdmin(message.getFrom()); + } +} diff --git a/src/main/java/com/github/yvasyliev/service/telegram/commands/BlockAuthor.java b/src/main/java/com/github/yvasyliev/service/telegram/commands/BlockAuthor.java index ce54f0f..f8f2224 100644 --- a/src/main/java/com/github/yvasyliev/service/telegram/commands/BlockAuthor.java +++ b/src/main/java/com/github/yvasyliev/service/telegram/commands/BlockAuthor.java @@ -8,9 +8,9 @@ import java.net.URISyntaxException; @Service("/blockauthor") -public class BlockAuthor extends Command { +public class BlockAuthor extends AdminCommand { @Override - public void acceptWithException(Message message) throws URISyntaxException, IOException, TelegramApiException { + public void execute(Message message) throws URISyntaxException, IOException, TelegramApiException { redTelBot.addUserCommand(message.getFrom().getId(), "/addblockedauthor"); reply(message, "responses/blockauthor.md"); } diff --git a/src/main/java/com/github/yvasyliev/service/telegram/commands/Cancel.java b/src/main/java/com/github/yvasyliev/service/telegram/commands/Cancel.java index 490d8b7..04374f8 100644 --- a/src/main/java/com/github/yvasyliev/service/telegram/commands/Cancel.java +++ b/src/main/java/com/github/yvasyliev/service/telegram/commands/Cancel.java @@ -10,7 +10,7 @@ @Service("/cancel") public class Cancel extends Command { @Override - public void acceptWithException(Message message) throws TelegramApiException, URISyntaxException, IOException { + public void execute(Message message) throws TelegramApiException, URISyntaxException, IOException { redTelBot.removeUserCommand(message.getFrom().getId()); reply(message, "responses/cancel.md"); } diff --git a/src/main/java/com/github/yvasyliev/service/telegram/commands/Command.java b/src/main/java/com/github/yvasyliev/service/telegram/commands/Command.java index 9c6b0c0..d05bd9d 100644 --- a/src/main/java/com/github/yvasyliev/service/telegram/commands/Command.java +++ b/src/main/java/com/github/yvasyliev/service/telegram/commands/Command.java @@ -19,6 +19,19 @@ public abstract class Command implements ThrowingConsumer { @Autowired protected BotResponseReader responseReader; + @Override + public void acceptWithException(Message message) throws Exception { + if (hasPermission(message)) { + execute(message); + } + } + + abstract void execute(Message message) throws Exception; + + protected boolean hasPermission(Message message) { + return true; + } + protected Message reply(Message to, String template, Object... args) throws URISyntaxException, IOException, TelegramApiException { var sendMessage = SendMessage.builder() .chatId(to.getChatId()) diff --git a/src/main/java/com/github/yvasyliev/service/telegram/commands/ContactAdmin.java b/src/main/java/com/github/yvasyliev/service/telegram/commands/ContactAdmin.java index 3d9dac8..876df7b 100644 --- a/src/main/java/com/github/yvasyliev/service/telegram/commands/ContactAdmin.java +++ b/src/main/java/com/github/yvasyliev/service/telegram/commands/ContactAdmin.java @@ -6,7 +6,7 @@ @Service("/contactadmin") public class ContactAdmin extends Command { @Override - public void acceptWithException(Message message) throws Exception { + public void execute(Message message) throws Exception { redTelBot.addUserCommand(message.getFrom().getId(), "/textadmin"); reply(message, "responses/contactadmin.md"); } diff --git a/src/main/java/com/github/yvasyliev/service/telegram/commands/GetBlocked.java b/src/main/java/com/github/yvasyliev/service/telegram/commands/GetBlocked.java index 5d62876..de82129 100644 --- a/src/main/java/com/github/yvasyliev/service/telegram/commands/GetBlocked.java +++ b/src/main/java/com/github/yvasyliev/service/telegram/commands/GetBlocked.java @@ -8,12 +8,12 @@ import java.util.stream.Collectors; @Service("/getblocked") -public class GetBlocked extends Command { +public class GetBlocked extends AdminCommand { @Autowired private StateManager stateManager; @Override - public void acceptWithException(Message message) throws Exception { + public void execute(Message message) throws Exception { var blockedAuthorTemplate = responseReader.applyWithException("responses/getblocked/blocked_author.md"); var blockedAuthors = stateManager .getBlockedAuthors() diff --git a/src/main/java/com/github/yvasyliev/service/telegram/commands/PausePublishing.java b/src/main/java/com/github/yvasyliev/service/telegram/commands/PausePublishing.java index 7e9cfda..49b2cac 100644 --- a/src/main/java/com/github/yvasyliev/service/telegram/commands/PausePublishing.java +++ b/src/main/java/com/github/yvasyliev/service/telegram/commands/PausePublishing.java @@ -11,12 +11,12 @@ import java.net.URISyntaxException; @Service("/pausepublishing") -public class PausePublishing extends Command { +public class PausePublishing extends AdminCommand { @Autowired private PostManager postManager; @Override - public void acceptWithException(Message message) throws TelegramApiException, URISyntaxException, IOException { + public void execute(Message message) throws TelegramApiException, URISyntaxException, IOException { postManager.pausePublishing(); redTelBot.execute(new SendMessage( message.getChatId().toString(), diff --git a/src/main/java/com/github/yvasyliev/service/telegram/commands/PostSuggested.java b/src/main/java/com/github/yvasyliev/service/telegram/commands/PostSuggested.java index 01e4057..37c718b 100644 --- a/src/main/java/com/github/yvasyliev/service/telegram/commands/PostSuggested.java +++ b/src/main/java/com/github/yvasyliev/service/telegram/commands/PostSuggested.java @@ -22,7 +22,7 @@ public class PostSuggested extends Command { private ObjectMapper objectMapper; @Override - public void acceptWithException(Message message) throws TelegramApiException, IOException, URISyntaxException { + public void execute(Message message) throws TelegramApiException, IOException, URISyntaxException { var sourceChatId = message.getChatId().toString(); var sourceMessageId = message.getMessageId(); 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 2f6747e..37c91fd 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 @@ -8,7 +8,7 @@ import org.telegram.telegrambots.meta.api.objects.Message; @Service("/removeblockedauthor") -public class RemoveBlockedAuthor extends Command { +public class RemoveBlockedAuthor extends AdminCommand { @Autowired private UsernameParser usernameParser; @@ -17,7 +17,7 @@ public class RemoveBlockedAuthor extends Command { private StateManager stateManager; @Override - public void acceptWithException(Message message) throws Exception { + public void execute(Message message) throws Exception { var optionalUsername = usernameParser.apply(message); if (optionalUsername.isPresent()) { var username = optionalUsername.get(); 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 49e91aa..6549ee3 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 @@ -11,9 +11,9 @@ import java.net.URISyntaxException; @Service("/replysent") -public class ReplySent extends Command { +public class ReplySent extends AdminCommand { @Override - public void acceptWithException(Message message) throws URISyntaxException, IOException, TelegramApiException { + public void execute(Message message) throws URISyntaxException, IOException, TelegramApiException { if (message.hasText()) { var userId = message.getFrom().getId(); redTelBot.removeUserCommand(userId); diff --git a/src/main/java/com/github/yvasyliev/service/telegram/commands/ResumePublishing.java b/src/main/java/com/github/yvasyliev/service/telegram/commands/ResumePublishing.java index aeaed0e..42af016 100644 --- a/src/main/java/com/github/yvasyliev/service/telegram/commands/ResumePublishing.java +++ b/src/main/java/com/github/yvasyliev/service/telegram/commands/ResumePublishing.java @@ -11,12 +11,12 @@ import java.net.URISyntaxException; @Service("/resumepublishing") -public class ResumePublishing extends Command { +public class ResumePublishing extends AdminCommand { @Autowired private PostManager postManager; @Override - public void acceptWithException(Message message) throws TelegramApiException, URISyntaxException, IOException { + public void execute(Message message) throws TelegramApiException, URISyntaxException, IOException { postManager.resumePublishing(); redTelBot.execute(new SendMessage( message.getChatId().toString(), diff --git a/src/main/java/com/github/yvasyliev/service/telegram/commands/Start.java b/src/main/java/com/github/yvasyliev/service/telegram/commands/Start.java index 6810a52..8ad09fe 100644 --- a/src/main/java/com/github/yvasyliev/service/telegram/commands/Start.java +++ b/src/main/java/com/github/yvasyliev/service/telegram/commands/Start.java @@ -1,5 +1,7 @@ package com.github.yvasyliev.service.telegram.commands; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.exceptions.TelegramApiException; @@ -8,9 +10,22 @@ import java.net.URISyntaxException; @Service("/start") +@Qualifier("/help") public class Start extends Command { + @Value("telegram.channel.name") + private String telegramChannelName; + @Override - public void acceptWithException(Message message) throws TelegramApiException, URISyntaxException, IOException { - reply(message, "responses/start.md"); + public void execute(Message message) throws TelegramApiException, URISyntaxException, IOException { + if (redTelBot.isAdmin(message.getFrom())) { + reply( + message, + "responses/admin/start.md", + telegramChannelName, + redTelBot.getMe().getFirstName() + ); + } else { + reply(message, "responses/start.md", telegramChannelName); + } } } diff --git a/src/main/java/com/github/yvasyliev/service/telegram/commands/Stop.java b/src/main/java/com/github/yvasyliev/service/telegram/commands/Stop.java index af66e38..8432a8f 100644 --- a/src/main/java/com/github/yvasyliev/service/telegram/commands/Stop.java +++ b/src/main/java/com/github/yvasyliev/service/telegram/commands/Stop.java @@ -10,12 +10,12 @@ import java.net.URISyntaxException; @Service("/stop") -public class Stop extends Command { +public class Stop extends AdminCommand { @Autowired private PostManager postManager; @Override - public void acceptWithException(Message message) throws TelegramApiException, URISyntaxException, IOException { + public void execute(Message message) throws TelegramApiException, URISyntaxException, IOException { postManager.stopPublishing(); redTelBot.stopPolling(); reply(message, "responses/stop.md"); diff --git a/src/main/java/com/github/yvasyliev/service/telegram/commands/SuggestPost.java b/src/main/java/com/github/yvasyliev/service/telegram/commands/SuggestPost.java index 32a6c7d..4ecc25f 100644 --- a/src/main/java/com/github/yvasyliev/service/telegram/commands/SuggestPost.java +++ b/src/main/java/com/github/yvasyliev/service/telegram/commands/SuggestPost.java @@ -10,7 +10,7 @@ @Service("/suggestpost") public class SuggestPost extends Command { @Override - public void acceptWithException(Message message) throws TelegramApiException, URISyntaxException, IOException { + public void execute(Message message) throws TelegramApiException, URISyntaxException, IOException { redTelBot.addUserCommand(message.getFrom().getId(), "/postsuggested"); reply(message, "responses/suggestpost.md"); } diff --git a/src/main/java/com/github/yvasyliev/service/telegram/commands/TextAdmin.java b/src/main/java/com/github/yvasyliev/service/telegram/commands/TextAdmin.java index 9343756..10275e0 100644 --- a/src/main/java/com/github/yvasyliev/service/telegram/commands/TextAdmin.java +++ b/src/main/java/com/github/yvasyliev/service/telegram/commands/TextAdmin.java @@ -21,7 +21,7 @@ public class TextAdmin extends Command { private ObjectMapper objectMapper; @Override - public void acceptWithException(Message message) throws TelegramApiException, IOException, URISyntaxException { + public void execute(Message message) throws TelegramApiException, IOException, URISyntaxException { redTelBot.removeUserCommand(message.getFrom().getId()); var sourceChatId = message.getChatId(); var sourceMessageId = message.getMessageId(); diff --git a/src/main/java/com/github/yvasyliev/service/telegram/commands/UnblockAuthor.java b/src/main/java/com/github/yvasyliev/service/telegram/commands/UnblockAuthor.java index 6d1b08d..f28f829 100644 --- a/src/main/java/com/github/yvasyliev/service/telegram/commands/UnblockAuthor.java +++ b/src/main/java/com/github/yvasyliev/service/telegram/commands/UnblockAuthor.java @@ -8,9 +8,9 @@ import java.net.URISyntaxException; @Service("/unblockauthor") -public class UnblockAuthor extends Command { +public class UnblockAuthor extends AdminCommand { @Override - public void acceptWithException(Message message) throws TelegramApiException, URISyntaxException, IOException { + public void execute(Message message) throws TelegramApiException, URISyntaxException, IOException { redTelBot.addUserCommand(message.getFrom().getId(), "/removeblockedauthor"); reply(message, "responses/unblockauthor.md"); } diff --git a/src/main/resources/responses/admin/start.md b/src/main/resources/responses/admin/start.md new file mode 100644 index 0000000..67fe310 --- /dev/null +++ b/src/main/resources/responses/admin/start.md @@ -0,0 +1,17 @@ +👋 Hello, I'm the manager bot of %s channel! + +🤓 I can duplicate Subreddit posts to Telegram channel. + +Use these command to control me: + +/contactadmin - send message to admin +/help - show available commands +/suggestpost - suggest your post to channel + +*Admin commands* +/blockauthor - block Reddit author +/getblocked - show blocked Reddit authors +/pausepublishing - pause fetching posts from subreddit +/resumepublishing - resume fetching posts from subreddit +/stop - stop %s +/unblockauthor - unblock Reddit author \ No newline at end of file diff --git a/src/main/resources/responses/start.md b/src/main/resources/responses/start.md index c7a5e80..35eaaa5 100644 --- a/src/main/resources/responses/start.md +++ b/src/main/resources/responses/start.md @@ -1,5 +1,9 @@ -👋 Greetings! +👋 Hello, I'm the manager bot of %s channel! -🤓 I can duplicate Subreddit posts to Telegram channel. +🤓 You can use me to suggest your posts or contact channel admin. -Send /help to see available commands. \ No newline at end of file +Use these command to control me: + +/contactadmin - send message to admin +/help - show available commands +/suggestpost - suggest your post to channel \ No newline at end of file