Skip to content

Commit

Permalink
fix(slackbot): Do not hang after a dialog is cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
mumoshu committed Feb 16, 2020
1 parent ccd4cbf commit 927042d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
12 changes: 12 additions & 0 deletions pkg/slack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Slackbot integration for Variant 2

This package provides a HTTP server that handles Slack interaction callbacks so that
it can run any variant command in response to a Slack slash command.

## Acknowledgements

I've learned how to code a Slack bot in Go by reading the following repositories and articles:

- https://github.com/kpurdon/slappd
- https://github.com/shiimaxx/slack-coffeebot
- https://github.com/eure/cafe-bot/
9 changes: 6 additions & 3 deletions pkg/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ func (conn *Connection) Run() error {

userInput := cmd.Command + " " + cmdText

c := "run " + cmdText
// This should be a full Variant command without the executable name.
//
// E.g. for what you would type `variant run foo bar --opt1`, this shuold be `run foo bar --opt1`, as without the executable name.
variantCmdWithArgs := "run " + cmdText

response := slack.Message{}
// You should always specify response type even when you're intended to use the default "ephemeral"
Expand All @@ -278,7 +281,7 @@ func (conn *Connection) Run() error {
_, _, _, err := conn.Client.SendMessage(
cmd.ChannelID,
slack.MsgOptionResponseURL(cmd.ResponseURL, slack.ResponseTypeEphemeral),
slack.MsgOptionText(fmt.Sprintf("Running `%s`... I'll soon post a message visible to everyone in this channel to share the progress and the result of it.", c), false),
slack.MsgOptionText(fmt.Sprintf("Running `%s`... I'll soon post a message visible to everyone in this channel to share the progress and the result of it.", variantCmdWithArgs), false),
)
if err != nil {
fmt.Printf("async response 1 error: %v", err)
Expand All @@ -299,7 +302,7 @@ func (conn *Connection) Run() error {
return
}

res := conn.HandleSlashCommand(conn, c, cmd)
res := conn.HandleSlashCommand(conn, variantCmdWithArgs, cmd)

fmt.Printf("async slash command run finished: %s\n", res)

Expand Down
15 changes: 11 additions & 4 deletions slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,22 @@ func (r *Runner) StartSlackbot(name string) error {

callbackID := message.UserID + "_" + message.TriggerID
dialog := slack.Dialog{
Title: cmd,
SubmitLabel: "Run",
CallbackID: callbackID,
Elements: elems,
Title: cmd,
SubmitLabel: "Run",
CallbackID: callbackID,
Elements: elems,
NotifyOnCancel: true,
}

done := make(chan error, 1)

bot.RegisterInteractionCallbackHandler(callbackID, func(callback slack.InteractionCallback) (interface{}, error) {
if callback.Type == slack.InteractionTypeDialogCancellation {
done <- nil

return nil, nil
}

if callback.Type != slack.InteractionTypeDialogSubmission {
return nil, fmt.Errorf("unexpected type of interaction callback: want %s, got %s", slack.InteractionTypeDialogSubmission, callback.Type)
}
Expand Down

0 comments on commit 927042d

Please sign in to comment.