mirror of
https://github.com/tiennm99/store-scraper-bot-java.git
synced 2026-05-14 07:52:47 +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:
+4
-1
@@ -24,7 +24,10 @@ dependencies {
|
||||
implementation("org.apache.logging.log4j:log4j-1.2-api:2.24.3")
|
||||
implementation("org.apache.logging.log4j:log4j-core:2.24.3")
|
||||
implementation("org.apache.logging.log4j:log4j-slf4j2-impl:2.24.3")
|
||||
implementation("org.telegram:telegrambots-abilities:8.0.0")
|
||||
// implementation("org.telegram:telegrambots-abilities:8.0.0")
|
||||
implementation("org.telegram:telegrambots-client:8.0.0")
|
||||
implementation("org.telegram:telegrambots-extensions:8.0.0")
|
||||
implementation("org.telegram:telegrambots-longpolling:8.0.0")
|
||||
|
||||
testAnnotationProcessor("org.projectlombok:lombok:1.18.36")
|
||||
testImplementation(platform("org.junit:junit-bom:5.11.4"))
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
package com.miti99.storescraperbot;
|
||||
|
||||
// TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
|
||||
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
|
||||
import com.miti99.storescraperbot.bot.StoreScrapeBot;
|
||||
import com.miti99.storescraperbot.config.Config;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.telegram.telegrambots.longpolling.TelegramBotsLongPollingApplication;
|
||||
|
||||
@Log4j2
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
|
||||
// to see how IntelliJ IDEA suggests fixing it.
|
||||
System.out.printf("Hello and welcome!");
|
||||
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
// TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
|
||||
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
|
||||
System.out.println("i = " + i);
|
||||
try (var botsApplication = new TelegramBotsLongPollingApplication()) {
|
||||
botsApplication.registerBot(Config.TELEGRAM_BOT_TOKEN, StoreScrapeBot.INSTANCE);
|
||||
log.info("StoreScrapeBot successfully started!");
|
||||
Thread.currentThread().join();
|
||||
} catch (Exception e) {
|
||||
log.error("Error while running bot", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
package com.miti99.storescraperbot.config;
|
||||
|
||||
import com.miti99.storescraperbot.type.Env;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Config {
|
||||
public static final Env ENV = Env.valueOf(System.getenv("ENV"));
|
||||
@@ -10,4 +15,19 @@ public class Config {
|
||||
public static final String COUCHBASE_USERNAME = System.getenv("COUCHBASE_USERNAME");
|
||||
public static final String COUCHBASE_PASSWORD = System.getenv("COUCHBASE_PASSWORD");
|
||||
public static final String COUCHBASE_BUCKET_NAME = System.getenv("COUCHBASE_BUCKET_NAME");
|
||||
|
||||
public static final String TELEGRAM_BOT_TOKEN = System.getenv("TELEGRAM_BOT_TOKEN");
|
||||
public static final String TELEGRAM_BOT_USERNAME = System.getenv("TELEGRAM_BOT_USERNAME");
|
||||
|
||||
public static final Long CREATOR_ID = Long.parseLong(System.getenv("CREATOR_ID"));
|
||||
public static final Set<Long> ADMIN_IDS =
|
||||
Optional.ofNullable(System.getenv("ADMIN_IDS"))
|
||||
.map(
|
||||
v ->
|
||||
Arrays.stream(v.split(","))
|
||||
.map(String::trim)
|
||||
.filter(s -> !s.isEmpty())
|
||||
.map(Long::parseLong)
|
||||
.collect(Collectors.toSet()))
|
||||
.orElse(Collections.emptySet());
|
||||
}
|
||||
|
||||
@@ -8,49 +8,20 @@ import static com.miti99.storescraperbot.config.Config.COUCHBASE_USERNAME;
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.Cluster;
|
||||
import com.couchbase.client.java.ClusterOptions;
|
||||
import com.couchbase.client.java.Collection;
|
||||
import com.couchbase.client.java.Scope;
|
||||
import com.couchbase.client.java.json.JsonObject;
|
||||
import com.couchbase.client.java.kv.GetResult;
|
||||
import com.couchbase.client.java.kv.MutationResult;
|
||||
import com.couchbase.client.java.query.QueryResult;
|
||||
import java.time.Duration;
|
||||
|
||||
public class CouchbaseUtil {
|
||||
public static final Cluster CLUSTER;
|
||||
public static final Bucket BUCKET;
|
||||
|
||||
static {
|
||||
CLUSTER =
|
||||
Cluster.connect(
|
||||
COUCHBASE_CONNECTION_STRING,
|
||||
ClusterOptions.clusterOptions(COUCHBASE_USERNAME, COUCHBASE_PASSWORD)
|
||||
.environment(
|
||||
env -> {
|
||||
// Customize client settings by calling methods on the "env" variable.
|
||||
}));
|
||||
.environment(env -> {}));
|
||||
|
||||
// get a bucket reference
|
||||
BUCKET = CLUSTER.bucket(COUCHBASE_BUCKET_NAME);
|
||||
BUCKET.waitUntilReady(Duration.ofSeconds(10));
|
||||
|
||||
// get a user-defined collection reference
|
||||
Scope scope = BUCKET.scope("tenant_agent_00");
|
||||
Collection collection = scope.collection("users");
|
||||
|
||||
// Upsert Document
|
||||
MutationResult upsertResult =
|
||||
collection.upsert("my-document", JsonObject.create().put("name", "mike"));
|
||||
|
||||
// Get Document
|
||||
GetResult getResult = collection.get("my-document");
|
||||
String name = getResult.contentAsObject().getString("name");
|
||||
System.out.println(name); // name == "mike"
|
||||
|
||||
// Call the query() method on the scope object and store the result.
|
||||
Scope inventoryScope = BUCKET.scope("inventory");
|
||||
QueryResult result = inventoryScope.query("SELECT * FROM airline WHERE id = 10;");
|
||||
|
||||
// Return the result rows with the rowsAsObject() method and print to the terminal.
|
||||
System.out.println(result.rowsAsObject());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user