Skip to content

Commit

Permalink
Merge be662b1 into a2ad386
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanSussman committed Jan 13, 2019
2 parents a2ad386 + be662b1 commit 8c5035c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
19 changes: 17 additions & 2 deletions core/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,30 @@ func isValidHitChatRule(message *models.Message, rule models.Rule, processedInpu
if rule.Hear == "" {
// Get all the args that the message sender supplied
args := utils.RuleArgTokenizer(processedInput)
var optionalArgs int
var requiredArgs int
// take note of all optional args that end with a '?'
for _, arg := range rule.Args {
if strings.HasSuffix(arg, "?") {
optionalArgs++
}
}
// ensure we only require args that don't end with '?'
requiredArgs = len(rule.Args) - optionalArgs
// Are we expecting a number of args but don't have as many as the rule defines? Send a helpful message
if len(rule.Args) > 0 && len(args) < len(rule.Args) {
if len(rule.Args) > 0 && requiredArgs > len(args) {
msg := fmt.Sprintf("You might be missing an argument or two. This is what I'm looking for\n```%s```", rule.HelpText)
message.Output = msg
return false
}
// Go through the supplied args and make them available as variables
for i, arg := range rule.Args {
message.Vars[arg] = args[i]
if i >= len(args) {
// assign variable and trim '?' from the end
message.Vars[strings.TrimSuffix(arg, "?")] = ""
} else {
message.Vars[arg] = args[i]
}
}
}
return true
Expand Down
14 changes: 14 additions & 0 deletions core/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,13 @@ func Test_handleChatServiceRule(t *testing.T) {
HelpText: "foo <arg1> <arg2>",
}

ruleOpt := models.Rule{
Name: "Test Rule with optional arg",
Respond: "foo",
Args: []string{"arg1", "arg2?"},
HelpText: "foo <arg1> <arg2>",
}

testBot := new(models.Bot)
testBot.Name = "Testbot"

Expand All @@ -695,6 +702,12 @@ func Test_handleChatServiceRule(t *testing.T) {
BotMentioned: true,
}

testMessageOptionalArgs := models.Message{
Input: "foo arg1",
Vars: map[string]string{},
BotMentioned: true,
}

tests := []struct {
name string
args args
Expand All @@ -707,6 +720,7 @@ func Test_handleChatServiceRule(t *testing.T) {
{"respond rule - hit true - valid", args{rule: rule, hit: true, bot: testBot, message: testMessage, processedInput: "arg1 arg2"}, true, true, "Hmm, the 'format_output' field in your configuration is empty"},
{"respond rule - hit true - bot not mentioned", args{rule: rule, hit: true, bot: testBot, message: testMessageBotNotMentioned, processedInput: "arg1 arg2"}, false, false, ""},
{"respond rule - hit true - valid - not enough args", args{rule: rule, hit: true, bot: testBot, message: testMessageNotEnoughArgs, processedInput: "arg1"}, true, true, "You might be missing an argument or two. This is what I'm looking for\n```foo <arg1> <arg2>```"},
{"respond rule - hit true - valid optional arg", args{rule: ruleOpt, hit: true, bot: testBot, message: testMessageOptionalArgs, processedInput: "arg1"}, true, true, ""},
{"respond rule - hit true - invalid", args{rule: rule, hit: true, bot: testBot, message: testMessage}, true, true, "You might be missing an argument or two. This is what I'm looking for\n```foo <arg1> <arg2>```"},
}
for _, tt := range tests {
Expand Down

0 comments on commit 8c5035c

Please sign in to comment.