-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
97 lines (79 loc) · 2.63 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
package main
import (
"fmt"
"log"
"time"
"github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/kasmetski/cmcAPI"
)
//GetCoinPrice - Get price information from Coin Market Cap and make a message
func GetCoinPrice(s string) (msg string, err error) {
coin, err := cmcAPI.GetCoinInfo(s)
if err != nil {
msg = "Can't find anything. Type the full name of the coin"
return
}
name := fmt.Sprintf("Name: %s | %s | #%d\n", coin.Name, coin.Symbol, coin.Rank)
price := fmt.Sprintf("PriceBTC: %f\nPriceUSD: %.2f\n", coin.PriceBtc, coin.PriceUsd)
change := fmt.Sprintf("Change 1H/24H/7d: %.2f | %.2f | %.2f\n", coin.PercentChange1H, coin.PercentChange24H, coin.PercentChange7D)
msg = name + price + change
return
}
//GetCoinInfo - Get full infomartion from Coin Market Cap and make a message
func GetCoinInfo(s string) (msg string, err error) {
coin, err := cmcAPI.GetCoinInfo(s)
if err != nil {
msg = "Can't find anything. Type the full name of the coin"
return
}
name := fmt.Sprintf("Name: %s | %s | #%d\n", coin.Name, coin.Symbol, coin.Rank)
supply := fmt.Sprintf("Available Supply: %d\n", int(coin.AvailableSupply))
mcap := fmt.Sprintf("MarketCap: %d\n", int(coin.MarketCapUsd))
volume := fmt.Sprintf("Volume: %d\n", int(coin.Two4HVolumeUsd))
price := fmt.Sprintf("PriceBTC: %f\nPriceUSD: %.2f\n", coin.PriceBtc, coin.PriceUsd)
change := fmt.Sprintf("Change 1H/24H/7d: %.2f | %.2f | %.2f\n", coin.PercentChange1H, coin.PercentChange24H, coin.PercentChange7D)
msg = name + supply + mcap + volume + price + change
return
}
func main() {
bot, err := tgbotapi.NewBotAPI("TOKEN HERE")
if err != nil {
log.Panic(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
time.Sleep(time.Millisecond * 500)
updates.Clear()
for update := range updates {
if update.Message == nil {
continue
}
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
if update.Message.IsCommand() {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
commandArgs := update.Message.CommandArguments()
switch update.Message.Command() {
case "help":
msg.Text = "there is no help for the people here\nbut you can try /status /info /price"
case "status":
msg.Text = "I'm ok. Thanks for checking"
case "info":
msg.Text, err = GetCoinInfo(commandArgs)
if err != nil {
log.Println(err)
}
case "price":
msg.Text, err = GetCoinPrice(commandArgs)
if err != nil {
log.Println(err)
}
default:
msg.Text = "I don't know that command, try help"
}
bot.Send(msg)
}
}
}