mirror of
https://github.com/tiennm99/store-scraper-bot.git
synced 2026-10-17 02:14:40 +00:00
feat: port logic from Java original to align with current Java behavior
- Switch Telegram parse mode to HTML to match Java client. - Rename command identifiers to Java's: delgroup/delapple/delgoogle/ checkappscore/rawappleapp/rawgoogleapp. - Move admin singleton to "common" collection at _id="admin". - Store group _id as string-of-int64 to match Java AbstractModel schema. - Add `class` discriminator field on persisted models. - Expand AppleAppResponse (42 fields) and GoogleAppResponse (63 fields, with nested Category/Feature) to match Java records. - Add AppleAppRequest (track id or bundle id) / GoogleAppRequest types. - Implement Raw* commands as Telegram document attachments instead of truncated text. - Group-authorization gate (admin.HasGroup(chatId)) on non-admin commands. - Cache TTL via millis timestamp; round score to 1 decimal; weekend silent send in scheduler. - Match Java table renderer: " │ " separators, "─┼─" rows every 5 lines. - Prefer MONGODB_CONNECTION_STRING env (Java parity); auto-extract DB name from URI with fallback to "store-scraper-bot". Note: This is an AI-assisted port (not human-verified). Behavior parity with the Java implementation has not been tested end-to-end; treat as a starting point.
This commit is contained in:
1 parent
cfc8d67641
commit
e695d9b223
34 files changed
+1132
-1149
No files matched your search
@@ -2,69 +2,53 @@ package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/miti99/store-scraper-bot-go/internal/api/google"
|
||||
"github.com/miti99/store-scraper-bot-go/internal/api/google/request"
|
||||
"github.com/miti99/store-scraper-bot-go/internal/config"
|
||||
"github.com/miti99/store-scraper-bot-go/internal/repository"
|
||||
)
|
||||
|
||||
// /addgoogle <appId> [country=vn] — Java AddGoogleAppCommand.
|
||||
type AddGoogleAppCommand struct {
|
||||
BaseCommand
|
||||
adminRepo *repository.AdminRepository
|
||||
groupRepo *repository.GroupRepository
|
||||
googleScraper *google.GoogleScraper
|
||||
cfg *config.Config
|
||||
adminRepo *repository.AdminRepository
|
||||
groupRepo *repository.GroupRepository
|
||||
scraper *google.GoogleScraper
|
||||
}
|
||||
|
||||
func NewAddGoogleAppCommand(
|
||||
cfg *config.Config,
|
||||
adminRepo *repository.AdminRepository,
|
||||
groupRepo *repository.GroupRepository,
|
||||
googleScraper *google.GoogleScraper,
|
||||
) *AddGoogleAppCommand {
|
||||
return &AddGoogleAppCommand{
|
||||
BaseCommand: BaseCommand{cfg: cfg},
|
||||
adminRepo: adminRepo,
|
||||
groupRepo: groupRepo,
|
||||
googleScraper: googleScraper,
|
||||
}
|
||||
func NewAddGoogleAppCommand(cfg *config.Config, adminRepo *repository.AdminRepository, groupRepo *repository.GroupRepository, scraper *google.GoogleScraper) *AddGoogleAppCommand {
|
||||
return &AddGoogleAppCommand{cfg: cfg, adminRepo: adminRepo, groupRepo: groupRepo, scraper: scraper}
|
||||
}
|
||||
|
||||
func (c *AddGoogleAppCommand) Execute(message *tgbotapi.Message) string {
|
||||
if !c.requireAdmin(message) {
|
||||
return "You are not authorized to use this command."
|
||||
func (c *AddGoogleAppCommand) Execute(msg *tgbotapi.Message, sender Sender) {
|
||||
if !authorizeGroup(msg.Chat.ID, c.adminRepo, sender) {
|
||||
return
|
||||
}
|
||||
|
||||
groupID := message.Chat.ID
|
||||
hasGroup, err := c.adminRepo.HasGroup(groupID)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Failed to check group: %v", err)
|
||||
args := splitArgs(msg.CommandArguments())
|
||||
if len(args) < 1 || len(args) > 2 {
|
||||
_ = sender.SendMessage(msg.Chat.ID, "Invalid arguments")
|
||||
return
|
||||
}
|
||||
if !hasGroup {
|
||||
return "This group is not registered. Please use /addgroup first."
|
||||
}
|
||||
|
||||
args := strings.Fields(message.CommandArguments())
|
||||
if len(args) == 0 {
|
||||
return "Usage: /addgoogle <appId> [country]\nExample: /addgoogle com.example.app vn"
|
||||
}
|
||||
|
||||
appID := args[0]
|
||||
country := "vn"
|
||||
if len(args) > 1 {
|
||||
if len(args) == 2 {
|
||||
country = args[1]
|
||||
}
|
||||
|
||||
// Verify app exists
|
||||
app, err := c.googleScraper.GetApp(appID, country)
|
||||
resp, err := c.scraper.FetchAndCache(request.New(appID, country))
|
||||
if err != nil || resp == nil {
|
||||
_ = sender.SendMessage(msg.Chat.ID, "Error when request app info")
|
||||
return
|
||||
}
|
||||
added, err := c.groupRepo.AddGoogleApp(msg.Chat.ID, appID, country)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Failed to fetch app from store: %v", err)
|
||||
_ = sender.SendMessage(msg.Chat.ID, "Internal server error")
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.groupRepo.AddGoogleApp(groupID, appID, country); err != nil {
|
||||
return fmt.Sprintf("Failed to add app: %v", err)
|
||||
if !added {
|
||||
_ = sender.SendMessage(msg.Chat.ID, fmt.Sprintf("Google app <code>%s</code> is already added", appID))
|
||||
return
|
||||
}
|
||||
|
||||
return fmt.Sprintf("Google app added successfully:\n*%s*\nApp ID: %s\nCountry: %s\nScore: %.1f", app.Title, appID, country, app.Score)
|
||||
_ = sender.SendMessage(msg.Chat.ID, fmt.Sprintf("Google app <code>%s</code>, country <b>%s</b> added successfully", appID, country))
|
||||
}
|
||||
Reference in new issue
Block a user