-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
176 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters