-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.go
46 lines (38 loc) · 1.39 KB
/
slack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package utils
import (
"context"
"errors"
"github.com/slack-go/slack"
)
var (
errSlashCommandNotFound = errors.New("no slash command found in context")
errInteractionCallbackNotFound = errors.New("no callback found in context")
)
type slashCommandKey struct{}
type interactionCallbackKey struct{}
// SlashCommand retrieves the verified slash command from the context. To
// utilize this functionality, you must use the VerifySlashCommand middleware.
func SlashCommand(ctx context.Context) (*slack.SlashCommand, error) {
val := ctx.Value(slashCommandKey{})
cmd, ok := val.(*slack.SlashCommand)
if !ok {
return nil, errSlashCommandNotFound
}
return cmd, nil
}
// InteractionCallback retrieves the verified interaction callback from the context.
// To utilize this functionality, you must use the VerifyInteractionCallback middleware.
func InteractionCallback(ctx context.Context) (*slack.InteractionCallback, error) {
val := ctx.Value(interactionCallbackKey{})
callback, ok := val.(*slack.InteractionCallback)
if !ok {
return nil, errInteractionCallbackNotFound
}
return callback, nil
}
func withSlashCommand(ctx context.Context, cmd *slack.SlashCommand) context.Context {
return context.WithValue(ctx, slashCommandKey{}, cmd)
}
func withInteractionCallback(ctx context.Context, cmd *slack.InteractionCallback) context.Context {
return context.WithValue(ctx, interactionCallbackKey{}, cmd)
}