Skip to content

Commit

Permalink
No error messages in group chat if not in tx (#191)
Browse files Browse the repository at this point in the history
Closes #190
  • Loading branch information
LucaBernstein authored Oct 21, 2022
1 parent 5445159 commit 1d6d59b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
10 changes: 5 additions & 5 deletions bot/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,12 +614,12 @@ func (bc *BotController) handleTextState(c tb.Context) error {
return nil
}

// If number has been entered
// Create new tx, inform user that tx has automatically been started, call handleTextState with same message again (infininite loop protection?)
// return
// else: warn
if crud.IsGroupChat(c.Message()) && !strings.HasPrefix(c.Message().Text, "/") {
bc.Logf(DEBUG, c.Message(), "Received text without having any prior state but am in a group chat. Ignoring and not sending error to user")
return nil
}

bc.Logf(WARN, c.Message(), "Received text without having any prior state")
bc.Logf(WARN, c.Message(), "Received text without having any prior state and not in group chat or message starts with '/'")
_, err := bc.Bot.Send(Recipient(c.Message()), fmt.Sprintf("Please check /%s on how to use this bot. E.g. you might need to start a transaction first before sending data.", CMD_HELP), clearKeyboard())
if err != nil {
bc.Logf(ERROR, c.Message(), "Sending bot message failed: %s", err.Error())
Expand Down
6 changes: 2 additions & 4 deletions bot/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"github.com/LucaBernstein/beancount-bot-tg/helpers"
)



// GitHub-Issue #16: Panic if plain message without state arrives
func TestTextHandlingWithoutPriorState(t *testing.T) {
// create test dependencies
Expand Down Expand Up @@ -64,7 +62,7 @@ func TestTextHandlingWithoutPriorState(t *testing.T) {
bc.handleTextState(&MockContext{M: &tb.Message{Chat: chat, Text: "Expenses:Groceries"}}) // to (via handleTextState)

// After the first tx is done, send some command
m := &MockContext{M: &tb.Message{Chat: chat}}
m := &MockContext{M: &tb.Message{Chat: chat, Sender: &tb.User{ID: chat.ID}}} // same ID: not group chat
bc.handleTextState(m)

// should catch and send help instead of fail
Expand Down Expand Up @@ -380,7 +378,7 @@ func TestTimezoneOffsetForAutomaticDate(t *testing.T) {
bc.handleTextState(&MockContext{&tb.Message{Chat: chat, Text: "Expenses:Groceries"}}) // to (via handleTextState)

// After the first tx is done, send some command
m := &MockContext{M: &tb.Message{Chat: chat}}
m := &MockContext{M: &tb.Message{Chat: chat, Sender: &tb.User{ID: chat.ID}}}
bc.handleTextState(m)

// should catch and send help instead of fail
Expand Down
5 changes: 5 additions & 0 deletions scenarioTests/features/start.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ Feature: start bot interaction
When I send the message "/help"
Then 1 messages should be sent back
And the response should include the message "/cancel - Cancel any running commands or transactions"

Scenario: Don't answer to random text in group chat
Given I have a bot
When I send the message "this is not a command and not a number and I am not in a tx"
Then 0 messages should be sent back

0 comments on commit 1d6d59b

Please sign in to comment.