Repository navigation
feat(adapters/slack): add Sign in with Slack and block_actions types to slackapi #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a62fd33
feat(slackapi): add openid.connect.token, user manifest scopes, butto…
ethanndickson 44e2e79
chore(slackapi): tighten ParseBlockActions doc and error, fix test he…
ethanndickson f3cacac
test(slackapi): make the openid and block_actions fixtures exercise e…
ethanndickson 267ef0f
fix(slackapi): decode container thread_ts and document what ParseBloc…
ethanndickson 9849b5d
docs(slackapi): state the scope of ParseBlockActions
ethanndickson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,123 @@ | ||
| package slackapi | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| ) | ||
|
|
||
| // InteractionTypeBlockActions is the type of the interaction payload that | ||
| // Slack sends when someone uses an interactive element, such as a button. | ||
| const InteractionTypeBlockActions = "block_actions" | ||
|
|
||
| // BlockActionsPayload is a block_actions interaction payload. | ||
| type BlockActionsPayload struct { | ||
| // Type is InteractionTypeBlockActions. | ||
| Type string `json:"type,omitempty"` | ||
| // Team is the workspace of the user. It is zero when Slack sends null, | ||
| // for an org-wide install on Enterprise Grid. | ||
| Team InteractionTeam `json:"team,omitzero"` | ||
| // User is the user who used the element. | ||
| User InteractionUser `json:"user,omitzero"` | ||
| // APIAppID is the ID of the app. | ||
| APIAppID string `json:"api_app_id,omitempty"` | ||
| // Container is where the element is. | ||
| Container InteractionContainer `json:"container,omitzero"` | ||
| // TriggerID opens a modal within 3 seconds of the interaction. | ||
| TriggerID string `json:"trigger_id,omitempty"` | ||
| // Channel is the conversation of the element. | ||
| Channel InteractionChannel `json:"channel,omitzero"` | ||
| // Message is the message that holds the element. It is nil when the | ||
| // element is not in a message, and for an ephemeral message. For the | ||
| // thread of the message, use Container.ThreadTS. | ||
| Message *Message `json:"message,omitempty"` | ||
| // ResponseURL is the URL that PostResponseURL can post a response to. | ||
| ResponseURL string `json:"response_url,omitempty"` | ||
| // Actions are the actions of the interaction, usually one. | ||
| Actions []BlockAction `json:"actions,omitempty"` | ||
| } | ||
|
|
||
| // InteractionTeam is the workspace in an interaction payload. | ||
| type InteractionTeam struct { | ||
| // ID is the workspace ID (T). | ||
| ID string `json:"id,omitempty"` | ||
| // Domain is the subdomain of the workspace. | ||
| Domain string `json:"domain,omitempty"` | ||
| } | ||
|
|
||
| // InteractionUser is the user in an interaction payload. | ||
| type InteractionUser struct { | ||
| // ID is the user ID. | ||
| ID string `json:"id,omitempty"` | ||
| // Username is the user name. | ||
| Username string `json:"username,omitempty"` | ||
| // Name is the name of the user. | ||
| Name string `json:"name,omitempty"` | ||
| // TeamID is the ID of the workspace of the user. | ||
| TeamID string `json:"team_id,omitempty"` | ||
| } | ||
|
|
||
| // InteractionContainer is the container of the element in an interaction | ||
| // payload. | ||
| type InteractionContainer struct { | ||
| // Type is the container type, for example "message". | ||
| Type string `json:"type,omitempty"` | ||
| // MessageTS is the ts of the message that holds the element. | ||
| MessageTS string `json:"message_ts,omitempty"` | ||
| // ChannelID is the ID of the conversation of the message. | ||
| ChannelID string `json:"channel_id,omitempty"` | ||
|
ethanndickson marked this conversation as resolved.
Resolved
|
||
| // ThreadTS is the thread_ts of the message that holds the element. It | ||
| // is empty when the message is not in a thread, or when Slack omits it. | ||
| ThreadTS string `json:"thread_ts,omitempty"` | ||
| // IsEphemeral is true when the message is an ephemeral message. | ||
| IsEphemeral bool `json:"is_ephemeral,omitzero"` | ||
| } | ||
|
|
||
| // InteractionChannel is the conversation in an interaction payload. | ||
| type InteractionChannel struct { | ||
| // ID is the conversation ID. | ||
| ID string `json:"id,omitempty"` | ||
| // Name is the name of the conversation. | ||
| Name string `json:"name,omitempty"` | ||
| } | ||
|
|
||
| // BlockAction is one action in a BlockActionsPayload, with the fields of a | ||
| // button action. | ||
| type BlockAction struct { | ||
| // Type is the element type, for example "button". | ||
| Type string `json:"type,omitempty"` | ||
| // ActionID is the action_id of the element. | ||
| ActionID string `json:"action_id,omitempty"` | ||
| // BlockID is the block_id of the block that holds the element. | ||
| BlockID string `json:"block_id,omitempty"` | ||
| // Value is the value of the element, for example ButtonElement.Value. | ||
| Value string `json:"value,omitempty"` | ||
| // ActionTS is the timestamp of the action. | ||
| ActionTS string `json:"action_ts,omitempty"` | ||
| } | ||
|
|
||
| // ParseBlockActions decodes the payload of a DeliveryInteractive delivery | ||
| // when its type is InteractionTypeBlockActions. It returns false for an | ||
| // interaction of another type, for example view_submission, and an error if | ||
| // the payload does not decode. | ||
| // | ||
| // The typed payload covers a button click in a message, for an app that is | ||
| // installed in one workspace. For a click in a view, a select menu or another | ||
| // element, or an org-wide install on Enterprise Grid, decode the other | ||
| // fields, for example view, selected_option, or enterprise, from the raw | ||
| // payload. | ||
| func ParseBlockActions(payload []byte) (*BlockActionsPayload, bool, error) { | ||
| var head struct { | ||
| Type string `json:"type"` | ||
| } | ||
| if err := json.Unmarshal(payload, &head); err != nil { | ||
| return nil, false, fmt.Errorf("slack: invalid interaction: %w", err) | ||
| } | ||
| if head.Type != InteractionTypeBlockActions { | ||
| return nil, false, nil | ||
| } | ||
| var p BlockActionsPayload | ||
| if err := json.Unmarshal(payload, &p); err != nil { | ||
| return nil, false, fmt.Errorf("slack: invalid block_actions interaction: %w", err) | ||
| } | ||
| return &p, true, nil | ||
| } | ||
This file contains hidden or 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,128 @@ | ||
| package slackapi_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/coder/chat/adapters/slack/slackapi" | ||
| ) | ||
|
|
||
| func TestParseBlockActions(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("ButtonClick", func(t *testing.T) { | ||
| t.Parallel() | ||
| got, ok, err := slackapi.ParseBlockActions([]byte(`{ | ||
| "type": "block_actions", | ||
| "team": {"id": "T0TEAM", "domain": "acme"}, | ||
| "user": {"id": "U0USER", "username": "ada", "name": "ada.lovelace", "team_id": "T0TEAM"}, | ||
| "api_app_id": "A0APP", | ||
| "token": "verification-token", | ||
| "container": {"type": "message", "message_ts": "1700000000.000200", "channel_id": "C0CHAN", "thread_ts": "1700000000.000100", "is_ephemeral": false}, | ||
| "trigger_id": "123.456.abc", | ||
| "channel": {"id": "C0CHAN", "name": "general"}, | ||
| "enterprise": null, | ||
| "message": { | ||
| "type": "message", | ||
| "user": "U0BOT", | ||
| "bot_id": "B0BOT", | ||
| "text": "Done.", | ||
| "ts": "1700000000.000200", | ||
| "thread_ts": "1700000000.000100", | ||
| "team": "T0TEAM", | ||
| "blocks": [{"type": "actions", "block_id": "open", "elements": []}] | ||
| }, | ||
| "state": {"values": {}}, | ||
| "response_url": "https://hooks.slack.com/actions/T0TEAM/1/abc", | ||
| "actions": [{ | ||
| "action_id": "open_in_coder", | ||
| "block_id": "open", | ||
| "text": {"type": "plain_text", "text": "Open in Coder", "emoji": true}, | ||
| "value": "chat-1", | ||
| "type": "button", | ||
| "action_ts": "1700000001.000300" | ||
| }] | ||
| }`)) | ||
| if err != nil || !ok { | ||
| t.Fatalf("ParseBlockActions = %v, %v, want true, nil", ok, err) | ||
| } | ||
| methodsCheckEqual(t, "payload", got, &slackapi.BlockActionsPayload{ | ||
| Type: slackapi.InteractionTypeBlockActions, | ||
| Team: slackapi.InteractionTeam{ID: "T0TEAM", Domain: "acme"}, | ||
| User: slackapi.InteractionUser{ID: "U0USER", Username: "ada", Name: "ada.lovelace", TeamID: "T0TEAM"}, | ||
| APIAppID: "A0APP", | ||
| Container: slackapi.InteractionContainer{ | ||
| Type: "message", | ||
| MessageTS: "1700000000.000200", | ||
| ChannelID: "C0CHAN", | ||
| ThreadTS: "1700000000.000100", | ||
| }, | ||
| TriggerID: "123.456.abc", | ||
| Channel: slackapi.InteractionChannel{ID: "C0CHAN", Name: "general"}, | ||
| Message: &slackapi.Message{ | ||
| Type: "message", | ||
| User: "U0BOT", | ||
| BotID: "B0BOT", | ||
| Text: "Done.", | ||
| TS: "1700000000.000200", | ||
| ThreadTS: "1700000000.000100", | ||
| Team: "T0TEAM", | ||
| }, | ||
| ResponseURL: "https://hooks.slack.com/actions/T0TEAM/1/abc", | ||
| Actions: []slackapi.BlockAction{{ | ||
| Type: "button", | ||
| ActionID: "open_in_coder", | ||
| BlockID: "open", | ||
| Value: "chat-1", | ||
| ActionTS: "1700000001.000300", | ||
| }}, | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("EphemeralMessage", func(t *testing.T) { | ||
| t.Parallel() | ||
| got, ok, err := slackapi.ParseBlockActions([]byte(`{ | ||
| "type": "block_actions", | ||
| "user": {"id": "U0USER"}, | ||
| "container": {"type": "message", "message_ts": "1700000000.000300", "channel_id": "C0CHAN", "thread_ts": "1700000000.000100", "is_ephemeral": true}, | ||
| "channel": {"id": "C0CHAN"}, | ||
| "actions": [{"action_id": "link", "block_id": "link", "type": "button", "action_ts": "1700000001.000400"}] | ||
| }`)) | ||
| if err != nil || !ok { | ||
| t.Fatalf("ParseBlockActions = %v, %v, want true, nil", ok, err) | ||
| } | ||
| methodsCheckEqual(t, "payload", got, &slackapi.BlockActionsPayload{ | ||
| Type: slackapi.InteractionTypeBlockActions, | ||
| User: slackapi.InteractionUser{ID: "U0USER"}, | ||
| Container: slackapi.InteractionContainer{ | ||
| Type: "message", | ||
| MessageTS: "1700000000.000300", | ||
| ChannelID: "C0CHAN", | ||
| ThreadTS: "1700000000.000100", | ||
| IsEphemeral: true, | ||
| }, | ||
| Channel: slackapi.InteractionChannel{ID: "C0CHAN"}, | ||
| Actions: []slackapi.BlockAction{{Type: "button", ActionID: "link", BlockID: "link", ActionTS: "1700000001.000400"}}, | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("OtherType", func(t *testing.T) { | ||
| t.Parallel() | ||
| got, ok, err := slackapi.ParseBlockActions([]byte(`{"type":"view_submission","channel":"C0CHAN","view":{"id":"V0VIEW"}}`)) | ||
| if got != nil || ok || err != nil { | ||
| t.Fatalf("ParseBlockActions = %+v, %v, %v, want nil, false, nil", got, ok, err) | ||
| } | ||
| }) | ||
|
|
||
| for name, payload := range map[string]string{ | ||
| "InvalidJSON": `{`, | ||
| "InvalidBlockActions": `{"type":"block_actions","actions":"open"}`, | ||
| } { | ||
| t.Run(name, func(t *testing.T) { | ||
| t.Parallel() | ||
| got, ok, err := slackapi.ParseBlockActions([]byte(payload)) | ||
| if got != nil || ok || err == nil { | ||
| t.Fatalf("ParseBlockActions = %+v, %v, %v, want nil, false, an error", got, ok, err) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.