Skip to content

Commit 3245044

Browse files
committed
Add command to query price of gold
1 parent bfa2d64 commit 3245044

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export BUILDKIT_PROGRESS=plain
2+
13
include .env
24

35
watch:
@@ -20,4 +22,3 @@ build:
2022

2123
run:
2224
docker run --env-file=.env -e SQLITE_DB=:memory: annie
23-

handlers/gold/gold.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package gold
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"goirc/bot"
7+
"net/http"
8+
"os"
9+
)
10+
11+
type response struct {
12+
Timestamp int `json:"timestamp"`
13+
Metal string `json:"metal"`
14+
Currency string `json:"currency"`
15+
Exchange string `json:"exchange"`
16+
Symbol string `json:"symbol"`
17+
PrevClosePrice float64 `json:"prev_close_price"`
18+
OpenPrice float64 `json:"open_price"`
19+
LowPrice float64 `json:"low_price"`
20+
HighPrice float64 `json:"high_price"`
21+
OpenTime int `json:"open_time"`
22+
Price float64 `json:"price"`
23+
Ch float64 `json:"ch"`
24+
Chp float64 `json:"chp"`
25+
Ask float64 `json:"ask"`
26+
Bid float64 `json:"bid"`
27+
PriceGram24K float64 `json:"price_gram_24k"`
28+
PriceGram22K float64 `json:"price_gram_22k"`
29+
PriceGram21K float64 `json:"price_gram_21k"`
30+
PriceGram20K float64 `json:"price_gram_20k"`
31+
PriceGram18K float64 `json:"price_gram_18k"`
32+
PriceGram16K float64 `json:"price_gram_16k"`
33+
PriceGram14K float64 `json:"price_gram_14k"`
34+
PriceGram10K float64 `json:"price_gram_10k"`
35+
}
36+
37+
func getGoldPrice(token string) (float64, error) {
38+
url := "https://www.goldapi.io/api/XAU/CAD"
39+
req, err := http.NewRequest("GET", url, nil)
40+
if err != nil {
41+
return 0.0, err
42+
}
43+
req.Header.Set("x-access-token", token)
44+
45+
client := &http.Client{}
46+
resp, err := client.Do(req)
47+
if err != nil {
48+
return 0.0, err
49+
}
50+
defer resp.Body.Close()
51+
52+
var obj response
53+
json.NewDecoder(resp.Body).Decode(&obj)
54+
55+
fmt.Printf("Price: %f\n", obj.Price)
56+
57+
return obj.Price, nil
58+
}
59+
60+
func Handle(params bot.HandlerParams) error {
61+
price, err := getGoldPrice(os.Getenv("GOLD_API_TOKEN"))
62+
if err != nil {
63+
return err
64+
}
65+
66+
params.Privmsgf(params.Target, "gold is $%.2f / oz t.", price)
67+
68+
return nil
69+
}

handlers/gold/gold_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package gold
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestGetGoldPrice(t *testing.T) {
9+
var token = os.Getenv("GOLD_API_TOKEN")
10+
price, err := getGoldPrice(token)
11+
if err != nil {
12+
t.Fatal(err)
13+
}
14+
t.Logf("Price: %f\n", price)
15+
}

main.go

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"goirc/handlers"
77
"goirc/handlers/day"
88
"goirc/handlers/epigram"
9+
"goirc/handlers/gold"
910
"goirc/handlers/kinfonet"
1011
"goirc/handlers/mlb"
1112
"goirc/handlers/weather"
@@ -93,4 +94,5 @@ func addHandlers(b *bot.Bot) {
9394
b.Handle(`^!w$`, weather.Handle)
9495
b.Handle(`^!xweather (.+)$`, weather.XHandle)
9596
b.Handle(`^!k`, kinfonet.TodaysQuoteHandler)
97+
b.Handle(`^!gold`, gold.Handle)
9698
}

0 commit comments

Comments
 (0)