mirror of
https://github.com/tiennm99/store-scraper-bot-java.git
synced 2026-05-21 10:26:10 +00:00
Refactor bot client and add group management commands
Replaced ScoreScrapeBotTelegramClient with StoreScrapeBotTelegramClient and updated related references. Added BaseStoreScraperBotCommand for command error handling. Implemented AddGroupCommand and GetGroupCommand for managing allowed groups via admin commands. Improved CouchbaseUtil to ensure scope and collection creation. Updated logging configuration and environment example. Minor model and repository adjustments for initialization and group handling.
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
package com.miti99.storescraperbot.bot;
|
||||
|
||||
import com.miti99.storescraperbot.config.Config;
|
||||
import org.telegram.telegrambots.client.okhttp.OkHttpTelegramClient;
|
||||
|
||||
public class ScoreScrapeBotTelegramClient extends OkHttpTelegramClient {
|
||||
public static final ScoreScrapeBotTelegramClient INSTANCE = new ScoreScrapeBotTelegramClient();
|
||||
|
||||
public ScoreScrapeBotTelegramClient() {
|
||||
super(Config.TELEGRAM_BOT_TOKEN);
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,10 @@ package com.miti99.storescraperbot.bot;
|
||||
import com.miti99.storescraperbot.bot.command.AddGroupCommand;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.telegram.telegrambots.extensions.bots.commandbot.CommandLongPollingTelegramBot;
|
||||
import org.telegram.telegrambots.meta.api.methods.commands.SetMyCommands;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
import org.telegram.telegrambots.meta.api.objects.commands.BotCommand;
|
||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||
|
||||
@Log4j2
|
||||
@@ -12,8 +14,22 @@ public class StoreScrapeBot extends CommandLongPollingTelegramBot {
|
||||
public static final StoreScrapeBot INSTANCE = new StoreScrapeBot();
|
||||
|
||||
StoreScrapeBot() {
|
||||
super(ScoreScrapeBotTelegramClient.INSTANCE, true, ScoreScrapeBotUsernameSupplier.INSTANCE);
|
||||
super(StoreScrapeBotTelegramClient.INSTANCE, true, StoreScrapeBotUsernameSupplier.INSTANCE);
|
||||
register(AddGroupCommand.INSTANCE);
|
||||
setMyCommands();
|
||||
}
|
||||
|
||||
private void setMyCommands() {
|
||||
try {
|
||||
var commands =
|
||||
getRegisteredCommands().stream()
|
||||
.map(cmd -> new BotCommand(cmd.getCommandIdentifier(), cmd.getDescription()))
|
||||
.toList();
|
||||
var setMyCommands = SetMyCommands.builder().commands(commands).build();
|
||||
StoreScrapeBotTelegramClient.INSTANCE.execute(setMyCommands);
|
||||
} catch (TelegramApiException e) {
|
||||
log.error("register error", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.miti99.storescraperbot.bot;
|
||||
|
||||
import com.miti99.storescraperbot.config.Config;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.telegram.telegrambots.client.okhttp.OkHttpTelegramClient;
|
||||
import org.telegram.telegrambots.meta.api.methods.ParseMode;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
|
||||
@Log4j2
|
||||
public class StoreScrapeBotTelegramClient extends OkHttpTelegramClient {
|
||||
public static final StoreScrapeBotTelegramClient INSTANCE = new StoreScrapeBotTelegramClient();
|
||||
|
||||
public StoreScrapeBotTelegramClient() {
|
||||
super(Config.TELEGRAM_BOT_TOKEN);
|
||||
}
|
||||
|
||||
public void sendMessage(long chatId, String text) {
|
||||
try {
|
||||
var sendMessage =
|
||||
SendMessage.builder().parseMode(ParseMode.HTML).chatId(chatId).text(text).build();
|
||||
execute(sendMessage);
|
||||
} catch (Exception e) {
|
||||
log.error("sendMessage error", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendMessage(long chatId, int threadId, String text) {
|
||||
try {
|
||||
var sendMessage =
|
||||
SendMessage.builder()
|
||||
.parseMode(ParseMode.HTML)
|
||||
.chatId(chatId)
|
||||
.messageThreadId(threadId)
|
||||
.text(text)
|
||||
.build();
|
||||
execute(sendMessage);
|
||||
} catch (Exception e) {
|
||||
log.error("sendMessage error", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -3,9 +3,9 @@ package com.miti99.storescraperbot.bot;
|
||||
import com.miti99.storescraperbot.config.Config;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ScoreScrapeBotUsernameSupplier implements Supplier<String> {
|
||||
public static final ScoreScrapeBotUsernameSupplier INSTANCE =
|
||||
new ScoreScrapeBotUsernameSupplier();
|
||||
public class StoreScrapeBotUsernameSupplier implements Supplier<String> {
|
||||
public static final StoreScrapeBotUsernameSupplier INSTANCE =
|
||||
new StoreScrapeBotUsernameSupplier();
|
||||
|
||||
@Override
|
||||
public String get() {
|
||||
@@ -1,23 +1,48 @@
|
||||
package com.miti99.storescraperbot.bot.command;
|
||||
|
||||
import com.miti99.storescraperbot.bot.StoreScrapeBotTelegramClient;
|
||||
import com.miti99.storescraperbot.config.Config;
|
||||
import org.telegram.telegrambots.extensions.bots.commandbot.commands.BotCommand;
|
||||
import com.miti99.storescraperbot.repository.AdminRepository;
|
||||
import org.telegram.telegrambots.meta.api.objects.User;
|
||||
import org.telegram.telegrambots.meta.api.objects.chat.Chat;
|
||||
import org.telegram.telegrambots.meta.generics.TelegramClient;
|
||||
|
||||
public class AddGroupCommand extends BotCommand {
|
||||
public class AddGroupCommand extends BaseStoreScraperBotCommand {
|
||||
public static final AddGroupCommand INSTANCE = new AddGroupCommand();
|
||||
|
||||
AddGroupCommand() {
|
||||
super("addgroup", "Thêm group vào list group cho phép sử dụng bot");
|
||||
super("addgroup", "<groupId>. Thêm group vào list group cho phép sử dụng bot");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(TelegramClient telegramClient, User user, Chat chat, String[] arguments) {
|
||||
if (!Config.ADMIN_IDS.contains(user.getId()) ) {
|
||||
protected void executeCommand(
|
||||
TelegramClient telegramClient, User user, Chat chat, String[] arguments) {
|
||||
if (!Config.ADMIN_IDS.contains(user.getId())) {
|
||||
StoreScrapeBotTelegramClient.INSTANCE.sendMessage(chat.getId(), "You are not admin");
|
||||
return;
|
||||
}
|
||||
// TODO
|
||||
|
||||
if (arguments.length != 1) {
|
||||
StoreScrapeBotTelegramClient.INSTANCE.sendMessage(chat.getId(), "Invalid arguments");
|
||||
return;
|
||||
}
|
||||
|
||||
long groupId;
|
||||
try {
|
||||
groupId = Long.parseLong(arguments[0]);
|
||||
} catch (NumberFormatException e) {
|
||||
StoreScrapeBotTelegramClient.INSTANCE.sendMessage(chat.getId(), "Invalid groupId");
|
||||
return;
|
||||
}
|
||||
|
||||
var admin = AdminRepository.INSTANCE.load();
|
||||
if (admin.getGroups().contains(groupId)) {
|
||||
StoreScrapeBotTelegramClient.INSTANCE.sendMessage(chat.getId(), "Group is already added");
|
||||
return;
|
||||
}
|
||||
|
||||
admin.getGroups().add(groupId);
|
||||
AdminRepository.INSTANCE.save(admin);
|
||||
StoreScrapeBotTelegramClient.INSTANCE.sendMessage(chat.getId(), "Group added successfully");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.miti99.storescraperbot.bot.command;
|
||||
|
||||
import com.miti99.storescraperbot.bot.StoreScrapeBotTelegramClient;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.telegram.telegrambots.extensions.bots.commandbot.commands.BotCommand;
|
||||
import org.telegram.telegrambots.meta.api.objects.User;
|
||||
import org.telegram.telegrambots.meta.api.objects.chat.Chat;
|
||||
import org.telegram.telegrambots.meta.generics.TelegramClient;
|
||||
|
||||
@Log4j2
|
||||
public abstract class BaseStoreScraperBotCommand extends BotCommand {
|
||||
|
||||
public BaseStoreScraperBotCommand(String commandIdentifier, String description) {
|
||||
super(commandIdentifier, description);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(TelegramClient telegramClient, User user, Chat chat, String[] arguments) {
|
||||
try {
|
||||
executeCommand(telegramClient, user, chat, arguments);
|
||||
} catch (Exception e) {
|
||||
log.error("execute error", e);
|
||||
StoreScrapeBotTelegramClient.INSTANCE.sendMessage(chat.getId(), "Internal server error");
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void executeCommand(
|
||||
TelegramClient telegramClient, User user, Chat chat, String[] arguments);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.miti99.storescraperbot.bot.command;
|
||||
|
||||
import com.miti99.storescraperbot.bot.StoreScrapeBotTelegramClient;
|
||||
import com.miti99.storescraperbot.config.Config;
|
||||
import com.miti99.storescraperbot.repository.AdminRepository;
|
||||
import org.telegram.telegrambots.meta.api.objects.User;
|
||||
import org.telegram.telegrambots.meta.api.objects.chat.Chat;
|
||||
import org.telegram.telegrambots.meta.generics.TelegramClient;
|
||||
|
||||
public class GetGroupCommand extends BaseStoreScraperBotCommand {
|
||||
public static final GetGroupCommand INSTANCE = new GetGroupCommand();
|
||||
|
||||
GetGroupCommand() {
|
||||
super("getgroup", "Lấy danh sách group được phép sử dụng bot hiện tại");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void executeCommand(
|
||||
TelegramClient telegramClient, User user, Chat chat, String[] arguments) {
|
||||
if (!Config.ADMIN_IDS.contains(user.getId())) {
|
||||
StoreScrapeBotTelegramClient.INSTANCE.sendMessage(chat.getId(), "You are not admin");
|
||||
return;
|
||||
}
|
||||
|
||||
if (arguments.length != 0) {
|
||||
StoreScrapeBotTelegramClient.INSTANCE.sendMessage(chat.getId(), "Invalid arguments");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var admin = AdminRepository.INSTANCE.load();
|
||||
var groups = admin.getGroups();
|
||||
var sb = new StringBuilder();
|
||||
sb.append("<b>Groups:</b>/n");
|
||||
for (var groupId : groups) {
|
||||
sb.append("- ").append(groupId).append("\n");
|
||||
}
|
||||
StoreScrapeBotTelegramClient.INSTANCE.sendMessage(chat.getId(), sb.toString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user