diff --git a/antam/scout.go b/antam/scout.go new file mode 100644 index 0000000..540f1c1 --- /dev/null +++ b/antam/scout.go @@ -0,0 +1,41 @@ +package antam + +import ( + "fmt" + "gobot/pkg" +) + +/* +Scouting +Run every day at specified times. +*/ +func Scouting(now bool) { + // Helper function to log and send price + sendGoldPriceAt := func(timeStr string, immediate bool) { + execute := func() { + pkg.LogWithTimestamp(fmt.Sprintf("Scouting Antam at %s", timeStr)) + price, err := getGoldPricesFromHTML() + if err != nil { + pkg.LogWithTimestamp(fmt.Sprintf("Error fetching gold prices at %s: %v", timeStr, err)) + return + } + SendPrice(*price) + } + + if immediate { + execute() + } else { + pkg.EverydayAtThisHour(execute, timeStr) + } + } + + if now { + pkg.LogWithTimestamp("Running scouting logic immediately") + sendGoldPriceAt("now", true) + } else { + pkg.LogWithTimestamp("Scheduling scouting antam price") + sendGoldPriceAt("10:05", false) + sendGoldPriceAt("15:05", false) + sendGoldPriceAt("16:30", false) + } +} \ No newline at end of file diff --git a/antam/telegram.go b/antam/telegram.go index 007c9ef..78c1e8c 100644 --- a/antam/telegram.go +++ b/antam/telegram.go @@ -6,6 +6,7 @@ import ( "gobot/pkg" "log" "os" + "strconv" "time" "gopkg.in/telebot.v3" @@ -23,6 +24,11 @@ const ( Emas Antam Bot dibuat dengan ❤️ oleh @crossix` ) +// Helper function to format the gold price response message +func formatGoldPriceResponse(price GoldPrice) string { + return fmt.Sprintf("`Harga Emas:\n\nBeli: %s\nJual: %s`", price.Buy, price.Sell) +} + func Run() { pref := tele.Settings{ Token: os.Getenv(config.AntamTelegramBot), @@ -36,7 +42,7 @@ func Run() { } b.Handle("/start", func(c tele.Context) error { - prices, err := getGoldPricesFromHTML() + price, err := getGoldPricesFromHTML() if err != nil { pkg.LogWithTimestamp("Error fetching gold prices: %v", err) return c.Send("Sorry, I couldn't fetch the gold prices right now.", &telebot.SendOptions{ @@ -44,8 +50,7 @@ func Run() { }) } - // Prepare the response message with prices - responseMessage := fmt.Sprintf("`Harga Emas Antam:\n\nBeli: %s\nJual: %s`", prices.Buy, prices.Sell) + responseMessage := formatGoldPriceResponse(*price) return c.Send(responseMessage, &telebot.SendOptions{ ParseMode: telebot.ModeMarkdown, @@ -53,7 +58,7 @@ func Run() { }) b.Handle("/p", func(c tele.Context) error { - prices, err := getPluangGoldPricesFromHTML() // Fetch gold prices + price, err := getPluangGoldPricesFromHTML() // Fetch gold prices if err != nil { pkg.LogWithTimestamp("Error fetching gold prices: %v", err) return c.Send("Sorry, I couldn't fetch the gold prices right now.", &telebot.SendOptions{ @@ -61,8 +66,7 @@ func Run() { }) } - // Prepare the response message with prices - responseMessage := fmt.Sprintf("`Harga di Pluang:\n\nBeli: %s\nJual: %s`", prices.Buy, prices.Sell) + responseMessage := formatGoldPriceResponse(*price) return c.Send(responseMessage, &telebot.SendOptions{ ParseMode: telebot.ModeMarkdown, @@ -77,3 +81,38 @@ func Run() { b.Start() } + +/* +SendPrice + +Send antam gold price to config.ChannelAntam channel +*/ +func SendPrice(price GoldPrice) { + pref := tele.Settings{ + Token: os.Getenv(config.AntamTelegramBot), + Poller: &tele.LongPoller{Timeout: 10 * time.Second}, + } + + b, err := tele.NewBot(pref) + if err != nil { + log.Fatal(err) + return + } + + num, chatIdErr := strconv.ParseInt(os.Getenv(config.ChannelAntam), 10, 64) + if chatIdErr != nil { + pkg.LogWithTimestamp("Error from send price chatIdErr: %v", chatIdErr) + return + } + + responseMessage := formatGoldPriceResponse(price) + + _, sendErr := b.Send(tele.ChatID(num), responseMessage, &telebot.SendOptions{ + ParseMode: telebot.ModeMarkdown, + }) + + if sendErr != nil { + pkg.LogWithTimestamp("Error from send price: %v", err) + return + } +} diff --git a/config/config.go b/config/config.go index 0aa4c7c..28f5117 100644 --- a/config/config.go +++ b/config/config.go @@ -10,5 +10,6 @@ const TelegramDebug string = "TELEGRAM_CHANNEL_DEBUG" const TelegramFreeGames string = "TELEGRAM_CHANNEL_FREE_GAMES" const TelegramRemoteOk string = "TELEGRAM_CHANNEL_REMOTE_OK" const TelegramWarta string = "TELEGRAM_CHANNEL_WARTA" +const ChannelAntam string = "TELEGRAM_CHANNEL_ANTAM" const SmmryKey string = "SMMRY_KEY" diff --git a/main.go b/main.go index ad7cca7..8648db7 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,8 @@ func main() { warta.Scouting(false) + antam.Scouting(false) + go summarizer.Run() go antam.Run()