Skip to content

Commit

Permalink
created AdminCommand.java
Browse files Browse the repository at this point in the history
  • Loading branch information
yvasyliev committed Oct 20, 2023
1 parent 4ccbfc8 commit 065026c
Show file tree
Hide file tree
Showing 20 changed files with 88 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
import java.net.URISyntaxException;

@Service("/addblockedauthor")
public class AddBlockedAuthor extends Command {
public class AddBlockedAuthor extends AdminCommand {
@Autowired
private UsernameParser usernameParser;

@Autowired
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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ public abstract class Command implements ThrowingConsumer<Message> {
@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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/resources/responses/admin/start.md
Original file line number Diff line number Diff line change
@@ -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
10 changes: 7 additions & 3 deletions src/main/resources/responses/start.md
Original file line number Diff line number Diff line change
@@ -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.
Use these command to control me:

/contactadmin - send message to admin
/help - show available commands
/suggestpost - suggest your post to channel

0 comments on commit 065026c

Please sign in to comment.