-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New command "notify user @whateveruser active"
- Loading branch information
Showing
10 changed files
with
222 additions
and
23 deletions.
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
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
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,72 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/innogames/slack-bot/v2/command/queue" | ||
|
||
"github.com/innogames/slack-bot/v2/bot" | ||
"github.com/innogames/slack-bot/v2/bot/matcher" | ||
"github.com/innogames/slack-bot/v2/bot/msg" | ||
) | ||
|
||
const notifyCheckInterval = time.Minute * 1 | ||
|
||
// Command which informs the user when the given user got active | ||
func newUserStatusCommand(base bot.BaseCommand) *userStatus { | ||
return &userStatus{ | ||
base, | ||
notifyCheckInterval, | ||
} | ||
} | ||
|
||
type userStatus struct { | ||
bot.BaseCommand | ||
checkInterval time.Duration | ||
} | ||
|
||
func (c *userStatus) GetMatcher() matcher.Matcher { | ||
return matcher.NewRegexpMatcher(`notify user <@(?P<user>.*)> (?P<status>(away|active))`, c.NotifyUserActive) | ||
} | ||
|
||
func (c *userStatus) NotifyUserActive(match matcher.Result, message msg.Message) { | ||
user := match.GetString("user") | ||
expectedStatus := match.GetString("status") | ||
|
||
// in case of bot restart: restart this command again | ||
runningCommand := queue.AddRunningCommand(message, message.Text) | ||
|
||
c.AddReaction("⌛", message) | ||
go func() { | ||
defer c.RemoveReaction("⌛", message) | ||
defer runningCommand.Done() | ||
|
||
for { | ||
presence, err := c.SlackClient.GetUserPresence(user) | ||
if err != nil { | ||
c.ReplyError(message, err) | ||
return | ||
} | ||
|
||
if presence.Presence == expectedStatus { | ||
c.SendMessage(message, fmt.Sprintf("User <@%s> is %s now!", user, presence.Presence)) | ||
return | ||
} | ||
|
||
time.Sleep(c.checkInterval) | ||
} | ||
}() | ||
} | ||
|
||
func (c *userStatus) GetHelp() []bot.Help { | ||
return []bot.Help{ | ||
{ | ||
Command: "notify user active", | ||
Description: "Inform you if the given user change the slack status to active.", | ||
Examples: []string{ | ||
"notify user @myboss active", | ||
}, | ||
}, | ||
} | ||
} |
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,71 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/innogames/slack-bot/v2/bot" | ||
"github.com/innogames/slack-bot/v2/bot/msg" | ||
"github.com/innogames/slack-bot/v2/command/queue" | ||
"github.com/innogames/slack-bot/v2/mocks" | ||
"github.com/slack-go/slack" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUserStatus(t *testing.T) { | ||
slackClient := &mocks.SlackClient{} | ||
|
||
base := bot.BaseCommand{SlackClient: slackClient} | ||
command := newUserStatusCommand(base) | ||
|
||
commands := bot.Commands{} | ||
commands.AddCommand(command) | ||
|
||
t.Run("Invalid command", func(t *testing.T) { | ||
message := msg.Message{} | ||
message.Text = "notify for something" | ||
|
||
actual := commands.Run(message) | ||
assert.False(t, actual) | ||
}) | ||
|
||
t.Run("Check with error", func(t *testing.T) { | ||
message := msg.Message{} | ||
message.Text = "notify user <@U123456> active" | ||
|
||
err := fmt.Errorf("some slack error") | ||
slackClient.On("GetUserPresence", "U123456").Once().Return(nil, err) | ||
|
||
mocks.AssertReaction(slackClient, "⌛", message) | ||
mocks.AssertRemoveReaction(slackClient, "⌛", message) | ||
mocks.AssertError(slackClient, message, err) | ||
|
||
actual := commands.Run(message) | ||
assert.True(t, actual) | ||
queue.WaitTillHavingNoQueuedMessage() | ||
}) | ||
|
||
t.Run("Check user getting active", func(t *testing.T) { | ||
message := msg.Message{} | ||
message.Text = "notify user <@U123456> active" | ||
|
||
command.checkInterval = time.Millisecond * 1 | ||
presenceAway := &slack.UserPresence{ | ||
Presence: "away", | ||
} | ||
presenceActive := &slack.UserPresence{ | ||
Presence: "active", | ||
} | ||
slackClient.On("GetUserPresence", "U123456").Once().Return(presenceAway, nil) | ||
slackClient.On("GetUserPresence", "U123456").Once().Return(presenceActive, nil) | ||
|
||
mocks.AssertReaction(slackClient, "⌛", message) | ||
mocks.AssertRemoveReaction(slackClient, "⌛", message) | ||
mocks.AssertSlackMessage(slackClient, message, "User <@U123456> is active now!") | ||
|
||
actual := commands.Run(message) | ||
assert.True(t, actual) | ||
queue.WaitTillHavingNoQueuedMessage() | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.