Skip to content

Commit

Permalink
add antam bot
Browse files Browse the repository at this point in the history
  • Loading branch information
100nandoo committed Oct 7, 2024
1 parent e41f3b3 commit f2635de
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 4 deletions.
81 changes: 81 additions & 0 deletions antam/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package antam

import (
"fmt"
"net/http"
"strings"

"github.com/PuerkitoBio/goquery"
)

// GoldPrice represents the sell and buy prices for gold
type GoldPrice struct {
Buy string
Sell string
}

// Get gold prices from the website. It returns a GoldPrice struct.
func getGoldPricesFromHTML() (*GoldPrice, error) {
resp, err := http.Get("https://harga-emas.org/")
if err != nil {
return nil, fmt.Errorf("failed to fetch page: %w", err)
}
defer resp.Body.Close()

doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to parse HTML: %w", err)
}

var sellPrice, buyPrice string

// Find the first table with the class "in_table"
table := doc.Find(".in_table").First()

// Select the row which contains the prices (4th row)
priceRow := table.Find("tr").Eq(3)

// Get the last two <td> elements for sell and buy prices
buyPrice = priceRow.Find("td").Eq(8).Text()
sellPrice = priceRow.Find("td").Eq(9).Text()

return &GoldPrice{
Buy: buyPrice,
Sell: sellPrice,
}, nil
}

func getPluangGoldPricesFromHTML() (*GoldPrice, error) {
resp, err := http.Get("https://pluang.com/widgets/price-graph/desktop-vertical")
if err != nil {
return nil, fmt.Errorf("failed to fetch page: %w", err)
}
defer resp.Body.Close()

doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to parse HTML: %w", err)
}

var sellPrice, buyPrice string

doc.Find(".halfwidth").Each(func(i int, s *goquery.Selection) {
// Find the <p> element within the current <div>
s.Find("p").Each(func(j int, p *goquery.Selection) {
text := strings.TrimSpace(p.Text())
// Remove the "/g" suffix from the prices
text = strings.ReplaceAll(text, "/g", "")
text = strings.ReplaceAll(text, "Rp", "")
if i == 0 {
sellPrice = text
} else if i == 1 {
buyPrice = text
}
})
})

return &GoldPrice{
Buy: buyPrice,
Sell: sellPrice,
}, nil
}
79 changes: 79 additions & 0 deletions antam/telegram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package antam

import (
"fmt"
"gobot/config"
"gobot/pkg"
"log"
"os"
"time"

"gopkg.in/telebot.v3"
tele "gopkg.in/telebot.v3"
)

const (

helpMessage = `Halo ini adalah bot cek harga emas antam
*Cara penggunaan:*
- Kirim /start untuk cek harga jual beli emas antam
- Kirim /p untuk cek harga jual beli emas antam di pluang
Emas Antam Bot dibuat dengan ❤️ oleh @crossix`
)

func Run() {
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
}

b.Handle("/start", func(c tele.Context) error {
prices, 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)

return c.Send(responseMessage, &telebot.SendOptions{
ParseMode: telebot.ModeMarkdown,
})
})

b.Handle("/p", func(c tele.Context) error {
prices, 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)

return c.Send(responseMessage, &telebot.SendOptions{
ParseMode: telebot.ModeMarkdown,
})
})

b.Handle("/help", func(c tele.Context) error {
return c.Send(helpMessage, &telebot.SendOptions{
ParseMode: telebot.ModeMarkdown,
})
})

b.Start()
}
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package config

const SupabaseUrl, SupabaseKey string = "SUPABASE_URL", "SUPABASE_KEY"

const TelegramBot string = "TELEGRAM_BOT"
const AntamTelegramBot string = "ANTAM_TELEGRAM_BOT"
const CaptainKiddBot string = "CAPTAIN_KIDD_BOT"

const TelegramDebug string = "TELEGRAM_CHANNEL_DEBUG"
const TelegramFreeGames string = "TELEGRAM_CHANNEL_FREE_GAMES"
const TelegramRemoteOk string = "TELEGRAM_CHANNEL_REMOTE_OK"
Expand Down
2 changes: 1 addition & 1 deletion freegames/supabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func GetAllPost() []SupabasePost {
var results []SupabasePost
err := pkg.SupabaseClient.DB.From(dbName).Select("*").Execute(&results)
if err != nil {
pkg.LogWithTimestamp("Error calling GetAllPost", err)
pkg.LogWithTimestamp("Error calling GetAllPost %v", err)
return nil
}
return results
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
)

require (
github.com/PuerkitoBio/goquery v1.9.2 // indirect
github.com/PuerkitoBio/goquery v1.10.0 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-querystring v1.1.0 // indirect
Expand All @@ -23,6 +23,6 @@ require (
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/stretchr/testify v1.8.3 // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/text v0.19.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PuerkitoBio/goquery v1.9.2 h1:4/wZksC3KgkQw7SQgkKotmKljk0M6V8TUvA8Wb4yPeE=
github.com/PuerkitoBio/goquery v1.9.2/go.mod h1:GHPCaP0ODyyxqcNoFGYlAprUFH81NuRPd0GX3Zu2Mvk=
github.com/PuerkitoBio/goquery v1.10.0 h1:6fiXdLuUvYs2OJSvNRqlNPoBm6YABE226xrbavY5Wv4=
github.com/PuerkitoBio/goquery v1.10.0/go.mod h1:TjZZl68Q3eGHNBA8CWaxAN7rOU1EbDz3CWuolcO5Yu4=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand Down Expand Up @@ -512,6 +514,8 @@ golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -644,6 +648,8 @@ golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"gobot/antam"
"gobot/freegames"
"gobot/pkg"
"gobot/remoteok"
Expand All @@ -19,5 +20,7 @@ func main() {
warta.Scouting(false)

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

pkg.StartBlocking()
}

0 comments on commit f2635de

Please sign in to comment.