-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
126 lines (102 loc) · 3.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"fmt"
"io/ioutil"
"log"
"math"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/preichenberger/go-coinbasepro"
"gopkg.in/yaml.v2"
)
var numericKeyboard = tgbotapi.NewReplyKeyboard(
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("BTC"),
tgbotapi.NewKeyboardButton("ETH"),
tgbotapi.NewKeyboardButton("XRP"),
),
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("EOS"),
tgbotapi.NewKeyboardButton("SNX"),
tgbotapi.NewKeyboardButton("LINK"),
),
)
var client = coinbasepro.NewClient()
type conf struct {
APIKey string `yaml:"API_KEY"`
}
func main() {
c, err := readConf("conf.yaml")
if err != nil {
log.Fatal(err)
}
client.UpdateConfig(&coinbasepro.ClientConfig{
BaseURL: "https://api.pro.coinbase.com",
})
bot, err := tgbotapi.NewBotAPI(c.APIKey)
if err != nil {
log.Panic(err)
}
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, _ := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message == nil {
continue
}
if update.Message.Text == "/start" {
reply := "Welcome, my name is Kraken! Send me a ticker (e.g. `BTC`) or pick one from the buttons below. You can find my source code [here](www.github.com/sjagoori/krakenbot)."
message := tgbotapi.NewMessage(update.Message.Chat.ID, reply)
message.ParseMode = "markdown"
message.ReplyMarkup = numericKeyboard
bot.Send(message)
} else {
send(bot, update.Message.Chat.ID, update.Message.Text, getCoinPrice(update.Message.Text))
}
}
}
func getCoinPrice(coinType string) string {
bookUSD, err := client.GetBook(coinType+"-USD", 1)
if err != nil {
return "Coin not found"
}
priceUSD := bookUSD.Asks[0].Price
stamp := time.Now()
location, _ := time.LoadLocation("Europe/Amsterdam")
startOfTheDay := time.Date(stamp.Year(), stamp.Month(), stamp.Day(), int(0), int(0), int(0), int(0), location)
now := time.Date(stamp.Year(), stamp.Month(), stamp.Day(), stamp.Hour(), stamp.Minute(), stamp.Second(), stamp.Nanosecond(), location)
data24h := coinbasepro.GetHistoricRatesParams{Start: startOfTheDay, End: now, Granularity: 86400}
product24h, err := client.GetHistoricRates(coinType+"-USD", data24h)
if err != nil {
return fmt.Sprintf("%s", err)
}
table := "\n\n*24H:*\t\t\t\t\t\t\t`" + fmt.Sprintf("%v", (math.Round(percentageChange(product24h[0].Open, product24h[0].Close)*100)/100)) + "%" +
"`\n*Lowest:*\t `" + "$" + fmt.Sprintf("%v", product24h[0].Low) +
"`\n*Highest:* `" + "$" + fmt.Sprintf("%v", product24h[0].High) +
"`\n\n_" + stamp.Format("15:04:05 02-01") + "_"
return "*Price:*\t\t\t\t\t`$" + priceUSD + "`" + table
}
func readConf(filename string) (*conf, error) {
buf, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
c := &conf{}
err = yaml.Unmarshal(buf, c)
if err != nil {
return nil, fmt.Errorf("in file %q: %v", filename, err)
}
return c, nil
}
func percentageChange(old, new float64) float64 {
diff := float64(new - old)
return (diff / float64(old)) * 100
}
func send(bot *tgbotapi.BotAPI, chatid int64, coin string, price string) {
reply := "`" + coin + "`\n\n" + price
message := tgbotapi.NewMessage(chatid, reply)
message.ParseMode = "markdown"
message.ReplyMarkup = numericKeyboard
bot.Send(message)
}