forked from rodrigo-brito/ninjabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram.go
387 lines (332 loc) · 9.15 KB
/
telegram.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package notification
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
tb "gopkg.in/tucnak/telebot.v2"
"github.com/buugaaga/ninjabot/exchange"
"github.com/buugaaga/ninjabot/model"
"github.com/buugaaga/ninjabot/order"
"github.com/buugaaga/ninjabot/service"
)
var (
buyRegexp = regexp.MustCompile(`/buy\s+(?P<pair>\w+)\s+(?P<amount>\d+(?:\.\d+)?)(?P<percent>%)?`)
sellRegexp = regexp.MustCompile(`/sell\s+(?P<pair>\w+)\s+(?P<amount>\d+(?:\.\d+)?)(?P<percent>%)?`)
)
type telegram struct {
settings model.Settings
orderController *order.Controller
defaultMenu *tb.ReplyMarkup
client *tb.Bot
}
type Option func(telegram *telegram)
func NewTelegram(controller *order.Controller, settings model.Settings, options ...Option) (service.Telegram, error) {
menu := &tb.ReplyMarkup{ResizeReplyKeyboard: true}
poller := &tb.LongPoller{Timeout: 10 * time.Second}
userMiddleware := tb.NewMiddlewarePoller(poller, func(u *tb.Update) bool {
if u.Message == nil || u.Message.Sender == nil {
log.Error("no message, ", u)
return false
}
for _, user := range settings.Telegram.Users {
if int(u.Message.Sender.ID) == user {
return true
}
}
log.Error("invalid user, ", u.Message)
return false
})
client, err := tb.NewBot(tb.Settings{
ParseMode: tb.ModeMarkdown,
Token: settings.Telegram.Token,
Poller: userMiddleware,
})
if err != nil {
return nil, err
}
var (
statusBtn = menu.Text("/status")
profitBtn = menu.Text("/profit")
balanceBtn = menu.Text("/balance")
startBtn = menu.Text("/start")
stopBtn = menu.Text("/stop")
buyBtn = menu.Text("/buy")
sellBtn = menu.Text("/sell")
)
err = client.SetCommands([]tb.Command{
{Text: "/help", Description: "Display help instructions"},
{Text: "/stop", Description: "Stop buy and sell coins"},
{Text: "/start", Description: "Start buy and sell coins"},
{Text: "/status", Description: "Check bot status"},
{Text: "/balance", Description: "Wallet balance"},
{Text: "/profit", Description: "Summary of last trade results"},
{Text: "/buy", Description: "open a buy order"},
{Text: "/sell", Description: "open a sell order"},
})
if err != nil {
return nil, err
}
menu.Reply(
menu.Row(statusBtn, balanceBtn, profitBtn),
menu.Row(startBtn, stopBtn, buyBtn, sellBtn),
)
bot := &telegram{
orderController: controller,
client: client,
settings: settings,
defaultMenu: menu,
}
for _, option := range options {
option(bot)
}
client.Handle("/help", bot.HelpHandle)
client.Handle("/start", bot.StartHandle)
client.Handle("/stop", bot.StopHandle)
client.Handle("/status", bot.StatusHandle)
client.Handle("/balance", bot.BalanceHandle)
client.Handle("/profit", bot.ProfitHandle)
client.Handle("/buy", bot.BuyHandle)
client.Handle("/sell", bot.SellHandle)
return bot, nil
}
func (t telegram) Start() {
go t.client.Start()
for _, id := range t.settings.Telegram.Users {
_, err := t.client.Send(&tb.User{ID: int64(id)}, "Bot initialized.", t.defaultMenu)
if err != nil {
log.Error(err)
}
}
}
func (t telegram) Notify(text string) {
for _, user := range t.settings.Telegram.Users {
_, err := t.client.Send(&tb.User{ID: int64(user)}, text)
if err != nil {
log.Error(err)
}
}
}
func (t telegram) BalanceHandle(m *tb.Message) {
message := "*BALANCE*\n"
quotesValue := make(map[string]float64)
total := 0.0
account, err := t.orderController.Account()
if err != nil {
log.Error(err)
t.OnError(err)
return
}
for _, pair := range t.settings.Pairs {
assetPair, quotePair := exchange.SplitAssetQuote(pair)
assetBalance, quoteBalance := account.Balance(assetPair, quotePair)
assetSize := assetBalance.Free + assetBalance.Lock
quoteSize := quoteBalance.Free + quoteBalance.Lock
quote, err := t.orderController.LastQuote(pair)
if err != nil {
log.Error(err)
t.OnError(err)
return
}
assetValue := assetSize * quote
quotesValue[quotePair] = quoteSize
total += assetValue
message += fmt.Sprintf("%s: `%.4f` ≅ `%.2f` %s \n", assetPair, assetSize, assetValue, quotePair)
}
for quote, value := range quotesValue {
total += value
message += fmt.Sprintf("%s: `%.4f`\n", quote, value)
}
message += fmt.Sprintf("-----\nTotal: `%.4f`\n", total)
_, err = t.client.Send(m.Sender, message)
if err != nil {
log.Error(err)
}
}
func (t telegram) HelpHandle(m *tb.Message) {
commands, err := t.client.GetCommands()
if err != nil {
log.Error(err)
t.OnError(err)
return
}
lines := make([]string, 0, len(commands))
for _, command := range commands {
lines = append(lines, fmt.Sprintf("/%s - %s", command.Text, command.Description))
}
_, err = t.client.Send(m.Sender, strings.Join(lines, "\n"))
if err != nil {
log.Error(err)
}
}
func (t telegram) ProfitHandle(m *tb.Message) {
if len(t.orderController.Results) == 0 {
_, err := t.client.Send(m.Sender, "No trades registered.")
if err != nil {
log.Error(err)
}
return
}
for pair, summary := range t.orderController.Results {
_, err := t.client.Send(m.Sender, fmt.Sprintf("*PAIR*: `%s`\n`%s`", pair, summary.String()))
if err != nil {
log.Error(err)
}
}
}
func (t telegram) BuyHandle(m *tb.Message) {
match := buyRegexp.FindStringSubmatch(m.Text)
if len(match) == 0 {
_, err := t.client.Send(m.Sender, "Invalid command.\nExamples of usage:\n`/buy BTCUSDT 100`\n\n`/buy BTCUSDT 50%`")
if err != nil {
log.Error(err)
}
return
}
command := make(map[string]string)
for i, name := range buyRegexp.SubexpNames() {
if i != 0 && name != "" {
command[name] = match[i]
}
}
pair := strings.ToUpper(command["pair"])
amount, err := strconv.ParseFloat(command["amount"], 64)
if err != nil {
log.Error(err)
t.OnError(err)
return
} else if amount <= 0 {
_, err := t.client.Send(m.Sender, "Invalid amount")
if err != nil {
log.Error(err)
}
return
}
if command["percent"] != "" {
_, quote, err := t.orderController.Position(pair)
if err != nil {
log.Error(err)
t.OnError(err)
return
}
amount = amount * quote / 100.0
}
order, err := t.orderController.CreateOrderMarketQuote(model.SideTypeBuy, pair, amount)
if err != nil {
return
}
log.Info("[TELEGRAM]: BUY ORDER CREATED: ", order)
}
func (t telegram) SellHandle(m *tb.Message) {
match := sellRegexp.FindStringSubmatch(m.Text)
if len(match) == 0 {
_, err := t.client.Send(m.Sender, "Invalid command.\nExample of usage:\n`/sell BTCUSDT 100`\n\n`/sell BTCUSDT 50%")
if err != nil {
log.Error(err)
}
return
}
command := make(map[string]string)
for i, name := range sellRegexp.SubexpNames() {
if i != 0 && name != "" {
command[name] = match[i]
}
}
pair := strings.ToUpper(command["pair"])
amount, err := strconv.ParseFloat(command["amount"], 64)
if err != nil {
log.Error(err)
t.OnError(err)
return
} else if amount <= 0 {
_, err := t.client.Send(m.Sender, "Invalid amount")
if err != nil {
log.Error(err)
}
return
}
if command["percent"] != "" {
asset, _, err := t.orderController.Position(pair)
if err != nil {
return
}
amount = amount * asset / 100.0
order, err := t.orderController.CreateOrderMarket(model.SideTypeSell, pair, amount)
if err != nil {
return
}
log.Info("[TELEGRAM]: SELL ORDER CREATED: ", order)
return
}
order, err := t.orderController.CreateOrderMarketQuote(model.SideTypeSell, pair, amount)
if err != nil {
return
}
log.Info("[TELEGRAM]: SELL ORDER CREATED: ", order)
}
func (t telegram) StatusHandle(m *tb.Message) {
status := t.orderController.Status()
_, err := t.client.Send(m.Sender, fmt.Sprintf("Status: `%s`", status))
if err != nil {
log.Error(err)
}
}
func (t telegram) StartHandle(m *tb.Message) {
if t.orderController.Status() == order.StatusRunning {
_, err := t.client.Send(m.Sender, "Bot is already running.", t.defaultMenu)
if err != nil {
log.Error(err)
}
return
}
t.orderController.Start()
_, err := t.client.Send(m.Sender, "Bot started.", t.defaultMenu)
if err != nil {
log.Error(err)
}
}
func (t telegram) StopHandle(m *tb.Message) {
if t.orderController.Status() == order.StatusStopped {
_, err := t.client.Send(m.Sender, "Bot is already stopped.", t.defaultMenu)
if err != nil {
log.Error(err)
}
return
}
t.orderController.Stop()
_, err := t.client.Send(m.Sender, "Bot stopped.", t.defaultMenu)
if err != nil {
log.Error(err)
}
}
func (t telegram) OnOrder(order model.Order) {
title := ""
switch order.Status {
case model.OrderStatusTypeFilled:
title = fmt.Sprintf("✅ ORDER FILLED - %s", order.Pair)
case model.OrderStatusTypeNew:
title = fmt.Sprintf("🆕 NEW ORDER - %s", order.Pair)
case model.OrderStatusTypeCanceled, model.OrderStatusTypeRejected:
title = fmt.Sprintf("❌ ORDER CANCELED / REJECTED - %s", order.Pair)
}
message := fmt.Sprintf("%s\n-----\n%s", title, order)
t.Notify(message)
}
func (t telegram) OnError(err error) {
title := "🛑 ERROR"
var orderError *exchange.OrderError
if errors.As(err, &orderError) {
message := fmt.Sprintf(`%s
-----
Pair: %s
Quantity: %.4f
-----
%s`, title, orderError.Pair, orderError.Quantity, orderError.Err)
t.Notify(message)
return
}
t.Notify(fmt.Sprintf("%s\n-----\n%s", title, err))
}