-
Notifications
You must be signed in to change notification settings - Fork 44
/
tip.go
143 lines (122 loc) · 4.21 KB
/
tip.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
package telegram
import (
"context"
"fmt"
"strings"
"time"
"github.com/LightningTipBot/LightningTipBot/internal"
"github.com/LightningTipBot/LightningTipBot/internal/str"
"github.com/LightningTipBot/LightningTipBot/internal/i18n"
log "github.com/sirupsen/logrus"
tb "gopkg.in/lightningtipbot/telebot.v2"
)
func helpTipUsage(ctx context.Context, errormsg string) string {
if len(errormsg) > 0 {
return fmt.Sprintf(Translate(ctx, "tipHelpText"), fmt.Sprintf("%s", errormsg))
} else {
return fmt.Sprintf(Translate(ctx, "tipHelpText"), "")
}
}
func TipCheckSyntax(ctx context.Context, m *tb.Message) (bool, string) {
arguments := strings.Split(m.Text, " ")
if len(arguments) < 2 {
return false, Translate(ctx, "tipEnterAmountMessage")
}
return true, ""
}
func (bot *TipBot) tipHandler(ctx context.Context, m *tb.Message) {
// check and print all commands
bot.anyTextHandler(ctx, m)
user := LoadUser(ctx)
if user.Wallet == nil {
return
}
// only if message is a reply
if !m.IsReply() {
bot.tryDeleteMessage(m)
bot.trySendMessage(m.Sender, helpTipUsage(ctx, Translate(ctx, "tipDidYouReplyMessage")))
bot.trySendMessage(m.Sender, Translate(ctx, "tipInviteGroupMessage"))
return
}
if ok, err := TipCheckSyntax(ctx, m); !ok {
bot.trySendMessage(m.Sender, helpTipUsage(ctx, err))
NewMessage(m, WithDuration(0, bot))
return
}
// get tip amount
amount, err := decodeAmountFromCommand(m.Text)
if err != nil || amount < 1 {
errmsg := fmt.Sprintf("[/tip] Error: Tip amount not valid.")
// immediately delete if the amount is bullshit
NewMessage(m, WithDuration(0, bot))
bot.trySendMessage(m.Sender, helpTipUsage(ctx, Translate(ctx, "tipValidAmountMessage")))
log.Warnln(errmsg)
return
}
err = bot.parseCmdDonHandler(ctx, m)
if err == nil {
return
}
// TIP COMMAND IS VALID
from := LoadUser(ctx)
to := LoadReplyToUser(ctx)
if from.Telegram.ID == to.Telegram.ID {
NewMessage(m, WithDuration(0, bot))
bot.trySendMessage(m.Sender, Translate(ctx, "tipYourselfMessage"))
return
}
toUserStrMd := GetUserStrMd(to.Telegram)
fromUserStrMd := GetUserStrMd(from.Telegram)
toUserStr := GetUserStr(to.Telegram)
fromUserStr := GetUserStr(from.Telegram)
if _, exists := bot.UserExists(to.Telegram); !exists {
log.Infof("[/tip] User %s has no wallet.", toUserStr)
to, err = bot.CreateWalletForTelegramUser(to.Telegram)
if err != nil {
errmsg := fmt.Errorf("[/tip] Error: Could not create wallet for %s", toUserStr)
log.Errorln(errmsg)
return
}
}
// check for memo in command
tipMemo := ""
if len(strings.Split(m.Text, " ")) > 2 {
tipMemo = strings.SplitN(m.Text, " ", 3)[2]
if len(tipMemo) > 200 {
tipMemo = tipMemo[:200]
tipMemo = tipMemo + "..."
}
}
// todo: user new get username function to get userStrings
transactionMemo := fmt.Sprintf("Tip from %s to %s (%d sat).", fromUserStr, toUserStr, amount)
t := NewTransaction(bot, from, to, amount, TransactionType("tip"), TransactionChat(m.Chat))
t.Memo = transactionMemo
success, err := t.Send()
if !success {
NewMessage(m, WithDuration(0, bot))
bot.trySendMessage(m.Sender, Translate(ctx, "tipErrorMessage"))
errMsg := fmt.Sprintf("[/tip] Transaction failed: %s", err)
log.Warnln(errMsg)
return
}
// update tooltip if necessary
messageHasTip := tipTooltipHandler(m, bot, amount, to.Initialized)
log.Infof("[💸 tip] Tip from %s to %s (%d sat).", fromUserStr, toUserStr, amount)
// notify users
_, err = bot.Telegram.Send(from.Telegram, fmt.Sprintf(i18n.Translate(from.Telegram.LanguageCode, "tipSentMessage"), amount, toUserStrMd))
if err != nil {
errmsg := fmt.Errorf("[/tip] Error: Send message to %s: %s", toUserStr, err)
log.Warnln(errmsg)
}
// forward tipped message to user once
if !messageHasTip {
bot.tryForwardMessage(to.Telegram, m.ReplyTo, tb.Silent)
}
bot.trySendMessage(to.Telegram, fmt.Sprintf(i18n.Translate(to.Telegram.LanguageCode, "tipReceivedMessage"), fromUserStrMd, amount))
if len(tipMemo) > 0 {
bot.trySendMessage(to.Telegram, fmt.Sprintf("✉️ %s", str.MarkdownEscape(tipMemo)))
}
// delete the tip message after a few seconds, this is default behaviour
NewMessage(m, WithDuration(time.Second*time.Duration(internal.Configuration.Telegram.MessageDisposeDuration), bot))
return
}