- added start command
- added info command
This commit is contained in:
Noah 2024-12-21 08:28:28 +01:00
parent 9c64a3e4b5
commit 453f03d832
5 changed files with 98 additions and 7 deletions

28
bot.go
View file

@ -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)
}
}
}

View file

@ -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
}

60
commands/commands.go Normal file
View file

@ -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 := "<b>Watching profiles:</b> " + "\n"
for _, steamID := range config.BotConfig.Watchers {
profile := steam.GetPlayerDetails(steamID)
textInfo += fmt.Sprintf(`- <a href="%s">%s</a>`, 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)
}

View file

@ -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 {

View file

@ -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")