-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from innogames/variables
custom variables which can be used in macros etc
- Loading branch information
Showing
10 changed files
with
272 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package variables | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/innogames/slack-bot/bot/matcher" | ||
"github.com/nlopes/slack" | ||
) | ||
|
||
func (c *command) Add(match matcher.Result, event slack.MessageEvent) { | ||
name := match.GetString("name") | ||
value := match.GetString("value") | ||
|
||
list := loadList(event.User) | ||
list[name] = value | ||
storeList(event, list) | ||
|
||
c.slackClient.Reply( | ||
event, | ||
fmt.Sprintf("Added variable: `%s` = `%s`.", name, value), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package variables | ||
|
||
import ( | ||
"github.com/innogames/slack-bot/bot" | ||
"github.com/innogames/slack-bot/bot/matcher" | ||
"github.com/innogames/slack-bot/client" | ||
) | ||
|
||
// GetCommand returns a set of all commands to manage user specific variables | ||
func GetCommand(slackClient client.SlackClient) bot.Command { | ||
return &command{slackClient} | ||
} | ||
|
||
type command struct { | ||
slackClient client.SlackClient | ||
} | ||
|
||
func (c *command) GetMatcher() matcher.Matcher { | ||
return matcher.NewGroupMatcher( | ||
matcher.NewRegexpMatcher("(add|set) variable '?(?P<name>.*?)'? '?(?P<value>.*?)'?", c.Add), | ||
matcher.NewRegexpMatcher("(delete|remove) variable '?(?P<name>.*?)'?", c.Delete), | ||
matcher.NewTextMatcher("list variables", c.List), | ||
) | ||
} | ||
|
||
func (c *command) GetHelp() []bot.Help { | ||
return []bot.Help{ | ||
{ | ||
"custom variables", | ||
"", | ||
[]string{}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package variables | ||
|
||
import ( | ||
"fmt" | ||
"github.com/innogames/slack-bot/bot/matcher" | ||
"github.com/nlopes/slack" | ||
) | ||
|
||
func (c *command) Delete(match matcher.Result, event slack.MessageEvent) { | ||
name := match.GetString("name") | ||
|
||
list := loadList(event.User) | ||
delete(list, name) | ||
storeList(event, list) | ||
|
||
c.slackClient.Reply(event, fmt.Sprintf("Okay, I deleted variable: `%s`", name)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package variables | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/innogames/slack-bot/bot" | ||
"github.com/innogames/slack-bot/mocks" | ||
"github.com/nlopes/slack" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCustomCommands(t *testing.T) { | ||
slackClient := &mocks.SlackClient{} | ||
commands := bot.Commands{} | ||
variablesCommand := GetCommand(slackClient).(*command) | ||
commands.AddCommand(variablesCommand) | ||
|
||
t.Run("Invalid command", func(t *testing.T) { | ||
event := slack.MessageEvent{} | ||
event.User = "user1" | ||
event.Text = "notify me not" | ||
|
||
actual := commands.Run(event) | ||
assert.Equal(t, false, actual) | ||
}) | ||
|
||
t.Run("List empty variables", func(t *testing.T) { | ||
event := slack.MessageEvent{} | ||
event.Text = "list variables" | ||
event.User = "user1" | ||
slackClient.On("Reply", event, "No variables define yet. Use `add variable 'defaultServer' 'beta'`").Return("") | ||
actual := commands.Run(event) | ||
assert.Equal(t, true, actual) | ||
}) | ||
|
||
t.Run("Add a variable with invalid syntax", func(t *testing.T) { | ||
event := slack.MessageEvent{} | ||
event.User = "user1" | ||
event.Text = "add variable name" | ||
actual := commands.Run(event) | ||
assert.Equal(t, false, actual) | ||
}) | ||
|
||
t.Run("Add valid variable", func(t *testing.T) { | ||
event := slack.MessageEvent{} | ||
event.User = "user1" | ||
event.Text = "add variable 'myKey' 'myValue'" | ||
|
||
slackClient.On("Reply", event, "Added variable: `myKey` = `myValue`.").Return("") | ||
actual := commands.Run(event) | ||
|
||
assert.Equal(t, true, actual) | ||
}) | ||
|
||
t.Run("List commands should list new variable", func(t *testing.T) { | ||
event := slack.MessageEvent{} | ||
event.User = "user1" | ||
event.Text = "list variables" | ||
|
||
slackClient.On("Reply", event, "You defined 1 variables:\n - myKey: `myValue`") | ||
actual := commands.Run(event) | ||
|
||
assert.Equal(t, true, actual) | ||
}) | ||
|
||
t.Run("Template with unknown user", func(t *testing.T) { | ||
|
||
function := variablesCommand.GetTemplateFunction()["customVariable"] | ||
|
||
actual := function.(func(string, string) string)("U123", "myKey") | ||
assert.Equal(t, "_unknown variable: myKey_", actual) | ||
}) | ||
|
||
t.Run("Template with unknown user", func(t *testing.T) { | ||
|
||
function := variablesCommand.GetTemplateFunction()["customVariable"] | ||
|
||
actual := function.(func(string, string) string)("user1", "myKey2") | ||
assert.Equal(t, "_unknown variable: myKey2_", actual) | ||
}) | ||
|
||
t.Run("Template with known variable", func(t *testing.T) { | ||
|
||
function := variablesCommand.GetTemplateFunction()["customVariable"] | ||
|
||
actual := function.(func(string, string) string)("user1", "myKey") | ||
assert.Equal(t, "myValue", actual) | ||
}) | ||
|
||
t.Run("Delete variable with invalid syntax", func(t *testing.T) { | ||
event := slack.MessageEvent{} | ||
event.Text = "delete variable" | ||
event.User = "user1" | ||
|
||
actual := commands.Run(event) | ||
|
||
assert.Equal(t, false, actual) | ||
}) | ||
|
||
t.Run("Delete variable", func(t *testing.T) { | ||
event := slack.MessageEvent{} | ||
event.Text = "delete variable myKey" | ||
event.User = "user1" | ||
|
||
slackClient.On("Reply", event, "Okay, I deleted variable: `myKey`") | ||
|
||
actual := commands.Run(event) | ||
|
||
assert.Equal(t, true, actual) | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package variables | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/innogames/slack-bot/bot/matcher" | ||
"github.com/nlopes/slack" | ||
) | ||
|
||
func (c *command) List(match matcher.Result, event slack.MessageEvent) { | ||
list := loadList(event.User) | ||
if len(list) == 0 { | ||
c.slackClient.Reply(event, "No variables define yet. Use `add variable 'defaultServer' 'beta'`") | ||
return | ||
} | ||
|
||
responseText := fmt.Sprintf("You defined %d variables:", len(list)) | ||
for name, value := range list { | ||
responseText += fmt.Sprintf("\n - %s: `%s`", name, value) | ||
} | ||
|
||
c.slackClient.Reply(event, responseText) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package variables | ||
|
||
import ( | ||
"github.com/innogames/slack-bot/bot/storage" | ||
"github.com/nlopes/slack" | ||
) | ||
|
||
const storeKey = "user_variables" | ||
|
||
type list map[string]string | ||
|
||
func loadList(userId string) list { | ||
list := make(list, 0) | ||
|
||
storage.Read(storeKey, userId, &list) | ||
|
||
return list | ||
} | ||
|
||
func storeList(event slack.MessageEvent, list list) { | ||
storage.Write(storeKey, event.User, list) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package variables | ||
|
||
import ( | ||
"fmt" | ||
"text/template" | ||
) | ||
|
||
func (c *command) GetTemplateFunction() template.FuncMap { | ||
return template.FuncMap{ | ||
"customVariable": func(userId string, name string) string { | ||
list := loadList(userId) | ||
|
||
if value, ok := list[name]; ok { | ||
return value | ||
} | ||
|
||
return fmt.Sprintf("_unknown variable: %s_", name) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters