-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (116 loc) · 3.04 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"log"
"math"
"net/http"
"os"
"strconv"
"strings"
"time"
"golang.org/x/net/html"
)
func send(chatId string, function string, text string) error {
log.Printf("%-16s %-20s %s\n", chatId, function, text)
_, err := http.Get("https://api.telegram.org/bot" + os.Getenv("bot_token") +
"/sendMessage?chat_id=" + chatId + "&text=" + text)
return err
}
func sendLog(function string, log string) {
send(os.Getenv("user_chat_id"), function, log)
}
func sendMessage(message string) {
err := send(os.Getenv("channel_chat_id"), "", message)
if err != nil {
sendLog("sendMessage", err.Error())
}
}
func getExchangeRate() (string, float64, bool) {
function := "getExchangeRate"
response, err := http.Get(
"https://finance.naver.com/marketindex/exchangeDetail.naver?marketindexCd=FX_USDKRW",
)
if err != nil {
sendLog(function, err.Error())
return "", 0, false
}
defer response.Body.Close()
document, err := html.Parse(response.Body)
if err != nil {
sendLog(function, err.Error())
return "", 0, false
}
exchangeRateString := ""
for node := range document.Descendants() {
if node.Type == html.ElementNode && node.Data == "td" {
exchangeRateString = node.FirstChild.Data
break
}
}
exchangeRateString = strings.TrimSuffix(strings.ReplaceAll(exchangeRateString, ",", ""), "0")
exchangeRateFloat, err := strconv.ParseFloat(exchangeRateString, 64)
if err != nil {
sendLog(function, err.Error())
return "", 0, false
}
return exchangeRateString, exchangeRateFloat, true
}
func main() {
function := "main"
_, ok := os.LookupEnv("bot_token")
if !ok {
sendLog(function, "Bot token is missing")
return
}
_, ok = os.LookupEnv("user_chat_id")
if !ok {
sendLog(function, "User chat ID is missing")
return
}
_, ok = os.LookupEnv("channel_chat_id")
if !ok {
sendLog(function, "Channel chat ID is missing")
return
}
_, previousFloat, ok := getExchangeRate()
if !ok {
return
}
previousDivision := int(math.Floor(previousFloat / 5))
sendMessage("Program started")
checkExchangeRate := true
checkRunning := true
for {
time.Sleep(30 * time.Second)
now := time.Now().UTC().Add(time.Hour * 9)
if now.Minute()%15 == 0 {
if checkExchangeRate {
checkExchangeRate = false
currentString, currentFloat, ok := getExchangeRate()
if !ok {
break
}
currentDivision := int(math.Floor(currentFloat / 5))
currentModulo := int(math.Floor(currentFloat)) % 5
difference := currentDivision - previousDivision
if (difference == 1 && currentModulo >= 2) || difference > 1 {
sendMessage("△ " + currentString + " 원")
previousDivision = currentDivision
} else if (difference == -1 && currentModulo <= 2) || difference < -1 {
sendMessage("▽ " + currentString + " 원")
previousDivision = currentDivision
}
}
} else {
checkExchangeRate = true
}
if now.Hour()%6 == 0 {
if checkRunning {
checkRunning = false
sendLog(function, "Program running")
}
} else {
checkRunning = true
}
}
sendMessage("Program stopped")
}