-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathinline_send.go
296 lines (255 loc) · 8.7 KB
/
inline_send.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
package main
import (
"fmt"
"time"
"github.com/LightningTipBot/LightningTipBot/internal/runtime"
log "github.com/sirupsen/logrus"
tb "gopkg.in/tucnak/telebot.v2"
)
const (
inlineSendMessage = "Press ✅ to receive payment from %s.\n\n💸 Amount: %d sat"
inlineSendAppendMemo = "\n✉️ %s"
inlineSendUpdateMessageAccept = "💸 %d sat sent from %s to %s."
inlineSendCreateWalletMessage = "Chat with %s 👈 to manage your wallet."
sendYourselfMessage = "📖 You can't pay to yourself."
inlineSendFailedMessage = "🚫 Send failed."
inlineSendInvalidAmountMessage = "🚫 Amount must be larger than 0."
inlineSendBalanceLowMessage = "🚫 Your balance is too low (👑 %d sat)."
)
var (
inlineQuerySendTitle = "💸 Send payment to a chat."
inlineQuerySendDescription = "Usage: @%s send <amount> [<memo>]"
inlineResultSendTitle = "💸 Send %d sat."
inlineResultSendDescription = "👉 Click to send %d sat to this chat."
inlineSendMenu = &tb.ReplyMarkup{ResizeReplyKeyboard: true}
btnCancelInlineSend = inlineSendMenu.Data("🚫 Cancel", "cancel_send_inline")
btnAcceptInlineSend = inlineSendMenu.Data("✅ Receive", "confirm_send_inline")
)
type InlineSend struct {
Message string `json:"inline_send_message"`
Amount int `json:"inline_send_amount"`
From *tb.User `json:"inline_send_from"`
To *tb.User `json:"inline_send_to"`
Memo string `json:"inline_send_memo"`
ID string `json:"inline_send_id"`
Active bool `json:"inline_send_active"`
InTransaction bool `json:"inline_send_intransaction"`
}
func NewInlineSend() *InlineSend {
inlineSend := &InlineSend{
Message: "",
Active: true,
InTransaction: false,
}
return inlineSend
}
func (msg InlineSend) Key() string {
return msg.ID
}
func (bot *TipBot) LockSend(tx *InlineSend) error {
// immediatelly set intransaction to block duplicate calls
tx.InTransaction = true
err := bot.bunt.Set(tx)
if err != nil {
return err
}
return nil
}
func (bot *TipBot) ReleaseSend(tx *InlineSend) error {
// immediatelly set intransaction to block duplicate calls
tx.InTransaction = false
err := bot.bunt.Set(tx)
if err != nil {
return err
}
return nil
}
func (bot *TipBot) inactivateSend(tx *InlineSend) error {
tx.Active = false
err := bot.bunt.Set(tx)
if err != nil {
return err
}
return nil
}
func (bot *TipBot) getInlineSend(c *tb.Callback) (*InlineSend, error) {
inlineSend := NewInlineSend()
inlineSend.ID = c.Data
err := bot.bunt.Get(inlineSend)
// to avoid race conditions, we block the call if there is
// already an active transaction by loop until InTransaction is false
ticker := time.NewTicker(time.Second * 10)
for inlineSend.InTransaction {
select {
case <-ticker.C:
return nil, fmt.Errorf("inline send timeout")
default:
log.Infoln("in transaction")
time.Sleep(time.Duration(500) * time.Millisecond)
err = bot.bunt.Get(inlineSend)
}
}
if err != nil {
return nil, fmt.Errorf("could not get inline send message")
}
return inlineSend, nil
}
func (bot TipBot) handleInlineSendQuery(q *tb.Query) {
inlineSend := NewInlineSend()
var err error
inlineSend.Amount, err = decodeAmountFromCommand(q.Text)
if err != nil {
bot.inlineQueryReplyWithError(q, inlineQuerySendTitle, fmt.Sprintf(inlineQuerySendDescription, bot.telegram.Me.Username))
return
}
if inlineSend.Amount < 1 {
bot.inlineQueryReplyWithError(q, inlineSendInvalidAmountMessage, fmt.Sprintf(inlineQuerySendDescription, bot.telegram.Me.Username))
return
}
fromUserStr := GetUserStr(&q.From)
balance, err := bot.GetUserBalance(&q.From)
if err != nil {
errmsg := fmt.Sprintf("could not get balance of user %s", fromUserStr)
log.Errorln(errmsg)
return
}
// check if fromUser has balance
if balance < inlineSend.Amount {
log.Errorln("Balance of user %s too low", fromUserStr)
bot.inlineQueryReplyWithError(q, fmt.Sprintf(inlineSendBalanceLowMessage, balance), fmt.Sprintf(inlineQuerySendDescription, bot.telegram.Me.Username))
return
}
// check for memo in command
inlineSend.Memo = GetMemoFromCommand(q.Text, 2)
urls := []string{
queryImage,
}
results := make(tb.Results, len(urls)) // []tb.Result
for i, url := range urls {
inlineMessage := fmt.Sprintf(inlineSendMessage, fromUserStr, inlineSend.Amount)
if len(inlineSend.Memo) > 0 {
inlineMessage = inlineMessage + fmt.Sprintf(inlineSendAppendMemo, inlineSend.Memo)
}
result := &tb.ArticleResult{
// URL: url,
Text: inlineMessage,
Title: fmt.Sprintf(inlineResultSendTitle, inlineSend.Amount),
Description: fmt.Sprintf(inlineResultSendDescription, inlineSend.Amount),
// required for photos
ThumbURL: url,
}
id := fmt.Sprintf("inl-send-%d-%d-%s", q.From.ID, inlineSend.Amount, RandStringRunes(5))
btnAcceptInlineSend.Data = id
btnCancelInlineSend.Data = id
inlineSendMenu.Inline(inlineSendMenu.Row(btnAcceptInlineSend, btnCancelInlineSend))
result.ReplyMarkup = &tb.InlineKeyboardMarkup{InlineKeyboard: inlineSendMenu.InlineKeyboard}
results[i] = result
// needed to set a unique string ID for each result
results[i].SetResultID(id)
// add data to persistent object
inlineSend.Message = inlineMessage
inlineSend.ID = id
inlineSend.From = &q.From
// add result to persistent struct
runtime.IgnoreError(bot.bunt.Set(inlineSend))
}
err = bot.telegram.Answer(q, &tb.QueryResponse{
Results: results,
CacheTime: 1, // 60 == 1 minute, todo: make higher than 1 s in production
})
if err != nil {
log.Errorln(err)
}
}
func (bot *TipBot) acceptInlineSendHandler(c *tb.Callback) {
inlineSend, err := bot.getInlineSend(c)
if err != nil {
log.Errorf("[acceptInlineSendHandler] %s", err)
return
}
// immediatelly set intransaction to block duplicate calls
err = bot.LockSend(inlineSend)
if err != nil {
log.Errorf("[getInlineSend] %s", err)
return
}
if !inlineSend.Active {
log.Errorf("[acceptInlineSendHandler] inline send not active anymore")
return
}
defer bot.ReleaseSend(inlineSend)
amount := inlineSend.Amount
to := c.Sender
from := inlineSend.From
inlineSend.To = to
if from.ID == to.ID {
bot.trySendMessage(from, sendYourselfMessage)
return
}
toUserStrMd := GetUserStrMd(to)
fromUserStrMd := GetUserStrMd(from)
toUserStr := GetUserStr(to)
fromUserStr := GetUserStr(from)
// check if user exists and create a wallet if not
_, exists := bot.UserExists(to)
if !exists {
log.Infof("[sendInline] User %s has no wallet.", toUserStr)
err = bot.CreateWalletForTelegramUser(to)
if err != nil {
errmsg := fmt.Errorf("[sendInline] Error: Could not create wallet for %s", toUserStr)
log.Errorln(errmsg)
return
}
}
// set inactive to avoid double-sends
bot.inactivateSend(inlineSend)
// todo: user new get username function to get userStrings
transactionMemo := fmt.Sprintf("Send from %s to %s (%d sat).", fromUserStr, toUserStr, amount)
t := NewTransaction(bot, from, to, amount, TransactionType("inline send"))
t.Memo = transactionMemo
success, err := t.Send()
if !success {
if err != nil {
bot.trySendMessage(from, fmt.Sprintf(tipErrorMessage, err))
} else {
bot.trySendMessage(from, fmt.Sprintf(tipErrorMessage, tipUndefinedErrorMsg))
}
errMsg := fmt.Sprintf("[sendInline] Transaction failed: %s", err)
log.Errorln(errMsg)
bot.tryEditMessage(c.Message, inlineSendFailedMessage, &tb.ReplyMarkup{})
return
}
log.Infof("[sendInline] %d sat from %s to %s", amount, fromUserStr, toUserStr)
inlineSend.Message = fmt.Sprintf("%s", fmt.Sprintf(inlineSendUpdateMessageAccept, amount, fromUserStrMd, toUserStrMd))
memo := inlineSend.Memo
if len(memo) > 0 {
inlineSend.Message = inlineSend.Message + fmt.Sprintf(inlineSendAppendMemo, memo)
}
if !bot.UserInitializedWallet(to) {
inlineSend.Message += "\n\n" + fmt.Sprintf(inlineSendCreateWalletMessage, GetUserStrMd(bot.telegram.Me))
}
bot.tryEditMessage(c.Message, inlineSend.Message, &tb.ReplyMarkup{})
// notify users
_, err = bot.telegram.Send(to, fmt.Sprintf(sendReceivedMessage, fromUserStrMd, amount))
_, err = bot.telegram.Send(from, fmt.Sprintf(tipSentMessage, amount, toUserStrMd))
if err != nil {
errmsg := fmt.Errorf("[sendInline] Error: Send message to %s: %s", toUserStr, err)
log.Errorln(errmsg)
return
}
}
func (bot *TipBot) cancelInlineSendHandler(c *tb.Callback) {
inlineSend, err := bot.getInlineSend(c)
if err != nil {
log.Errorf("[cancelInlineSendHandler] %s", err)
return
}
if c.Sender.ID == inlineSend.From.ID {
bot.tryEditMessage(c.Message, sendCancelledMessage, &tb.ReplyMarkup{})
// set the inlineSend inactive
inlineSend.Active = false
inlineSend.InTransaction = false
runtime.IgnoreError(bot.bunt.Set(inlineSend))
}
return
}