Skip to content

Commit

Permalink
feat: subscription check
Browse files Browse the repository at this point in the history
  • Loading branch information
almax07082005 committed Sep 12, 2024
1 parent 1d5b0b0 commit e0e0c8b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.example.cardiotelegrambot.exceptions.NotCommandException;
import lombok.AllArgsConstructor;
import org.springframework.data.util.Pair;

@AllArgsConstructor
public enum LoggerCommands {

start("/start");
start("/start"),
check("/check");

private final String name;

Expand All @@ -15,11 +17,20 @@ public String toString() {
return name;
}

public static LoggerCommands fromString(String name) throws NotCommandException {
public static Pair<LoggerCommands, Long> fromString(String name) throws NotCommandException {

for (LoggerCommands loggerCommand : LoggerCommands.values()) {
if (loggerCommand.name.equals(name)) {
return loggerCommand;
String[] commandsList = name.split(" ");
if (commandsList[0].equals("/check") && commandsList.length == 2) {
try {
return Pair.of(LoggerCommands.check, Long.parseLong(commandsList[1]));
} catch (NumberFormatException e) {
return Pair.of(LoggerCommands.check, -1L);
}
}

for (LoggerCommands command : LoggerCommands.values()) {
if (command.name.equals(name)) {
return Pair.of(command, -1L);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.example.cardiotelegrambot.config.enums.logger.LoggerCommands;
import com.example.cardiotelegrambot.exceptions.NotAdminException;
import com.example.cardiotelegrambot.exceptions.NotCommandException;
import com.example.cardiotelegrambot.exceptions.NotMemberException;
import com.example.cardiotelegrambot.service.bot.main.Button;
import com.example.cardiotelegrambot.service.database.ReferralService;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.model.Update;
Expand All @@ -14,6 +16,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.util.Pair;
import org.springframework.stereotype.Component;

import java.util.HashMap;
Expand All @@ -24,24 +27,30 @@ public class LoggerCommand {

private final TelegramBot bot;
private final ReferralService referralService;
private final Button button;

@Value("${telegram.logger.id}")
private Long adminChatId;

private final Map<LoggerCommands, Runnable> mapCommands;
private Long userChatId;

@Autowired
public LoggerCommand(@Qualifier("loggerBotBean") TelegramBot bot, ReferralService referralService) {
public LoggerCommand(@Qualifier("loggerBotBean") TelegramBot bot, ReferralService referralService, Button button) {
this.bot = bot;
this.referralService = referralService;
this.button = button;

mapCommands = new HashMap<>();
mapCommands.put(LoggerCommands.start, this::start);
mapCommands.put(LoggerCommands.check, this::check);
}

public Runnable getCommand(String command) {
try {
return mapCommands.get(LoggerCommands.fromString(command));
Pair<LoggerCommands, Long> pair = LoggerCommands.fromString(command);
userChatId = pair.getSecond();
return mapCommands.get(pair.getFirst());
} catch (NotCommandException exception) {
return this::notACommand;
}
Expand Down Expand Up @@ -78,6 +87,34 @@ public void start() {
LoggerButton.setMessageId(response.message().messageId());
}

private void check() {
SendMessage message;
try {
button
.setByVariables(userChatId)
.isSubscribed();
message = new SendMessage(
adminChatId,
String.format("""
Пользователь %s подписан на твой канал.
""", userChatId
)
);
} catch (NotMemberException | NullPointerException exception) {
message = new SendMessage(
adminChatId,
String.format("""
Пользователь %s НЕ подписан на твой канал.
""", userChatId
)
);
}
message.replyMarkup(getInlineKeyboardMarkupForMainMenu());

SendResponse response = bot.execute(message);
LoggerButton.setMessageId(response.message().messageId());
}

public InlineKeyboardMarkup getInlineKeyboardMarkupForMainMenu() {
InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();

Expand Down

0 comments on commit e0e0c8b

Please sign in to comment.