Skip to content

Commit

Permalink
add antam scout
Browse files Browse the repository at this point in the history
  • Loading branch information
100nandoo committed Oct 14, 2024
1 parent 75a0764 commit 4bfd4bb
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
41 changes: 41 additions & 0 deletions antam/scout.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
51 changes: 45 additions & 6 deletions antam/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"gobot/pkg"
"log"
"os"
"strconv"
"time"

"gopkg.in/telebot.v3"
Expand All @@ -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),
Expand All @@ -36,33 +42,31 @@ 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{
ParseMode: telebot.ModeMarkdown,
})
}

// 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,
})
})

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{
ParseMode: telebot.ModeMarkdown,
})
}

// 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,
Expand All @@ -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
}
}
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func main() {

warta.Scouting(false)

antam.Scouting(false)

go summarizer.Run()
go antam.Run()

Expand Down

0 comments on commit 4bfd4bb

Please sign in to comment.