-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreply.go
137 lines (104 loc) · 2.71 KB
/
reply.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
package reply
import (
"context"
"fmt"
"sync"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
)
var onceCreate sync.Once
var replyMessages map[int]OnReplyCallback
var msgMu sync.RWMutex
type OnReplyCallback bot.HandlerFunc
type Reply struct {
bot *bot.Bot
allowWithoutReply bool
inputFieldPlaceholder string
parseMode models.ParseMode
}
func New(bot *bot.Bot) *Reply {
return &Reply{
bot: bot,
allowWithoutReply: false,
parseMode: models.ParseModeHTML,
}
}
func (r *Reply) AllowSendingWithoutReply() *Reply {
r.allowWithoutReply = true
return r
}
func (r *Reply) WithInputFieldPlaceholber(text string) *Reply {
r.inputFieldPlaceholder = text
return r
}
func (r *Reply) WithParseMode(mode models.ParseMode) *Reply {
r.parseMode = mode
return r
}
func (r *Reply) Send(toChat int64, messageText string, onReply OnReplyCallback) error {
if onReply == nil {
return fmt.Errorf("onReply must be set. Nil")
}
if r.bot == nil {
return fmt.Errorf("bot instance must be set. Nil")
}
onceCreate.Do(func() {
replyMessages = make(map[int]OnReplyCallback, 0)
r.bot.RegisterHandlerMatchFunc(func(update *models.Update) bool {
if update == nil || update.Message == nil || update.Message.ReplyToMessage == nil {
return false
}
msgMu.RLock()
_, ok := replyMessages[update.Message.ReplyToMessage.ID]
if !ok {
msgMu.RUnlock()
return false
}
msgMu.RUnlock()
return true
}, func(ctx context.Context, botClient *bot.Bot, update *models.Update) {
if update == nil || update.Message == nil {
return
}
msgMu.RLock()
replyCallback, ok := replyMessages[update.Message.ReplyToMessage.ID]
if !ok {
msgMu.RUnlock()
return
}
msgMu.RUnlock()
replyCallback(ctx, r.bot, update)
// delete reply messages
botClient.DeleteMessage(ctx, &bot.DeleteMessageParams{ // bot send
ChatID: update.Message.Chat.ID,
MessageID: update.Message.ReplyToMessage.ID,
})
botClient.DeleteMessage(ctx, &bot.DeleteMessageParams{ // user reply
ChatID: update.Message.Chat.ID,
MessageID: update.Message.ID,
})
msgMu.Lock()
delete(replyMessages, update.Message.ID)
msgMu.Unlock()
})
})
m, err := r.bot.SendMessage(context.Background(), &bot.SendMessageParams{
ChatID: toChat,
Text: messageText,
ParseMode: r.parseMode,
ReplyMarkup: &models.ForceReply{
ForceReply: true,
InputFieldPlaceholder: r.inputFieldPlaceholder,
},
ReplyParameters: &models.ReplyParameters{
AllowSendingWithoutReply: r.allowWithoutReply,
},
})
if err != nil {
return err
}
msgMu.Lock()
defer msgMu.Unlock()
replyMessages[m.ID] = onReply
return nil
}