-
Notifications
You must be signed in to change notification settings - Fork 0
/
i_loss_ratio.go
92 lines (79 loc) · 1.91 KB
/
i_loss_ratio.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
package main
import (
"context"
"log"
"trade_calc_bot/utils"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
)
type InputLossRatio struct {
text string
callbackHandlerID string
}
func NewInputLossRatio() *InputLossRatio {
return &InputLossRatio{
text: "请输入止损比例(单位: %), 例如: 0.38",
}
}
func (m *InputLossRatio) SendMessage(ctx context.Context, b *bot.Bot, chatId int64) error {
_, err := b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: chatId,
Text: m.text,
})
return err
}
func (m *InputLossRatio) Show(ctx context.Context, b *bot.Bot, chatID int64) error {
// 注册消息回调函数
m.callbackHandlerID = b.RegisterHandlerMatchFunc(
func(update *models.Update) bool {
return true
},
m.callback,
)
_, err := b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: chatID,
Text: m.text,
ReplyMarkup: &models.ForceReply{
ForceReply: true,
},
})
return err
}
func (m *InputLossRatio) callback(ctx context.Context, b *bot.Bot, update *models.Update) {
task := GetTaskManager().GetTask(update.Message.Chat.ID)
if task == nil {
return
}
lossRatio, err := utils.ParseFloat(update.Message.Text)
if err != nil {
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Text: "请输入有效的数字",
})
return
}
task.Payload.SetLossRatio(lossRatio / 100)
// 删除用户的回复消息
_, err = b.DeleteMessages(ctx, &bot.DeleteMessagesParams{
ChatID: update.Message.Chat.ID,
MessageIDs: []int{update.Message.ID, update.Message.ReplyToMessage.ID},
})
if err != nil {
m.onError(err)
return
}
// 返回上一级菜单
_, err = NewOpenPositionMenu().Show(
ctx,
b,
update.Message.Chat.ID,
)
if err != nil {
m.onError(err)
return
}
b.UnregisterHandler(m.callbackHandlerID)
}
func (m *InputLossRatio) onError(err error) {
log.Println("[InputLossRatio] [ERROR] Error: ", err)
}