-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathack.go
127 lines (116 loc) · 3.83 KB
/
ack.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
package ant
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"time"
bot "github.com/MixinNetwork/bot-api-go-client"
"github.com/shopspring/decimal"
)
const (
SubcribedUser = "subcriberd_user"
OceanWebsite = "https://mixcoin.one"
ExinWebsite = "https://exinone.com/#/exchange/flash/flashTakeOrder?uuid=%d"
)
var PairIndex = map[string]int{
"BTC/USDT": 15,
"ETH/USDT": 17,
"BCH/USDT": 16,
"EOS/USDT": 18,
"ETH/BTC": 19,
"BCH/BTC": 21,
"EOS/BTC": 20,
"XIN/BTC": 4,
"EOS/ETH": 22,
}
func (ant *Ant) OnMessage(ctx context.Context, msgView bot.MessageView, userId string) error {
if msgView.Category == bot.MessageCategoryPlainText {
data, err := base64.StdEncoding.DecodeString(msgView.Data)
if err != nil {
return err
}
log.Println("I got a message, it said: ", string(data))
switch strings.ToLower(string(data)) {
case "whoisyourdaddy":
assets, err := ReadAssets(ctx)
if err != nil {
return err
}
out := make(map[string]string, 0)
for asset, balance := range assets {
if amount, _ := strconv.ParseFloat(balance, 64); amount > 0.0 {
out[Who(asset)] = balance
}
}
bt, err := json.Marshal(out)
if err != nil {
return err
}
return ant.client.SendPlainText(ctx, msgView, string(bt))
case "sub":
if _, err := Redis(ctx).SAdd(SubcribedUser, msgView.UserId).Result(); err != nil {
log.Println("Add user err", err)
}
ant.client.SendPlainText(ctx, msgView, "Thanks for your attention.\n You may get a notification if you can benefit from the price differences below.")
ocean := bot.Button{Label: "Mixcoin", Action: OceanWebsite, Color: "#2e8b57"}
exin := bot.Button{Label: "ExinOne", Action: fmt.Sprintf(ExinWebsite, 15), Color: "#bc8f8f"}
return ant.client.SendAppButtons(ctx, msgView.ConversationId, msgView.UserId, ocean, exin)
case "unsub":
if _, err := Redis(ctx).SRem(SubcribedUser, msgView.UserId).Result(); err != nil {
return err
}
return ant.client.SendPlainText(ctx, msgView, "Goodbye! But I am sure you will come back soon.")
case "help", "??":
return ant.client.SendPlainText(ctx, msgView, "Too young too simple. No help message.")
default:
reply, err := Reply(string(data))
if err != nil {
return ant.client.SendPlainText(ctx, msgView, "I am busy!!! Stop disturbing me.")
}
return ant.client.SendPlainText(ctx, msgView, reply)
}
}
return nil
}
func (ant *Ant) Notice(ctx context.Context, event ProfitEvent) error {
users, err := Redis(ctx).SMembers(SubcribedUser).Result()
if err != nil {
return err
}
actions := map[string]string{
PageSideBid: " Buy in Mixcoin",
PageSideAsk: "Sell in Mixcoin",
}
template := "Go Go Go!\nAction: %-10s\nPair: %-10s\nPrice: %-10.8s\nAmount: %-10s\nProfit: %8s%%"
pair := Who(event.Base) + "/" + Who(event.Quote)
ocean := bot.Button{Label: "Mixcoin", Action: OceanWebsite, Color: "#2e8b57"}
exin := bot.Button{Label: "ExinOne", Action: fmt.Sprintf(ExinWebsite, PairIndex[pair]), Color: "#bc8f8f"}
msg := fmt.Sprintf(template, actions[event.Category], pair, event.Price.String(),
event.Amount.String(), event.Profit.Mul(decimal.NewFromFloat(100.0)).Round(2).String())
for _, user := range users {
msgView := bot.MessageView{
ConversationId: bot.UniqueConversationId(ClientId, user),
UserId: user,
}
if err := ant.client.SendPlainText(ctx, msgView, msg); err != nil {
log.Println("Send message error", err)
}
if err := ant.client.SendAppButtons(ctx, msgView.ConversationId, msgView.UserId, ocean, exin); err != nil {
log.Println("Trade error", err)
}
}
return nil
}
func (ant *Ant) PollMixinMessage(ctx context.Context) {
for {
ant.client = bot.NewBlazeClient(ClientId, SessionId, PrivateKey)
if err := ant.client.Loop(ctx, ant); err != nil {
log.Println(err)
}
time.Sleep(1 * time.Second)
}
}