Skip to content

Commit

Permalink
feat: Handle more interaction type
Browse files Browse the repository at this point in the history
A CallbackID field was added to the struct InteractionDefinition struct, to
handle the following interactions:
- shortcut
- message_actions
- view_submission
- view_closed
  • Loading branch information
nicolas-carlier-ls committed Oct 24, 2023
1 parent 32e6347 commit e3e7bdf
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 14 deletions.
111 changes: 111 additions & 0 deletions examples/interaction-view/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package main

import (
"context"
"fmt"
"log"
"os"

"github.com/shomali11/slacker/v2"
"github.com/slack-go/slack"
)

var moodSurveyView = slack.ModalViewRequest{
Type: "modal",
CallbackID: "mood-survey-callback-id",
Title: &slack.TextBlockObject{
Type: "plain_text",
Text: "Which mood are you in?",
},
Submit: &slack.TextBlockObject{
Type: "plain_text",
Text: "Submit",
},
NotifyOnClose: true,
Blocks: slack.Blocks{
BlockSet: []slack.Block{
&slack.InputBlock{
Type: slack.MBTInput,
BlockID: "mood",
Label: &slack.TextBlockObject{
Type: "plain_text",
Text: "Mood",
},
Element: &slack.SelectBlockElement{
Type: slack.OptTypeStatic,
ActionID: "mood",
Options: []*slack.OptionBlockObject{
{
Text: &slack.TextBlockObject{
Type: "plain_text",
Text: "Happy",
},
Value: "Happy",
},
{
Text: &slack.TextBlockObject{
Type: "plain_text",
Text: "Sad",
},
Value: "Sad",
},
},
},
},
},
},
}

// Implements a basic interactive command with modal view.
func main() {
bot := slacker.NewClient(
os.Getenv("SLACK_BOT_TOKEN"),
os.Getenv("SLACK_APP_TOKEN"),
slacker.WithDebug(false),
)

bot.AddCommand(&slacker.CommandDefinition{
Command: "mood",
Handler: moodCmdHandler,
})

bot.AddInteraction(&slacker.InteractionDefinition{
CallbackID: "mood-survey-callback-id",
Handler: moodViewHandler,
})

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

err := bot.Listen(ctx)
if err != nil {
log.Fatal(err)
}
}

func moodCmdHandler(ctx *slacker.CommandContext) {
_, err := ctx.SlackClient().OpenView(
ctx.Event().Data.(*slack.SlashCommand).TriggerID,
moodSurveyView,
)
if err != nil {
log.Printf("ERROR openEscalationModal: %v", err)
}
}

func moodViewHandler(ctx *slacker.InteractionContext) {
switch ctx.Callback().Type {
case slack.InteractionTypeViewSubmission:
{
viewState := ctx.Callback().View.State.Values
fmt.Printf(
"Mood view submitted.\nMood: %s\n",
viewState["mood"]["mood"].SelectedOption.Value,
)
}
case slack.InteractionTypeViewClosed:
{
fmt.Print("Mood view closed.\n")
}
}
}
1 change: 1 addition & 0 deletions interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package slacker
// InteractionDefinition structure contains definition of the bot interaction
type InteractionDefinition struct {
BlockID string
CallbackID string
Middlewares []InteractionMiddlewareHandler
Handler InteractionHandler
}
Expand Down
48 changes: 34 additions & 14 deletions slacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (s *Slacker) GetCommandGroups() []*CommandGroup {
return s.commandGroups
}

// GetInteractions returns Groups
// GetInteractions returns Interactions
func (s *Slacker) GetInteractions() []*Interaction {
return s.interactions
}
Expand Down Expand Up @@ -174,8 +174,12 @@ func (s *Slacker) AddCommandGroup(prefix string) *CommandGroup {

// AddInteraction define a new interaction and append it to the list of interactions
func (s *Slacker) AddInteraction(definition *InteractionDefinition) {
if len(definition.BlockID) == 0 {
s.logger.Error("missing `BlockID`")
if len(definition.BlockID) == 0 && len(definition.CallbackID) == 0 {
s.logger.Error("missing `BlockID` or `CallbackID`")
return
}
if len(definition.BlockID) != 0 && len(definition.CallbackID) != 0 {
s.logger.Error("`BlockID` or `CallbackID` should not be set at the same time")
return
}
s.interactions = append(s.interactions, newInteraction(definition))
Expand Down Expand Up @@ -437,21 +441,37 @@ func (s *Slacker) handleInteractionEvent(ctx context.Context, callback *slack.In
middlewares := make([]InteractionMiddlewareHandler, 0)
middlewares = append(middlewares, s.interactionMiddlewares...)

for _, interaction := range s.interactions {
for _, action := range callback.ActionCallback.BlockActions {
switch callback.Type {
case slack.InteractionTypeBlockActions:
for _, interaction := range s.interactions {
definition := interaction.Definition()
if action.BlockID != definition.BlockID {
continue
for _, action := range callback.ActionCallback.BlockActions {
if action.BlockID == definition.BlockID {
interactionCtx := newInteractionContext(ctx, s.logger, s.slackClient, callback, definition)
middlewares = append(middlewares, definition.Middlewares...)
executeInteraction(interactionCtx, definition.Handler, middlewares...)
return
}
}

interactionCtx := newInteractionContext(ctx, s.logger, s.slackClient, callback, definition)

middlewares = append(middlewares, definition.Middlewares...)
executeInteraction(interactionCtx, definition.Handler, middlewares...)
return
}
s.logger.Debugf("unsupported block actions interaction type received %+v\n", callback)
case slack.InteractionTypeViewSubmission,
slack.InteractionTypeViewClosed,
slack.InteractionTypeShortcut,
slack.InteractionTypeMessageAction:
for _, interaction := range s.interactions {
definition := interaction.Definition()
if definition.CallbackID == callback.View.CallbackID {
interactionCtx := newInteractionContext(ctx, s.logger, s.slackClient, callback, definition)
middlewares = append(middlewares, definition.Middlewares...)
executeInteraction(interactionCtx, definition.Handler, middlewares...)
return
}
}
s.logger.Debugf("unsupported interaction type received %+v\n", callback)
default:
s.logger.Debugf("unsupported interaction type received %+v\n", callback)
}

if s.unsupportedInteractionHandler != nil {
interactionCtx := newInteractionContext(ctx, s.logger, s.slackClient, callback, nil)
executeInteraction(interactionCtx, s.unsupportedInteractionHandler, middlewares...)
Expand Down

0 comments on commit e3e7bdf

Please sign in to comment.