From 1d6d59b2a0703c7e361df75330f446d2e609f405 Mon Sep 17 00:00:00 2001 From: Luca Bernstein Date: Fri, 21 Oct 2022 18:04:13 +0200 Subject: [PATCH] No error messages in group chat if not in tx (#191) Closes #190 --- bot/controller.go | 10 +++++----- bot/controller_test.go | 6 ++---- scenarioTests/features/start.feature | 5 +++++ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/bot/controller.go b/bot/controller.go index 453fad1..3abf43a 100644 --- a/bot/controller.go +++ b/bot/controller.go @@ -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()) diff --git a/bot/controller_test.go b/bot/controller_test.go index b385b5c..a48b484 100644 --- a/bot/controller_test.go +++ b/bot/controller_test.go @@ -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 @@ -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 @@ -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 diff --git a/scenarioTests/features/start.feature b/scenarioTests/features/start.feature index a185afc..e3bd81f 100644 --- a/scenarioTests/features/start.feature +++ b/scenarioTests/features/start.feature @@ -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