diff --git a/bot.go b/bot.go index de8fc00..2f57968 100644 --- a/bot.go +++ b/bot.go @@ -1,10 +1,36 @@ package main import ( + "time" + "watn3y/steamsalty/botIO" + "watn3y/steamsalty/commands" "watn3y/steamsalty/steam" + + "github.com/rs/zerolog/log" ) func bot() { - steam.StartWatchers(botIO.Authenticate()) + updates, bot := botIO.Authenticate() + + go steam.StartWatchers(bot) + + for update := range updates { + log.Debug().Interface("update", update).Msg("Received update") + + if update.Message == nil || update.Message.Text == "" { + log.Debug().Int("UpdateID", update.UpdateID).Msg("Unable to parse update") + continue + } + if update.Message.Time().UTC().Unix() < time.Now().UTC().Unix() { + log.Debug().Int("UpdateID", update.UpdateID).Msg("Skipping old update") + continue + } + + log.Info().Int64("ChatID", update.Message.Chat.ID).Int64("UserID", update.Message.From.ID).Str("Text", update.Message.Text).Msg("Recieved Message") + + if update.Message.IsCommand() { + commands.Commands(update, bot) + } + } } diff --git a/botIO/authenticate.go b/botIO/authenticate.go index d940bfa..64f5d13 100644 --- a/botIO/authenticate.go +++ b/botIO/authenticate.go @@ -6,7 +6,7 @@ import ( "watn3y/steamsalty/config" ) -func Authenticate() *tgbotapi.BotAPI { +func Authenticate() (tgbotapi.UpdatesChannel, *tgbotapi.BotAPI) { bot, err := tgbotapi.NewBotAPI(config.BotConfig.TelegramAPIToken) if err != nil { log.Panic().Err(err).Msg("Failed to authenticate") @@ -14,7 +14,11 @@ func Authenticate() *tgbotapi.BotAPI { bot.Debug = config.BotConfig.DebugMode + updates := tgbotapi.NewUpdate(0) + updates.Timeout = 60 + log.Info().Int64("ID", bot.Self.ID).Str("username", bot.Self.UserName).Msg("Successfully authenticated to Telegram API") - return bot + return bot.GetUpdatesChan(updates), bot + } diff --git a/commands/commands.go b/commands/commands.go new file mode 100644 index 0000000..70fe4b4 --- /dev/null +++ b/commands/commands.go @@ -0,0 +1,60 @@ +package commands + +import ( + "fmt" + "strings" + "watn3y/steamsalty/botIO" + "watn3y/steamsalty/config" + "watn3y/steamsalty/steam" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "github.com/rs/zerolog/log" +) + +func Commands(update tgbotapi.Update, bot *tgbotapi.BotAPI) { + + cmd := strings.ToLower(update.Message.Command()) + + log.Debug().Str("cmd", cmd).Msg("Matching command") + + switch cmd { + case "start": + start(update, bot) + case "info": + info(update, bot) + } +} + +func start(update tgbotapi.Update, bot *tgbotapi.BotAPI) { + message := tgbotapi.MessageConfig{ + BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID}, + ParseMode: "html", + DisableWebPagePreview: false, + Text: "https://github.com/watn3y/steamsalty", + } + botIO.SendMessage(message, bot) +} + +func info(update tgbotapi.Update, bot *tgbotapi.BotAPI) { + if update.Message.Chat.ID != config.BotConfig.ChatID { + return + } + + textInfo := "Watching profiles: " + "\n" + + for _, steamID := range config.BotConfig.Watchers { + profile := steam.GetPlayerDetails(steamID) + + textInfo += fmt.Sprintf(`- %s`, profile.ProfileURL, profile.PersonaName) + "\n" + + } + + message := tgbotapi.MessageConfig{ + BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID}, + ParseMode: "html", + DisableWebPagePreview: true, + Text: textInfo, + } + botIO.SendMessage(message, bot) + +} diff --git a/steam/api.go b/steam/api.go index 80bc253..e79f8b9 100644 --- a/steam/api.go +++ b/steam/api.go @@ -1,12 +1,13 @@ package steam import ( + "watn3y/steamsalty/config" + "github.com/Philipp15b/go-steamapi" "github.com/rs/zerolog/log" - "watn3y/steamsalty/config" ) -func getPlayerDetails(steamID uint64) (summary steamapi.PlayerSummary) { +func GetPlayerDetails(steamID uint64) (summary steamapi.PlayerSummary) { response, err := steamapi.GetPlayerSummaries([]uint64{steamID}, config.BotConfig.SteamAPIKey) if err != nil { diff --git a/steam/profile.go b/steam/profile.go index 24ee41b..1154020 100644 --- a/steam/profile.go +++ b/steam/profile.go @@ -46,14 +46,14 @@ func watcher(bot *tgbotapi.BotAPI, steamID uint64) { } if strings.Contains(currentCommentsPage.CommentsHTML, steamContentCheckText) { - log.Info().Uint64("ProfileID", steamID).Msg("Found new comment(s) still being checked by Steam, retrying in "+ sleeptime.String()) + log.Info().Uint64("ProfileID", steamID).Msg("Found new comment(s) still being checked by Steam, retrying in " + sleeptime.String()) time.Sleep(sleeptime) continue } log.Info().Uint64("ProfileID", steamID).Msg("Found new comment(s)") - profileOwner := getPlayerDetails(steamID) + profileOwner := GetPlayerDetails(steamID) for _, comment := range parseComments(currentCommentsPage) { log.Debug().Interface("Comment", comment).Msg("Processing Comment")