-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathdonate.go
162 lines (144 loc) · 4.04 KB
/
donate.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
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"github.com/LightningTipBot/LightningTipBot/internal/lnbits"
log "github.com/sirupsen/logrus"
tb "gopkg.in/tucnak/telebot.v2"
)
// PLEASE DO NOT CHANGE THE CODE IN THIS FILE
// YOU MIGHT BREAK DONATIONS TO THE ORIGINAL PROJECT
// THE DEVELOPMENT OF LIGHTNINGTIPBOT RELIES ON DONATIONS
// IF YOU USE THIS PROJECT, LEAVE THIS CODE ALONE
var (
donationSuccess = "🙏 Thank you for your donation."
donationErrorMessage = "🚫 Oh no. Donation failed."
donationProgressMessage = "🧮 Preparing your donation..."
donationFailedMessage = "🚫 Donation failed: %s"
donateEnterAmountMessage = "Did you enter an amount?"
donateValidAmountMessage = "Did you enter a valid amount?"
donateHelpText = "📖 Oops, that didn't work. %s\n\n" +
"*Usage:* `/donate <amount>`\n" +
"*Example:* `/donate 1000`"
endpoint string
)
func helpDonateUsage(errormsg string) string {
if len(errormsg) > 0 {
return fmt.Sprintf(donateHelpText, fmt.Sprintf("%s", errormsg))
} else {
return fmt.Sprintf(donateHelpText, "")
}
}
func (bot TipBot) donationHandler(m *tb.Message) {
// check and print all commands
bot.anyTextHandler(m)
if len(strings.Split(m.Text, " ")) < 2 {
bot.trySendMessage(m.Sender, helpDonateUsage(donateEnterAmountMessage))
return
}
amount, err := decodeAmountFromCommand(m.Text)
if err != nil {
return
}
if amount < 1 {
bot.trySendMessage(m.Sender, helpDonateUsage(donateValidAmountMessage))
return
}
// command is valid
msg := bot.trySendMessage(m.Sender, donationProgressMessage)
// get invoice
resp, err := http.Get(fmt.Sprintf(endpoint, amount, GetUserStr(m.Sender), GetUserStr(bot.telegram.Me)))
if err != nil {
log.Errorln(err)
bot.tryEditMessage(msg, donationErrorMessage)
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Errorln(err)
bot.tryEditMessage(msg, donationErrorMessage)
return
}
// send donation invoice
user, err := GetUser(m.Sender, bot)
if err != nil {
return
}
// bot.trySendMessage(user.Telegram, string(body))
_, err = user.Wallet.Pay(lnbits.PaymentParams{Out: true, Bolt11: string(body)}, *user.Wallet)
if err != nil {
userStr := GetUserStr(m.Sender)
errmsg := fmt.Sprintf("[/donate] Donation failed for user %s: %s", userStr, err)
log.Errorln(errmsg)
bot.tryEditMessage(msg, fmt.Sprintf(donationFailedMessage, err))
return
}
bot.tryEditMessage(msg, donationSuccess)
}
func init() {
var sb strings.Builder
_, err := io.Copy(&sb, rot13Reader{strings.NewReader("uggcf://ya.gvcf/qbangr/%q?sebz=%f&obg=%f")})
if err != nil {
panic(err)
}
endpoint = sb.String()
}
type rot13Reader struct {
r io.Reader
}
func (rot13 rot13Reader) Read(b []byte) (int, error) {
n, err := rot13.r.Read(b)
for i := 0; i < n; i++ {
switch {
case b[i] >= 65 && b[i] <= 90:
if b[i] <= 77 {
b[i] = b[i] + 13
} else {
b[i] = b[i] - 13
}
case b[i] >= 97 && b[i] <= 122:
if b[i] <= 109 {
b[i] = b[i] + 13
} else {
b[i] = b[i] - 13
}
}
}
return n, err
}
func (bot TipBot) parseCmdDonHandler(m *tb.Message) error {
arg := ""
if strings.HasPrefix(strings.ToLower(m.Text), "/send") {
arg, _ = getArgumentFromCommand(m.Text, 2)
if arg != "@"+bot.telegram.Me.Username {
return fmt.Errorf("err")
}
}
if strings.HasPrefix(strings.ToLower(m.Text), "/tip") {
arg = GetUserStr(m.ReplyTo.Sender)
if arg != "@"+bot.telegram.Me.Username {
return fmt.Errorf("err")
}
}
if arg == "@LightningTipBot" || len(arg) < 1 {
return fmt.Errorf("err")
}
amount, err := decodeAmountFromCommand(m.Text)
if err != nil {
return err
}
var sb strings.Builder
_, err = io.Copy(&sb, rot13Reader{strings.NewReader("Gunax lbh! V'z ebhgvat guvf qbangvba gb [email protected].")})
if err != nil {
panic(err)
}
donationInterceptMessage := sb.String()
bot.trySendMessage(m.Sender, MarkdownEscape(donationInterceptMessage))
m.Text = fmt.Sprintf("/donate %d", amount)
bot.donationHandler(m)
// returning nil here will abort the parent handler (/pay or /tip)
return nil
}