-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (145 loc) · 3.84 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
158
159
160
161
162
163
164
165
package main
import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/joho/godotenv"
"github.com/labstack/echo/v4"
"github.com/nntaoli-project/goex"
"github.com/nntaoli-project/goex/huobi"
"github.com/rocboss/go-huobi-monitor/utils"
)
func main() {
// 加载配置
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
// 初始化Redis
if initClient() != nil {
log.Fatal("Error connect to redis server")
}
spotWs := huobi.NewSpotWs()
spotWs.TickerCallback(func(ticker *goex.Ticker) {
// 获取所有监听规则
monitors, err := rdb.SMembers("HBPairs:" + ticker.Pair.String()).Result()
if err == nil {
if len(monitors) > 0 {
alerts := []string{}
for _, monitor := range monitors {
monitorRune := []rune(monitor)
if string(monitorRune[:1]) == "<" {
target, err := strconv.ParseFloat(string(monitorRune[1:]), 64)
if err != nil {
continue
}
// 小于类型匹配
if ticker.Last <= target {
alerts = append(alerts, "[Hit] "+ticker.Pair.String()+" : "+monitor, fmt.Sprintf("Latest Price: %.4f", ticker.Last), "")
}
}
if string(monitorRune[:1]) == ">" {
target, err := strconv.ParseFloat(string(monitorRune[1:]), 64)
if err != nil {
continue
}
// 小于类型匹配
if ticker.Last >= target {
alerts = append(alerts, "[Hit] "+ticker.Pair.String()+" : "+monitor, fmt.Sprintf("Latest Price: %.4f", ticker.Last), "")
}
}
}
if len(alerts) > 0 {
log.Println(alerts)
_, err = rdb.Get("MSG_LOCK:" + ticker.Pair.String()).Result()
if err != nil {
utils.PushMessage(append(alerts, "(该交易对提醒静默1分钟)"))
// 防止重复发送(1分钟再次发送)
rdb.Set("MSG_LOCK:"+ticker.Pair.String(), 1, time.Minute)
}
}
}
}
})
// 初始化监听
pairs, err := rdb.Keys("HBPairs:*").Result()
if err == nil {
for _, pair := range pairs {
spotWs.SubscribeTicker(goex.NewCurrencyPair2(strings.Split(pair, ":")[1]))
}
}
e := echo.New()
e.POST("/dingtalk_webhook", func(c echo.Context) error {
req := new(ReqData)
if err = c.Bind(req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
// TODO 校验签名
content := req.Text.Content
// eg. add zksusdt 1.2
commands := strings.Split(strings.Trim(content, " "), " ")
if len(commands) == 3 {
switch commands[0] {
case "add":
// 添加监听
rdb.SAdd("HBPairs:"+commands[1], commands[2])
spotWs.SubscribeTicker(goex.NewCurrencyPair2(commands[1]))
utils.PushMessage([]string{"命令执行成功"})
case "del":
rdb.SRem("HBPairs:"+commands[1], commands[2])
utils.PushMessage([]string{"命令执行成功"})
case "list":
monitors := []string{}
pairs, err := rdb.Keys("HBPairs:*").Result()
if err == nil {
for _, pair := range pairs {
ms, err := rdb.SMembers(pair).Result()
if err == nil {
for _, monitor := range ms {
monitors = append(monitors, strings.Split(pair, ":")[1]+": "+monitor)
}
}
}
}
utils.PushMessage(monitors)
default:
utils.PushMessage([]string{
"可用命令",
"",
"添加监控",
"add [COIN] [PRICE]",
"eg. add BTC_USDT <43000",
"",
"删除监控",
"del [COIN] [PRICE]",
"eg. del BTC_USDT <43000",
"",
"查看监控",
"list all coins",
})
}
} else {
utils.PushMessage([]string{
"可用命令",
"",
"添加监控",
"add [COIN] [PRICE]",
"eg. add BTC_USDT <43000",
"",
"删除监控",
"del [COIN] [PRICE]",
"eg. del BTC_USDT <43000",
"",
"查看监控",
"list all coins",
})
}
// utils.PushMessage()
return c.String(http.StatusOK, "success")
})
e.Logger.Fatal(e.Start(":" + os.Getenv("APP_PORT")))
}