-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtelegram.go
More file actions
190 lines (154 loc) · 5.12 KB
/
telegram.go
File metadata and controls
190 lines (154 loc) · 5.12 KB
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
package main
import (
"fmt"
"strings"
"time"
telegramBot "gopkg.in/tucnak/telebot.v2"
)
type TelegramReporter struct {
TelegramToken string
TelegramChat int
TelegramSetAliasCommand string
TelegramClearAliasCommand string
TelegramListAliasesCommand string
TelegramBot *telegramBot.Bot
HtmlSerializer Serializer
CacheManager *CacheManager
}
func (r TelegramReporter) Serialize(report Report) string {
var sb strings.Builder
sb.WriteString(report.Tx.Serialize(r.Serializer()) + "\n\n")
for _, msg := range report.Msgs {
sb.WriteString(msg.Serialize(r.Serializer()) + "\n\n")
}
return sb.String()
}
func (r *TelegramReporter) Init() {
if r.TelegramToken == "" || r.TelegramChat == 0 {
log.Debug().Msg("Telegram credentials not set, not creating Telegram reporter.")
return
}
bot, err := telegramBot.NewBot(telegramBot.Settings{
Token: TelegramToken,
Poller: &telegramBot.LongPoller{Timeout: 10 * time.Second},
})
if err != nil {
log.Warn().Err(err).Msg("Could not create Telegram bot")
return
}
r.TelegramBot = bot
r.HtmlSerializer = Serializer{
LinksSerializer: func(address string, text string) string {
return fmt.Sprintf(`<a href="%s">%s</a>`, address, text)
},
StrongSerializer: func(text string) string {
return fmt.Sprintf(`<strong>%s</strong>`, text)
},
CodeSerializer: func(text string) string {
return fmt.Sprintf(`<code>%s</code>`, text)
},
MultilineCodeSerializer: func(text string) string {
return fmt.Sprintf(`<pre>%s</pre>`, text)
},
CacheManager: r.CacheManager,
}
r.TelegramBot.Handle(r.TelegramSetAliasCommand, r.processSetAliasCommand)
r.TelegramBot.Handle(r.TelegramClearAliasCommand, r.processClearAliasCommand)
r.TelegramBot.Handle(r.TelegramListAliasesCommand, r.processListAliasesCommand)
go r.TelegramBot.Start()
}
func (reporter *TelegramReporter) processSetAliasCommand(message *telegramBot.Message) {
reporter.logQuery(message, reporter.TelegramSetAliasCommand)
text := fmt.Sprintf("Usage: `%s` <wallet-address> <alias>", reporter.TelegramSetAliasCommand)
args := strings.SplitN(message.Text, " ", 3)
if len(args) > 2 {
labelsConfigManager.setWalletLabel(args[1], args[2])
text = fmt.Sprintf(
"Successfully set alias for %s: %s",
reporter.HtmlSerializer.LinksSerializer(makeMintscanAccountLink(args[1]), args[1]),
reporter.HtmlSerializer.CodeSerializer(args[2]),
)
} else {
log.Info().Msg("/set-alias: args length <= 2")
}
if err := reporter.sendMessage(message, text); err != nil {
log.Error().Err(err).Msg("Could not send response to /set-alias command")
}
}
func (reporter *TelegramReporter) processClearAliasCommand(message *telegramBot.Message) {
reporter.logQuery(message, reporter.TelegramClearAliasCommand)
text := fmt.Sprintf("Usage: `%s` <wallet-address>", reporter.TelegramClearAliasCommand)
args := strings.SplitN(message.Text, " ", 2)
if len(args) >= 2 {
labelsConfigManager.clearWalletLabel(args[1])
text = fmt.Sprintf(
"Successfully cleared alias for %s",
reporter.HtmlSerializer.LinksSerializer(makeMintscanAccountLink(args[1]), args[1]),
)
} else {
log.Info().Msg("/clear-alias: args length < 2")
}
if err := reporter.sendMessage(message, text); err != nil {
log.Error().Err(err).Msg("Could not send response to /clear-alias command")
}
}
func (reporter *TelegramReporter) processListAliasesCommand(message *telegramBot.Message) {
reporter.logQuery(message, reporter.TelegramListAliasesCommand)
var sb strings.Builder
sb.WriteString(reporter.HtmlSerializer.StrongSerializer("Wallet aliases:") + "\n")
if len(labelsConfigManager.config.WalletLabels) == 0 {
sb.WriteString(fmt.Sprintf(
"No label aliases are set. You can set one using `%s` <wallet-address>",
reporter.TelegramSetAliasCommand,
))
}
for key, value := range labelsConfigManager.config.WalletLabels {
sb.WriteString(fmt.Sprintf(
"• %s: %s\n",
reporter.HtmlSerializer.LinksSerializer(makeMintscanAccountLink(key), key),
reporter.HtmlSerializer.CodeSerializer(value),
))
}
if err := reporter.sendMessage(message, sb.String()); err != nil {
log.Error().Err(err).Msg("Could not send response to /clear-alias command")
}
}
func (reporter *TelegramReporter) logQuery(message *telegramBot.Message, command string) {
log.Info().
Str("command", command).
Str("text", message.Text).
Str("user", message.Sender.Username).
Msg("Received command")
}
func (reporter *TelegramReporter) sendMessage(message *telegramBot.Message, text string) error {
_, err := reporter.TelegramBot.Send(
message.Chat,
text,
&telegramBot.SendOptions{
ParseMode: telegramBot.ModeHTML,
ReplyTo: message,
},
)
return err
}
func (r TelegramReporter) Enabled() bool {
return r.TelegramBot != nil
}
func (r TelegramReporter) Serializer() Serializer {
return r.HtmlSerializer
}
func (r TelegramReporter) SendReport(report Report) error {
serializedReport := r.Serialize(report)
_, err := r.TelegramBot.Send(
&telegramBot.User{
ID: r.TelegramChat,
},
serializedReport,
telegramBot.ModeHTML,
telegramBot.NoPreview,
)
return err
}
func (r TelegramReporter) Name() string {
return "TelegramReporter"
}