Skip to content

Commit

Permalink
Make bot work in group chats (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaBernstein authored Jul 27, 2022
1 parent 50cd867 commit fc9b398
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 80 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Supports you in keeping track of your beancount transactions also if you need to
* [x] Many optional commands, shorthands and parameters, leaving the full flexibility up to you
* [x] Automatically apply tags to transactions, e.g. when on vacation
* [x] Auto-format amount decimal point alignment to match [VSCode Beancount plugin](https://marketplace.visualstudio.com/items?itemName=Lencerf.beancount)
* [x] Bot works in group chat (required to disable [privacy mode](https://core.telegram.org/bots#privacy-mode) with BotFather)

Check out `/help` in the bot for all available commands and don't forget to configure your bot with `/config`. Just give it a try.

Expand Down
48 changes: 24 additions & 24 deletions bot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ 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(m.Sender, errorMsg+filledTemplate)
_, err = bc.Bot.Send(Recipient(m), errorMsg+filledTemplate)
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
Expand All @@ -82,7 +82,7 @@ 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(m.Sender, 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))
_, 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())
}
Expand All @@ -95,13 +95,13 @@ 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(m.Sender, "An error ocurred saving your currency preference: "+err.Error())
_, 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())
}
return
}
_, err = bc.Bot.Send(m.Sender, fmt.Sprintf("Changed default currency for all future transactions from '%s' to '%s'.", currency, newCurrency))
_, 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())
}
Expand All @@ -112,12 +112,12 @@ func (bc *BotController) configHandleTag(m *tb.Message, params ...string) {
// GET tag
tag := bc.Repo.UserGetTag(m)
if tag != "" {
_, err := bc.Bot.Send(m.Sender, fmt.Sprintf("All new transactions automatically get the tag #%s added (vacation mode enabled)", 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())
}
} else {
_, err := bc.Bot.Send(m.Sender, "No tags are currently added to new transactions (vacation mode disabled).")
_, 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())
}
Expand All @@ -130,7 +130,7 @@ func (bc *BotController) configHandleTag(m *tb.Message, params ...string) {
if params[0] == "off" {
// DELETE tag
bc.Repo.UserSetTag(m, "")
_, err := bc.Bot.Send(m.Sender, "Disabled automatically set tags on new transactions")
_, 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())
}
Expand All @@ -140,13 +140,13 @@ func (bc *BotController) configHandleTag(m *tb.Message, params ...string) {
tag := strings.TrimPrefix(params[0], "#")
err := bc.Repo.UserSetTag(m, tag)
if err != nil {
_, err = bc.Bot.Send(m.Sender, "An error ocurred saving the tag: "+err.Error())
_, 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())
}
return
}
_, err = bc.Bot.Send(m.Sender, fmt.Sprintf("From now on all new transactions automatically get the tag #%s added (vacation mode enabled)", tag))
_, 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())
}
Expand All @@ -168,7 +168,7 @@ func (bc *BotController) configHandleNotification(m *tb.Message, params ...strin
return
}
if daysDelay < 0 {
_, err = bc.Bot.Send(m.Sender, "Notifications are disabled for open transactions.")
_, 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())
}
Expand All @@ -178,7 +178,7 @@ func (bc *BotController) configHandleNotification(m *tb.Message, params ...strin
if daysDelay == 1 {
plural_s = ""
}
_, err = bc.Bot.Send(m.Sender, 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))
_, 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())
}
Expand All @@ -191,7 +191,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(m.Sender, "Successfully disabled notifications for open transactions.")
_, 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())
}
Expand Down Expand Up @@ -240,7 +240,7 @@ func (bc *BotController) configHandleLimit(m *tb.Message, params ...string) {
}
message += "\n\n"
message += "If new cache entries are created for the given types, old ones are automatically deleted.\nPlease note: If suggestions were added using /suggestions add, they will not be deleted automatically by this mechanism. '-1' means no limit."
_, err = bc.Bot.Send(m.Sender, message)
_, err = bc.Bot.Send(Recipient(m), message)
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
Expand All @@ -257,7 +257,7 @@ func (bc *BotController) configHandleLimit(m *tb.Message, params ...string) {
message := "You have the following cache limit configured for '" + cacheType + "': " + strconv.Itoa(cacheLimits[cacheType])
message += "\n\n"
message += "If new cache entries are created for the given types, old ones are automatically deleted.\nPlease note: If suggestions were added using /suggestions add, they will not be deleted automatically by this mechanism. '-1' means no limit."
_, err = bc.Bot.Send(m.Sender, message)
_, err = bc.Bot.Send(Recipient(m), message)
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
Expand Down Expand Up @@ -285,7 +285,7 @@ func (bc *BotController) configHandleLimit(m *tb.Message, params ...string) {
bc.configHelp(m, fmt.Errorf("an application error occurred while disabling suggestions cache: %s", err.Error()))
return
}
_, err = bc.Bot.Send(m.Sender, fmt.Sprintf("Successfully disabled suggestions cache limits for type %s", cacheType))
_, err = bc.Bot.Send(Recipient(m), fmt.Sprintf("Successfully disabled suggestions cache limits for type %s", cacheType))
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
Expand All @@ -296,7 +296,7 @@ func (bc *BotController) configHandleLimit(m *tb.Message, params ...string) {
bc.configHelp(m, fmt.Errorf("an application error occurred while setting a new suggestions cache limit: %s", err.Error()))
return
}
_, err = bc.Bot.Send(m.Sender, fmt.Sprintf("Successfully set suggestions cache limits for type %s to %d", cacheType, limitValueParsed))
_, err = bc.Bot.Send(Recipient(m), fmt.Sprintf("Successfully set suggestions cache limits for type %s to %d", cacheType, limitValueParsed))
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
Expand Down Expand Up @@ -324,7 +324,7 @@ func (bc *BotController) configHandleAbout(m *tb.Message, params ...string) {
if version == "" {
version = "not specified"
}
_, err := bc.Bot.Send(m.Sender, escapeCharacters(fmt.Sprintf(`Version information about [LucaBernstein/beancount-bot-tg](https://github.com/LucaBernstein/beancount-bot-tg)
_, err := bc.Bot.Send(Recipient(m), escapeCharacters(fmt.Sprintf(`Version information about [LucaBernstein/beancount-bot-tg](https://github.com/LucaBernstein/beancount-bot-tg)
Version: [%s](%s)`,
version,
Expand All @@ -345,7 +345,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(m.Sender, fmt.Sprintf("Your current timezone offset is set to 'UTC%s'.", prettyTzOffset(tz_offset)))
_, 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())
}
Expand All @@ -358,21 +358,21 @@ func (bc *BotController) configHandleTimezoneOffset(m *tb.Message, params ...str
newTzOffset := params[0]
newTzParsed, err := strconv.Atoi(newTzOffset)
if err != nil {
_, err = bc.Bot.Send(m.Sender, "An error ocurred saving your timezone offset preference: "+err.Error())
_, 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())
}
return
}
err = bc.Repo.UserSetTzOffset(m, newTzParsed)
if err != nil {
_, err = bc.Bot.Send(m.Sender, "An error ocurred saving your timezone offset preference: "+err.Error())
_, 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())
}
return
}
_, err = bc.Bot.Send(m.Sender, fmt.Sprintf("Changed timezone offset for default dates for all future transactions from 'UTC%s' to 'UTC%s'.", prettyTzOffset(tz_offset), prettyTzOffset(newTzParsed)))
_, 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())
}
Expand All @@ -392,18 +392,18 @@ func (bc *BotController) configHandleAccountDelete(m *tb.Message, params ...stri

bc.deleteUserData(m)

_, err := bc.Bot.Send(m.Sender, "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.")
_, 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(m.Sender, "============")
_, err = bc.Bot.Send(Recipient(m), "============")
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
return
}
bc.Logf(INFO, m, "Reset command failed 'yes' verification. Aborting.")
_, err := bc.Bot.Send(m.Sender, "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.")
_, 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())
}
Expand Down
Loading

0 comments on commit fc9b398

Please sign in to comment.