-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert BlockID to InteractionID field into the InteractionDefinition struct, and add the necessary dispatch mechanisms to handle the following interactions: - shortcut - message_actions - view_submission - view_closed Add Interaction type to interaction definition
- Loading branch information
1 parent
4d2ff79
commit 4151d3f
Showing
6 changed files
with
234 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/shomali11/slacker/v2" | ||
"github.com/slack-go/slack" | ||
) | ||
|
||
// 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.AddInteraction(&slacker.InteractionDefinition{ | ||
InteractionID: "mood-survey-message-shortcut-callback-id", | ||
Handler: moodShortcutHandler, | ||
Type: slack.InteractionTypeMessageAction, | ||
}) | ||
|
||
bot.AddInteraction(&slacker.InteractionDefinition{ | ||
InteractionID: "mood-survey-global-shortcut-callback-id", | ||
Handler: moodShortcutHandler, | ||
Type: slack.InteractionTypeShortcut, | ||
}) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
err := bot.Listen(ctx) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func moodShortcutHandler(ctx *slacker.InteractionContext) { | ||
switch ctx.Callback().Type { | ||
case slack.InteractionTypeMessageAction: | ||
{ | ||
fmt.Print("Message shortcut.\n") | ||
} | ||
case slack.InteractionTypeShortcut: | ||
{ | ||
fmt.Print("Global shortcut.\n") | ||
} | ||
} | ||
} |
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,118 @@ | ||
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{ | ||
InteractionID: "mood-survey-callback-id", | ||
Handler: moodViewHandler, | ||
Type: slack.InteractionTypeViewSubmission, | ||
}) | ||
|
||
bot.AddInteraction(&slacker.InteractionDefinition{ | ||
InteractionID: "mood-survey-callback-id", | ||
Handler: moodViewHandler, | ||
Type: slack.InteractionTypeViewClosed, | ||
}) | ||
|
||
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") | ||
} | ||
} | ||
} |
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