/
config.go
338 lines (303 loc) · 12 KB
/
config.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package bot
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/LucaBernstein/beancount-bot-tg/db/crud"
"github.com/LucaBernstein/beancount-bot-tg/helpers"
tb "gopkg.in/telebot.v3"
)
func (bc *BotController) configHandler(m *tb.Message) {
sc := helpers.MakeSubcommandHandler("/"+CMD_CONFIG, true)
sc.
Add("currency", bc.configHandleCurrency).
Add("tag", bc.configHandleTag).
Add("notify", bc.configHandleNotification).
Add("about", bc.configHandleAbout).
Add("tz_offset", bc.configHandleTimezoneOffset).
Add("delete_account", bc.configHandleAccountDelete).
Add("omit_slash", bc.configHandleOmitLeadingSlash).
Add("enable_api", bc.configHandleEnableApi)
_, err := sc.Handle(m)
if err != nil {
bc.configHelp(m, nil)
}
}
func (bc *BotController) configHelp(m *tb.Message, err error) {
errorMsg := ""
if err != nil {
errorMsg += fmt.Sprintf("Error executing your command: %s\n\n", err.Error())
}
tz, _ := time.Now().Zone()
filledTemplate, err := helpers.Template(`Usage help for /{{.CONFIG_COMMAND}}:
/{{.CONFIG_COMMAND}} currency <c> - Change default currency
Tags will be added to each new transaction with a '#':
/{{.CONFIG_COMMAND}} tag - Get currently set tag
/{{.CONFIG_COMMAND}} tag off - Turn off tag
/{{.CONFIG_COMMAND}} tag <name> - Set tag to apply to new transactions, e.g. when on vacation
Create a schedule to be notified of open transactions (i.e. not archived or deleted):
/{{.CONFIG_COMMAND}} notify - Get current notification status
/{{.CONFIG_COMMAND}} notify off - Disable reminder notifications
/{{.CONFIG_COMMAND}} notify <delay> <hour> - Notify of open transaction after <delay> days at <hour> of the day. Honors configured timezone offset (see below)
Timezone offset from {{.TZ}} to honor for notifications and current date (if set automatically) in new transactions:
/{{.CONFIG_COMMAND}} tz_offset - Get current timezone offset from {{.TZ}} (default 0)
/{{.CONFIG_COMMAND}} tz_offset <hours> - Set timezone offset from {{.TZ}}
Feature toggle: Also activate commands without leading slash if not in transaction
/{{.CONFIG_COMMAND}} omit_slash - Get current setting value
/{{.CONFIG_COMMAND}} omit_slash on|off - Enable or disable omitted leading slash support
Feature toggle: Activate API / UI usage
/{{.CONFIG_COMMAND}} enable_api - Get current setting value
/{{.CONFIG_COMMAND}} enable_api on|off
Additional information about this bot
/{{.CONFIG_COMMAND}} about - Display the version this bot is running on
Reset your data stored by the bot. WARNING: This action is permanent!
/{{.CONFIG_COMMAND}} delete_account yes - Permanently delete all account-related data
`, map[string]interface{}{
"CONFIG_COMMAND": CMD_CONFIG,
"TZ": tz,
})
if err != nil {
bc.Logf(ERROR, m, "Parsing configHelp template failed: %s", err.Error())
}
bc.Bot.SendSilent(bc.Logf, Recipient(m), errorMsg+filledTemplate)
}
func (bc *BotController) configHandleCurrency(m *tb.Message, params ...string) {
currency := bc.Repo.UserGetCurrency(m)
if len(params) == 0 { // 0 params: GET currency
// Return currently set currency
bc.Bot.SendSilent(bc.Logf, Recipient(m), fmt.Sprintf("Your current currency is set to '%s'. To change it add the new currency to use to the command like this: '/%s currency EUR'.", currency, CMD_CONFIG))
return
} else if len(params) > 1 { // 2 or more params: too many
bc.configHelp(m, fmt.Errorf("invalid amount of parameters specified"))
return
}
// Set new currency
newCurrency := params[0]
err := bc.Repo.UserSetCurrency(m, newCurrency)
if err != nil {
bc.Bot.SendSilent(bc.Logf, Recipient(m), "An error ocurred saving your currency preference: "+err.Error())
return
}
bc.Bot.SendSilent(bc.Logf, Recipient(m), fmt.Sprintf("Changed default currency for all future transactions from '%s' to '%s'.", currency, newCurrency))
}
func (bc *BotController) configHandleTag(m *tb.Message, params ...string) {
if len(params) == 0 {
// GET tag
tag := bc.Repo.UserGetTag(m)
if tag != "" {
bc.Bot.SendSilent(bc.Logf, Recipient(m), fmt.Sprintf("All new transactions automatically get the tag #%s added (vacation mode enabled)", tag))
} else {
bc.Bot.SendSilent(bc.Logf, Recipient(m), "No tags are currently added to new transactions (vacation mode disabled).")
}
return
} else if len(params) > 1 { // Only 0 or 1 allowed
bc.configHelp(m, fmt.Errorf("invalid amount of parameters specified"))
return
}
if params[0] == "off" {
// DELETE tag
bc.Repo.UserSetTag(m, "")
bc.Bot.SendSilent(bc.Logf, Recipient(m), "Disabled automatically set tags on new transactions")
return
}
// SET tag
tag := strings.TrimPrefix(params[0], "#")
err := bc.Repo.UserSetTag(m, tag)
if err != nil {
bc.Bot.SendSilent(bc.Logf, Recipient(m), "An error ocurred saving the tag: "+err.Error())
return
}
bc.Bot.SendSilent(bc.Logf, Recipient(m), fmt.Sprintf("From now on all new transactions automatically get the tag #%s added (vacation mode enabled)", tag))
}
func (bc *BotController) configHandleNotification(m *tb.Message, params ...string) {
var tz, _ = time.Now().Zone()
userTzOffset := bc.Repo.UserGetTzOffset(m)
if userTzOffset < 0 {
tz += strconv.Itoa(userTzOffset)
} else {
tz += "+" + strconv.Itoa(userTzOffset)
}
if len(params) == 0 {
// GET schedule
daysDelay, hour, err := bc.Repo.UserGetNotificationSetting(m)
if err != nil {
bc.configHelp(m, fmt.Errorf("an application error occurred while retrieving user information from database"))
return
}
if daysDelay < 0 {
bc.Bot.SendSilent(bc.Logf, Recipient(m), "Notifications are disabled for open transactions.")
return
}
plural_s := "s"
if daysDelay == 1 {
plural_s = ""
}
bc.Bot.SendSilent(bc.Logf, Recipient(m), fmt.Sprintf("The bot will notify you daily at hour %d (%s) if transactions are open for more than %d day%s", hour, tz, daysDelay, plural_s))
return
} else if len(params) == 1 {
// DELETE schedule
if params[0] == "off" {
err := bc.Repo.UserSetNotificationSetting(m, -1, -1)
if err != nil {
bc.configHelp(m, fmt.Errorf("error setting notification schedule: %s", err.Error()))
return
}
bc.Bot.SendSilent(bc.Logf, Recipient(m), "Successfully disabled notifications for open transactions.")
return
}
bc.configHelp(m, fmt.Errorf("invalid parameters"))
return
} else if len(params) == 2 {
// SET schedule
daysDelay, err := strconv.Atoi(params[0])
if err != nil {
bc.configHelp(m, fmt.Errorf("error converting daysDelay to number: %s: %s", params[0], err.Error()))
return
}
hour, err := strconv.Atoi(params[1])
if err != nil {
bc.configHelp(m, fmt.Errorf("error converting hour to number: %s: %s", params[1], err.Error()))
return
}
if hour > 23 || hour < 0 {
bc.configHelp(m, fmt.Errorf("invalid hour (%d is out of valid range 1-23)", hour))
return
}
err = bc.Repo.UserSetNotificationSetting(m, daysDelay, hour)
if err != nil {
bc.configHelp(m, fmt.Errorf("error setting notification schedule: %s", err.Error()))
}
bc.configHandleNotification(m) // Recursively call with zero params --> GET
return
}
bc.configHelp(m, fmt.Errorf("invalid amount of parameters specified"))
}
func (bc *BotController) configHandleAbout(m *tb.Message, params ...string) {
if len(params) > 0 {
bc.configHelp(m, fmt.Errorf("no parameters expected"))
return
}
version := os.Getenv("VERSION")
versionLink := "https://github.com/LucaBernstein/beancount-bot-tg/releases/"
if strings.HasPrefix(version, "v") {
versionLink += "tag/" + version
}
if version == "" {
version = "not specified"
}
bc.Bot.SendSilent(bc.Logf, Recipient(m), escapeCharacters(fmt.Sprintf(`Version information about [LucaBernstein/beancount-bot-tg](https://github.com/LucaBernstein/beancount-bot-tg)
Version: [%s](%s)`,
version,
versionLink,
), ".", "-"), tb.ModeMarkdownV2)
}
func escapeCharacters(s string, c ...string) string {
for _, char := range c {
s = strings.ReplaceAll(s, char, "\\"+char)
}
return s
}
func (bc *BotController) configHandleTimezoneOffset(m *tb.Message, params ...string) {
tz_offset := bc.Repo.UserGetTzOffset(m)
if len(params) == 0 { // 0 params: GET
bc.Bot.SendSilent(bc.Logf, Recipient(m), fmt.Sprintf("Your current timezone offset is set to 'UTC%s'.", prettyTzOffset(tz_offset)))
return
} else if len(params) > 1 { // 2 or more params: too many
bc.configHelp(m, fmt.Errorf("invalid amount of parameters specified"))
return
}
// Set new tz_offset
newTzOffset := params[0]
newTzParsed, err := strconv.Atoi(newTzOffset)
if err != nil {
bc.Bot.SendSilent(bc.Logf, Recipient(m), "An error ocurred saving your timezone offset preference: "+err.Error())
return
}
err = bc.Repo.UserSetTzOffset(m, newTzParsed)
if err != nil {
bc.Bot.SendSilent(bc.Logf, Recipient(m), "An error ocurred saving your timezone offset preference: "+err.Error())
return
}
bc.Bot.SendSilent(bc.Logf, Recipient(m), fmt.Sprintf("Changed timezone offset for default dates for all future transactions from 'UTC%s' to 'UTC%s'.", prettyTzOffset(tz_offset), prettyTzOffset(newTzParsed)))
}
func (bc *BotController) configHandleOmitLeadingSlash(m *tb.Message, params ...string) {
bc.configHandleBooleanFeature(m, helpers.USERSET_OMITCMDSLASH, "Omitting leading slash support", params...)
}
func (bc *BotController) configHandleEnableApi(m *tb.Message, params ...string) {
state := bc.configHandleBooleanFeature(m, helpers.USERSET_ENABLEAPI, "API support", params...)
if state {
var chatInformation string
if crud.IsGroupChat(m) {
chatInformation = "group chat"
} else {
chatInformation = "direct chat"
}
bc.Bot.SendSilent(bc.Logf, Recipient(m), fmt.Sprintf("Your chat ID to use for token verification: %d (%s with bot, make one request at a time, multiple tokens can be generated sequentially)", m.Chat.ID, chatInformation))
}
}
func (bc *BotController) configHandleBooleanFeature(m *tb.Message, key string, name string, params ...string) (state bool) {
var err error
if len(params) == 0 { // 0 params: GET
exists, value, err := bc.Repo.GetUserSetting(key, m.Chat.ID)
if err != nil {
bc.Bot.SendSilent(bc.Logf, Recipient(m), "There has been an error internally while retrieving the value currently set for the queried user setting. Please try again later.")
return
}
if !exists || strings.ToUpper(value) != "TRUE" {
bc.Bot.SendSilent(bc.Logf, Recipient(m), fmt.Sprintf("%s is currently turned off. Please check the help on how to turn it on.", name))
return
} else {
bc.Bot.SendSilent(bc.Logf, Recipient(m), fmt.Sprintf("%s is currently turned on.", name))
return true
}
} else if len(params) > 1 { // 2 or more params: too many
bc.configHelp(m, fmt.Errorf("invalid amount of parameters specified"))
return
}
// Set user setting
newUserSettingValue := params[0]
switch strings.ToUpper(newUserSettingValue) {
case "ON":
err = bc.Repo.SetUserSetting(key, "true", m.Chat.ID)
if err != nil {
bc.Bot.SendSilent(bc.Logf, Recipient(m), "There has been an error internally while setting your value. Please try again later.")
return
}
bc.Bot.SendSilent(bc.Logf, Recipient(m), fmt.Sprintf("%s has successfully been turned on.", name))
return true
case "OFF":
err = bc.Repo.SetUserSetting(key, "false", m.Chat.ID)
if err != nil {
bc.Bot.SendSilent(bc.Logf, Recipient(m), "There has been an error internally while setting your value. Please try again later.")
return
}
bc.Bot.SendSilent(bc.Logf, Recipient(m), fmt.Sprintf("%s has successfully been turned off.", name))
return
default:
bc.configHelp(m, fmt.Errorf("invalid setting value: '%s'. Not in ['ON', 'OFF']", newUserSettingValue))
return
}
}
func prettyTzOffset(tzOffset int) string {
if tzOffset < 0 {
return strconv.Itoa(tzOffset)
}
return "+" + strconv.Itoa(tzOffset)
}
type errors struct {
operation string
m *tb.Message
bc *BotController
}
func (e *errors) handle1(err error) {
if err != nil {
e.bc.Logf(ERROR, e.m, "Handling error for operation '%s' (failing silently, proceeding): %s", e.operation, err.Error())
}
}
func (e *errors) handle2(_ interface{}, err error) {
if err != nil {
e.bc.Logf(ERROR, e.m, "Handling error for operation '%s' (failing silently, proceeding): %s", e.operation, err.Error())
}
}