-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
135 lines (116 loc) · 3.66 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
127
128
129
130
131
132
133
134
135
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
netUrl "net/url"
"os"
"strconv"
"time"
)
const retryInterval = 5 * time.Minute
type Course struct {
ID string `json:"id"`
Name string `json:"names"`
Symbol string `json:"symbol"`
Rank string `json:"rank"`
PriceUSD string `json:"price_usd"`
PriceRUB string `json:"price_rub"`
PriceBTC string `json:"price_btc"`
PercentChange1h string `json:"percent_change_1h"`
PercentChange24h string `json:"percent_change_24h"`
PercentChange7d string `json:"percent_change_7d"`
LastUpdated string `json:"last_updated"`
}
type TelegramClient struct {
token string
}
func (t TelegramClient) sendMsg(channel string, msg string) error {
channel = netUrl.QueryEscape(channel)
msg = netUrl.QueryEscape(msg)
url := fmt.Sprintf("%vsendMessage?chat_id=%v&text=%v", t.token, channel, msg)
_, err := http.Get(url)
return err
}
func (c Course) String() string {
isNegativeGrow := c.PercentChange24h[0] == '-'
var courseGrowEmoji string
if isNegativeGrow {
courseGrowEmoji = "😭️️️"
} else {
courseGrowEmoji = "🤩️"
}
utcSeconds, _ := strconv.ParseInt(c.LastUpdated, 10, 64)
c.LastUpdated = time.Unix(utcSeconds, 0).String()
c.PriceRUB = fmt.Sprintf("%.2f", toFloat(c.PriceRUB))
c.PriceUSD = fmt.Sprintf("%.2f", toFloat(c.PriceUSD))
return fmt.Sprintf("%v\nPrice: %v$\n %v₽\nChange 24h: %v%% %v\nChange 7d: %v%%\nUpdated: %v",
c.Name, c.PriceUSD, c.PriceRUB, c.PercentChange24h, courseGrowEmoji, c.PercentChange7d, c.LastUpdated)
}
func getCourse(currency string) (Course, error) {
resp, _ := http.Get("https://api.coinmarketcap.com/v1/ticker/" + currency + "/?convert=RUB")
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return Course{}, err
}
var courses []Course
err = json.Unmarshal(bodyBytes, &courses)
if err != nil {
return Course{}, fmt.Errorf("failed to parse course: %v. \n Got body: %s", err, bodyBytes)
}
return courses[0], nil
}
func findSecondsUntil(future time.Time) time.Duration {
return time.Duration(future.Sub(time.Now()).Seconds()) * time.Second
}
func main() {
submitHour := convertToInt(os.Getenv("submitTime"))
channelID := os.Getenv("channelID")
currencyName := os.Getenv("currency")
botToken := os.Getenv("botToken")
telegramClient := TelegramClient{token: botToken}
for {
log.Printf("Getting %s course...", currencyName)
course, err := getCourse(currencyName)
if err != nil {
log.Printf("Failed to send message course: %v\n", err)
secondsTillNextTick := timeForTomorrow(submitHour)
log.Printf("Will try again after: %d minutes\n", secondsTillNextTick/time.Minute)
time.Sleep(secondsTillNextTick)
continue
}
log.Println("Course is:\n", course)
err = telegramClient.sendMsg(channelID, course.String())
if err != nil {
log.Printf("Failed to send message course: %v\n", err)
log.Printf("Will try again after: %d minutes\n", retryInterval/time.Minute)
time.Sleep(retryInterval)
continue
}
log.Println("Message has been sent.")
secondsTillNextTick := timeForTomorrow(submitHour)
log.Printf("Sleeping for %v.", secondsTillNextTick)
time.Sleep(secondsTillNextTick)
}
}
func convertToInt(string string) int {
hour, err := strconv.ParseInt(string, 10, 32)
if err != nil {
log.Fatal(err)
}
return int(hour)
}
func toFloat(s string) float64 {
res, _ := strconv.ParseFloat(s, 32)
return res
}
func timeForTomorrow(nextHour int) time.Duration {
now := time.Now()
nextTick := time.Date(
now.Year(), now.Month(), now.Day()+1, nextHour,
0, 0, 0, now.Location())
return findSecondsUntil(nextTick)
}