Skip to content

Commit

Permalink
Encapsulate error handling on bot send
Browse files Browse the repository at this point in the history
Closes #195
  • Loading branch information
LucaBernstein committed Nov 13, 2022
1 parent 7e6b45d commit d4221c9
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 364 deletions.
126 changes: 27 additions & 99 deletions bot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,14 @@ Reset your data stored by the bot. WARNING: This action is permanent!
bc.Logf(ERROR, m, "Parsing configHelp template failed: %s", err.Error())
}

_, err = bc.Bot.Send(Recipient(m), errorMsg+filledTemplate)
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, 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
_, err := bc.Bot.Send(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))
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, 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"))
Expand All @@ -98,32 +92,20 @@ func (bc *BotController) configHandleCurrency(m *tb.Message, params ...string) {
newCurrency := params[0]
err := bc.Repo.UserSetCurrency(m, newCurrency)
if err != nil {
_, err = bc.Bot.Send(Recipient(m), "An error ocurred saving your currency preference: "+err.Error())
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, Recipient(m), "An error ocurred saving your currency preference: "+err.Error())
return
}
_, err = bc.Bot.Send(Recipient(m), fmt.Sprintf("Changed default currency for all future transactions from '%s' to '%s'.", currency, newCurrency))
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, 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 != "" {
_, err := bc.Bot.Send(Recipient(m), fmt.Sprintf("All new transactions automatically get the tag #%s added (vacation mode enabled)", tag))
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, Recipient(m), fmt.Sprintf("All new transactions automatically get the tag #%s added (vacation mode enabled)", tag))
} else {
_, err := bc.Bot.Send(Recipient(m), "No tags are currently added to new transactions (vacation mode disabled).")
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, Recipient(m), "No tags are currently added to new transactions (vacation mode disabled).")
}
return
} else if len(params) > 1 { // Only 0 or 1 allowed
Expand All @@ -133,26 +115,17 @@ func (bc *BotController) configHandleTag(m *tb.Message, params ...string) {
if params[0] == "off" {
// DELETE tag
bc.Repo.UserSetTag(m, "")
_, err := bc.Bot.Send(Recipient(m), "Disabled automatically set tags on new transactions")
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, 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 {
_, err = bc.Bot.Send(Recipient(m), "An error ocurred saving the tag: "+err.Error())
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, Recipient(m), "An error ocurred saving the tag: "+err.Error())
return
}
_, err = bc.Bot.Send(Recipient(m), fmt.Sprintf("From now on all new transactions automatically get the tag #%s added (vacation mode enabled)", tag))
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, 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) {
Expand All @@ -171,20 +144,14 @@ func (bc *BotController) configHandleNotification(m *tb.Message, params ...strin
return
}
if daysDelay < 0 {
_, err = bc.Bot.Send(Recipient(m), "Notifications are disabled for open transactions.")
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, Recipient(m), "Notifications are disabled for open transactions.")
return
}
plural_s := "s"
if daysDelay == 1 {
plural_s = ""
}
_, err = bc.Bot.Send(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))
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, 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
Expand All @@ -194,10 +161,7 @@ func (bc *BotController) configHandleNotification(m *tb.Message, params ...strin
bc.configHelp(m, fmt.Errorf("error setting notification schedule: %s", err.Error()))
return
}
_, err = bc.Bot.Send(Recipient(m), "Successfully disabled notifications for open transactions.")
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, Recipient(m), "Successfully disabled notifications for open transactions.")
return
}
bc.configHelp(m, fmt.Errorf("invalid parameters"))
Expand Down Expand Up @@ -241,15 +205,12 @@ func (bc *BotController) configHandleAbout(m *tb.Message, params ...string) {
if version == "" {
version = "not specified"
}
_, err := bc.Bot.Send(Recipient(m), escapeCharacters(fmt.Sprintf(`Version information about [LucaBernstein/beancount-bot-tg](https://github.com/LucaBernstein/beancount-bot-tg)
bc.Bot.SendSilent(bc, 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)
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
}

func escapeCharacters(s string, c ...string) string {
Expand All @@ -262,10 +223,7 @@ func escapeCharacters(s string, c ...string) string {
func (bc *BotController) configHandleTimezoneOffset(m *tb.Message, params ...string) {
tz_offset := bc.Repo.UserGetTzOffset(m)
if len(params) == 0 { // 0 params: GET
_, err := bc.Bot.Send(Recipient(m), fmt.Sprintf("Your current timezone offset is set to 'UTC%s'.", prettyTzOffset(tz_offset)))
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, 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"))
Expand All @@ -275,45 +233,30 @@ func (bc *BotController) configHandleTimezoneOffset(m *tb.Message, params ...str
newTzOffset := params[0]
newTzParsed, err := strconv.Atoi(newTzOffset)
if err != nil {
_, err = bc.Bot.Send(Recipient(m), "An error ocurred saving your timezone offset preference: "+err.Error())
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, Recipient(m), "An error ocurred saving your timezone offset preference: "+err.Error())
return
}
err = bc.Repo.UserSetTzOffset(m, newTzParsed)
if err != nil {
_, err = bc.Bot.Send(Recipient(m), "An error ocurred saving your timezone offset preference: "+err.Error())
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, Recipient(m), "An error ocurred saving your timezone offset preference: "+err.Error())
return
}
_, err = bc.Bot.Send(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)))
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, 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) {
var err error
if len(params) == 0 { // 0 params: GET
exists, value, err := bc.Repo.GetUserSetting(helpers.USERSET_OMITCMDSLASH, m.Chat.ID)
if err != nil {
bc.Bot.Send(Recipient(m), "There has been an error internally while retrieving the value currently set for the queried user setting. Please try again later.")
bc.Bot.SendSilent(bc, 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" {
_, err = bc.Bot.Send(Recipient(m), "Omitting leading slash support is currently turned off. Please check the help on how to turn it on.")
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, Recipient(m), "Omitting leading slash support is currently turned off. Please check the help on how to turn it on.")
return
} else {
_, err = bc.Bot.Send(Recipient(m), "Omitting leading slash support is currently turned on.")
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, Recipient(m), "Omitting leading slash support is currently turned on.")
return
}
} else if len(params) > 1 { // 2 or more params: too many
Expand All @@ -326,24 +269,18 @@ func (bc *BotController) configHandleOmitLeadingSlash(m *tb.Message, params ...s
case "ON":
err = bc.Repo.SetUserSetting(helpers.USERSET_OMITCMDSLASH, "true", m.Chat.ID)
if err != nil {
bc.Bot.Send(Recipient(m), "There has been an error internally while setting your value. Please try again later.")
bc.Bot.SendSilent(bc, Recipient(m), "There has been an error internally while setting your value. Please try again later.")
return
}
_, err = bc.Bot.Send(Recipient(m), "Omitting leading slashes has successfully been turned on.")
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, Recipient(m), "Omitting leading slashes has successfully been turned on.")
return
case "OFF":
err = bc.Repo.SetUserSetting(helpers.USERSET_OMITCMDSLASH, "false", m.Chat.ID)
if err != nil {
bc.Bot.Send(Recipient(m), "There has been an error internally while setting your value. Please try again later.")
bc.Bot.SendSilent(bc, Recipient(m), "There has been an error internally while setting your value. Please try again later.")
return
}
_, err = bc.Bot.Send(Recipient(m), "Omitting leading slashes has successfully been turned off.")
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, Recipient(m), "Omitting leading slashes has successfully been turned off.")
return
default:
bc.configHelp(m, fmt.Errorf("invalid setting value: '%s'. Not in ['ON', 'OFF']", newUserSettingValue))
Expand All @@ -365,21 +302,12 @@ func (bc *BotController) configHandleAccountDelete(m *tb.Message, params ...stri

bc.deleteUserData(m)

_, err := bc.Bot.Send(Recipient(m), "I'm sad to see you go. Hopefully one day, you will come back.\n\nI have deleted all of your data stored in the bot. You can simply start over by sending me a message again. Goodbye.")
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
_, err = bc.Bot.Send(Recipient(m), "============")
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, Recipient(m), "I'm sad to see you go. Hopefully one day, you will come back.\n\nI have deleted all of your data stored in the bot. You can simply start over by sending me a message again. Goodbye.")
bc.Bot.SendSilent(bc, Recipient(m), "============")
return
}
bc.Logf(INFO, m, "Reset command failed 'yes' verification. Aborting.")
_, err := bc.Bot.Send(Recipient(m), "Reset has been aborted.\n\nYou tried to permanently delete your account. Please make sure to confirm this action by adding 'yes' to the end of your command. Please check /config for usage.")
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
bc.Bot.SendSilent(bc, Recipient(m), "Reset has been aborted.\n\nYou tried to permanently delete your account. Please make sure to confirm this action by adding 'yes' to the end of your command. Please check /config for usage.")
}

func (bc *BotController) deleteUserData(m *tb.Message) {
Expand Down
Loading

0 comments on commit d4221c9

Please sign in to comment.