mirror of
https://github.com/tiennm99/store-scraper-bot-java.git
synced 2026-05-23 06:25:30 +00:00
Integrate Telegram bot with command support
Replaces the previous TelegramBots dependency with the new telegrambots-client, extensions, and longpolling modules. Adds StoreScrapeBot with command handling, including a stub AddGroupCommand, and updates Main to start the bot. Config is extended to support bot credentials and admin IDs, and CouchbaseUtil is simplified to remove example code.
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
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();
|
||||
|
||||
@Override
|
||||
public String get() {
|
||||
return Config.TELEGRAM_BOT_USERNAME;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
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.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||
|
||||
@Log4j2
|
||||
public class StoreScrapeBot extends CommandLongPollingTelegramBot {
|
||||
public static final StoreScrapeBot INSTANCE = new StoreScrapeBot();
|
||||
|
||||
StoreScrapeBot() {
|
||||
super(ScoreScrapeBotTelegramClient.INSTANCE, true, ScoreScrapeBotUsernameSupplier.INSTANCE);
|
||||
register(AddGroupCommand.INSTANCE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processNonCommandUpdate(Update update) {
|
||||
try {
|
||||
var sendMessage =
|
||||
SendMessage.builder()
|
||||
.chatId(update.getMessage().getChatId())
|
||||
.text("Invalid command")
|
||||
.build();
|
||||
telegramClient.execute(sendMessage);
|
||||
} catch (TelegramApiException e) {
|
||||
log.error("processNonCommandUpdate error", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.miti99.storescraperbot.bot.command;
|
||||
|
||||
import com.miti99.storescraperbot.config.Config;
|
||||
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;
|
||||
|
||||
public class AddGroupCommand extends BotCommand {
|
||||
public static final AddGroupCommand INSTANCE = new AddGroupCommand();
|
||||
|
||||
AddGroupCommand() {
|
||||
super("addgroup", "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()) ) {
|
||||
return;
|
||||
}
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user