Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit f93ba94

Browse files
author
kirito
committed
update v0.0.2
1 parent 068df7f commit f93ba94

14 files changed

Lines changed: 190 additions & 10 deletions

bot/bot.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"net/url"
99
"os"
10+
"strings"
1011
)
1112

1213
type Bot struct {
@@ -48,6 +49,10 @@ func (bot *Bot) Dispatch(update *tgbotapi.Update) {
4849
if entity.Type == "bot_command" {
4950
bot.Logger.Debug("offse:len", entity.Offset, entity.Length, "msg" , update.Message.Text)
5051
url := update.Message.Text[entity.Offset : entity.Offset+entity.Length]
52+
i := strings.Index(url, "@") // 提取命令 /help@kirito_testonly_bot
53+
if i != -1 {
54+
url = url[:i]
55+
}
5156
bot.Router.DoHandle(url, update)
5257
return
5358
}

callback.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,30 @@ import (
1414
func help(update *tgbotapi.Update) {
1515
Log.Debug("recv msg:", update.Message.Text, "chatID:", update.Message.Chat.ID)
1616
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
17-
msg.Text = ParseToString("help", nil)
17+
msg.Text = "@" + update.Message.From.UserName + " - " + update.Message.From.FirstName + "\n"
18+
msg.Text += ParseToString("help", nil)
1819
msg.ParseMode = "Markdown"
1920
send(&msg, 5)
2021
}
2122

2223
func newest(update *tgbotapi.Update) {
2324
Log.Debug("recv msg:", update.Message.Text, "chatID:", update.Message.Chat.ID)
2425
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
25-
26+
msg.Text = "@" + update.Message.From.UserName + " - " + update.Message.From.FirstName + "\n"
2627
block, err := getNewestBlock()
2728
if err != nil {
2829
Log.Error("GetBlockInfo:", err)
2930
return
3031
}
31-
msg.Text = ParseToString("block", block)
32+
msg.Text += ParseToString("block", block)
3233
msg.ParseMode = "Markdown"
3334
send(&msg, 5)
3435
}
3536

3637
func recent(update *tgbotapi.Update) {
3738
Log.Debug("recv msg:", update.Message.Text, "chatID:", update.Message.Chat.ID)
3839
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
40+
msg.Text = "@" + update.Message.From.UserName + " - " + update.Message.From.FirstName + "\n"
3941
msg.ParseMode = "Markdown"
4042

4143
arr := make([]*btcinfo.Block, 0)
@@ -55,13 +57,14 @@ func recent(update *tgbotapi.Update) {
5557
arr = append(arr, b)
5658
}
5759

58-
msg.Text = ParseToString("recent", arr)
60+
msg.Text += ParseToString("recent", arr)
5961
send(&msg, 5)
6062
}
6163

6264
func q(update *tgbotapi.Update) {
6365
Log.Debug("recv msg:", update.Message.Text, "chatID:", update.Message.Chat.ID)
6466
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
67+
msg.Text = "@" + update.Message.From.UserName + " - " + update.Message.From.FirstName + "\n"
6568
msg.ParseMode = "Markdown"
6669
fields := strings.Fields(update.Message.Text)
6770
var want string
@@ -86,13 +89,13 @@ func q(update *tgbotapi.Update) {
8689
}
8790
if len(want) < 20 || strings.HasPrefix(want, "000000") { // block 块
8891
iface, err = API.GetBlockInfo(want)
89-
msg.Text = ParseToString("block", iface)
92+
msg.Text += ParseToString("block", iface)
9093
} else if len(want) > 20 && len(want) < 40 { // address 地址
9194
iface, err = API.GetAddressInfo(want)
92-
msg.Text = ParseToString("address", iface)
95+
msg.Text += ParseToString("address", iface)
9396
} else if len(want) == 64 { // transaction 交易
9497
iface, err = API.GetTransactionInfo(want)
95-
msg.Text = ParseToString("transaction", iface)
98+
msg.Text += ParseToString("transaction", iface)
9699
} else {
97100
return
98101
}
@@ -103,6 +106,20 @@ func q(update *tgbotapi.Update) {
103106
send(&msg, 5)
104107
}
105108

109+
func quotes(update *tgbotapi.Update) {
110+
Log.Debug("recv msg:", update.Message.Text, "chatID:", update.Message.Chat.ID)
111+
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
112+
msg.Text = "@" + update.Message.From.UserName + " - " + update.Message.From.FirstName + "\n"
113+
msg.ParseMode = "Markdown"
114+
115+
klines, err := HuobiAPI.GetKLine("btcusdt", "60min", 1)
116+
if err != nil {
117+
return
118+
}
119+
msg.Text += ParseToString("quotes", klines[0])
120+
send(&msg, 5)
121+
}
122+
106123
func unknow(update *tgbotapi.Update) {
107124
q(update)
108125
}
@@ -138,7 +155,7 @@ func send(msg *tgbotapi.MessageConfig, duration int) {
138155
for ; cnt > 0; cnt-- {
139156
_, err := Bot.TgBot.Send(msg)
140157
if err != nil {
141-
Log.Debug("send msg:", err, " will retry...", cnt)
158+
Log.Debug("send msg:", err, " will retry...", cnt-1)
142159
<-time.After(time.Duration(duration)*time.Second)
143160
} else {
144161
break

houbi_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import (
4+
"github.com/zshorz/fkbro/util"
5+
"testing"
6+
)
7+
8+
func Test_KLine(t *testing.T) {
9+
huobi := NewHuobi(util.Config.Proxy)
10+
t.Log(util.Config.Proxy)
11+
klines, err := huobi.GetKLine("btcusdt", "60min", 1)
12+
t.Log(klines[0], err)
13+
}

huobi.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"github.com/zshorz/ezlog"
6+
"io/ioutil"
7+
"net/http"
8+
"net/url"
9+
"os"
10+
"strconv"
11+
)
12+
13+
/// https://huobiapi.github.io/docs/spot/v1/cn/
14+
type Response struct {
15+
Status string `json:"status"` // 接口返回状态
16+
Ch string `json:"ch"` // 接口数据对应的数据流。部分接口没有对应数据流因此不返回此字段
17+
Ts int64 `json:"ts"` // 返回的时间戳
18+
Data interface{} `json:"data"` // 数据主体
19+
}
20+
21+
type KLine struct {
22+
// TODO: 只使用了关注的字段
23+
Id int64 `json:"id"` // 调整为新加坡时间的时间戳,单位秒,并以此作为此K线柱的id
24+
Open float32 `json:"open"` // 开盘
25+
Close float32 `json:"close"` // 收盘价
26+
High float32 `json:"high"` // 最高
27+
Low float32 `json:"low"` // 最低
28+
}
29+
30+
var client *http.Client
31+
32+
type Huobi struct {
33+
Client *http.Client
34+
Proxy string
35+
Logger *ezlog.EzLogger
36+
}
37+
38+
func NewHuobi(proxy string) *Huobi {
39+
Proxy := func(_ *http.Request) (*url.URL, error) {
40+
return url.Parse(proxy)
41+
}
42+
Transport := &http.Transport{Proxy:Proxy}
43+
44+
huobi := &Huobi{
45+
Client: &http.Client{},
46+
Proxy: proxy,
47+
Logger: ezlog.New(os.Stdout, "", ezlog.BitDefault, ezlog.LogAll),
48+
}
49+
50+
if proxy != "" {
51+
huobi.Client.Transport = Transport
52+
}
53+
return huobi
54+
}
55+
56+
// btcusdt 60min 1
57+
func (h *Huobi) GetKLine(symbol, period string, size int) ([]*KLine, error) {
58+
req, _ := http.NewRequest("GET", "https://api.huobi.pro/market/history/kline", nil)
59+
q := req.URL.Query()
60+
q.Add("symbol", symbol)
61+
q.Add("period", period)
62+
q.Add("size", strconv.Itoa(size))
63+
req.URL.RawQuery = q.Encode()
64+
65+
resp, err := h.query(req)
66+
if err != nil {
67+
return nil, err
68+
}
69+
kline := make([]*KLine,0)
70+
js, err := json.Marshal(resp.Data)
71+
if err != nil {
72+
h.Logger.Debug(err)
73+
return nil, err
74+
}
75+
err = json.Unmarshal(js, &kline)
76+
if err != nil {
77+
h.Logger.Debug(err)
78+
return nil, err
79+
}
80+
return kline, nil
81+
}
82+
83+
func (h *Huobi) query(req *http.Request) (*Response, error) {
84+
resp, err := h.Client.Do(req)
85+
if err != nil {
86+
h.Logger.Debug(err, resp)
87+
return nil, err
88+
}
89+
var ret Response
90+
data, _ := ioutil.ReadAll(resp.Body)
91+
err = json.Unmarshal(data, &ret)
92+
if err != nil {
93+
h.Logger.Debug(err)
94+
return nil, err
95+
}
96+
return &ret, nil
97+
}

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
var Log = ezlog.New(os.Stdout, "", ezlog.BitDefault, ezlog.LogAll)
1616
var Bot *bot.Bot
1717
var API *btcinfo.BTC_com_api
18+
var HuobiAPI *Huobi
1819

1920
var configfile string
2021

@@ -26,6 +27,7 @@ func main() {
2627
util.Config.Reload(configfile)
2728

2829
API = btcinfo.NewBTC_com_api(util.Config.ApiHost, util.Config.CacheSize)
30+
HuobiAPI = NewHuobi(util.Config.Proxy)
2931

3032
for {
3133
bot, error := bot.NewBot(util.Config.BotToken, util.Config.Proxy)
@@ -52,6 +54,7 @@ func main() {
5254
Bot.Router.AddHandle("/q", q)
5355
Bot.Router.AddHandle("unknow", unknow)
5456
Bot.Router.AddHandle("/recent", recent)
57+
Bot.Router.AddHandle("/quotes", quotes)
5558

5659
LoadTemplate(util.Config.StaticPath)
5760
go doSignal()

static/address.tmp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
地址详情:
2+
-
13
`地址` : `{{.Address}}`
24
`总接收` : `{{.GetRecv}}BTC`
35
`总支出` : `{{.GetSent}}BTC`

static/block.tmp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
最新出块:
2+
-
13
`播报方`: [{{.Extras.Pool_name}}]({{.Extras.Pool_link}})
24
`块高度`: `{{.Height}}`
35
`块收益`: `{{.GetReword}}BTC`

static/help.tmp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
这里是帮助
1+
帮助:
2+
-
23
/newest - `最新出块`
34
/q - `查询: 块高度 块哈希 交易哈希 地址`
45
/recent - `查看最近出块情况`
5-
机器人地址 : [github](https://github.com/)
6+
/quotes - `1小时行情`
7+
机器人地址 : [github](https://github.com/zshorz/fkbro) @yesiare

static/quotes.tmp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1小时行情: `BTC/USDT`
2+
-
3+
`最高` : `{{.High}}`
4+
`当前` : *{{.Close}}*
5+
`开盘` : `{{.Open}}`
6+
`最低` : `{{.Low}}`

static/recent.tmp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
最近出块:
12
{{range .}}
23
`播报方` : [{{.Extras.Pool_name}}]({{.Extras.Pool_link}})
34
`时间` : `{{GetPastTime .Timestamp}}`

0 commit comments

Comments
 (0)