| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | ) |
| 7 | |
| 8 | // InteractionType type of interactions |
| 9 | type InteractionType string |
| 10 | |
| 11 | // ActionType type represents the type of action (attachment, block, etc.) |
| 12 | type ActionType string |
| 13 | |
| 14 | // action is an interface that should be implemented by all callback action types |
| 15 | type action interface { |
| 16 | actionType() ActionType |
| 17 | } |
| 18 | |
| 19 | // Types of interactions that can be received. |
| 20 | const ( |
| 21 | InteractionTypeDialogCancellation = InteractionType("dialog_cancellation") |
| 22 | InteractionTypeDialogSubmission = InteractionType("dialog_submission") |
| 23 | InteractionTypeDialogSuggestion = InteractionType("dialog_suggestion") |
| 24 | InteractionTypeInteractionMessage = InteractionType("interactive_message") |
| 25 | InteractionTypeMessageAction = InteractionType("message_action") |
| 26 | InteractionTypeBlockActions = InteractionType("block_actions") |
| 27 | InteractionTypeBlockSuggestion = InteractionType("block_suggestion") |
| 28 | InteractionTypeViewSubmission = InteractionType("view_submission") |
| 29 | InteractionTypeViewClosed = InteractionType("view_closed") |
| 30 | InteractionTypeShortcut = InteractionType("shortcut") |
| 31 | InteractionTypeWorkflowStepEdit = InteractionType("workflow_step_edit") |
| 32 | ) |
| 33 | |
| 34 | // InteractionCallback is sent from slack when a user interactions with a button or dialog. |
| 35 | type InteractionCallback struct { |
| 36 | Type InteractionType `json:"type"` |
| 37 | Token string `json:"token"` |
| 38 | CallbackID string `json:"callback_id"` |
| 39 | ResponseURL string `json:"response_url"` |
| 40 | TriggerID string `json:"trigger_id"` |
| 41 | ActionTs string `json:"action_ts"` |
| 42 | Team Team `json:"team"` |
| 43 | Channel Channel `json:"channel"` |
| 44 | User User `json:"user"` |
| 45 | OriginalMessage Message `json:"original_message"` |
| 46 | Message Message `json:"message"` |
| 47 | Name string `json:"name"` |
| 48 | Value string `json:"value"` |
| 49 | MessageTs string `json:"message_ts"` |
| 50 | AttachmentID string `json:"attachment_id"` |
| 51 | ActionCallback ActionCallbacks `json:"actions"` |
| 52 | View View `json:"view"` |
| 53 | ActionID string `json:"action_id"` |
| 54 | APIAppID string `json:"api_app_id"` |
| 55 | BlockID string `json:"block_id"` |
| 56 | Container Container `json:"container"` |
| 57 | Enterprise Enterprise `json:"enterprise"` |
| 58 | WorkflowStep InteractionWorkflowStep `json:"workflow_step"` |
| 59 | DialogSubmissionCallback |
| 60 | ViewSubmissionCallback |
| 61 | ViewClosedCallback |
| 62 | |
| 63 | // FIXME(kanata2): just workaround for backward-compatibility. |
| 64 | // See also https://github.com/slack-go/slack/issues/816 |
| 65 | RawState json.RawMessage `json:"state,omitempty"` |
| 66 | |
| 67 | // BlockActionState stands for the `state` field in block_actions type. |
| 68 | // NOTE: InteractionCallback.State has a role for the state of dialog_submission type, |
| 69 | // so we cannot use this field for backward-compatibility for now. |
| 70 | BlockActionState *BlockActionStates `json:"-"` |
| 71 | } |
| 72 | |
| 73 | type BlockActionStates struct { |
| 74 | Values map[string]map[string]BlockAction `json:"values"` |
| 75 | } |
| 76 | |
| 77 | func (ic *InteractionCallback) MarshalJSON() ([]byte, error) { |
| 78 | type alias InteractionCallback |
| 79 | tmp := alias(*ic) |
| 80 | if tmp.Type == InteractionTypeBlockActions { |
| 81 | if tmp.BlockActionState == nil { |
| 82 | tmp.RawState = []byte(`{}`) |
| 83 | } else { |
| 84 | state, err := json.Marshal(tmp.BlockActionState.Values) |
| 85 | if err != nil { |
| 86 | return nil, err |
| 87 | } |
| 88 | tmp.RawState = []byte(`{"values":` + string(state) + `}`) |
| 89 | } |
| 90 | } else if ic.Type == InteractionTypeDialogSubmission { |
| 91 | tmp.RawState = []byte(tmp.State) |
| 92 | } |
| 93 | // Use pointer for go1.7 |
| 94 | return json.Marshal(&tmp) |
| 95 | } |
| 96 | |
| 97 | func (ic *InteractionCallback) UnmarshalJSON(b []byte) error { |
| 98 | type alias InteractionCallback |
| 99 | tmp := struct { |
| 100 | Type InteractionType `json:"type"` |
| 101 | *alias |
| 102 | }{ |
| 103 | alias: (*alias)(ic), |
| 104 | } |
| 105 | if err := json.Unmarshal(b, &tmp); err != nil { |
| 106 | return err |
| 107 | } |
| 108 | *ic = InteractionCallback(*tmp.alias) |
| 109 | ic.Type = tmp.Type |
| 110 | if ic.Type == InteractionTypeBlockActions { |
| 111 | if len(ic.RawState) > 0 { |
| 112 | err := json.Unmarshal(ic.RawState, &ic.BlockActionState) |
| 113 | if err != nil { |
| 114 | return err |
| 115 | } |
| 116 | } |
| 117 | } else if ic.Type == InteractionTypeDialogSubmission { |
| 118 | ic.State = string(ic.RawState) |
| 119 | } |
| 120 | return nil |
| 121 | } |
| 122 | |
| 123 | type Container struct { |
| 124 | Type string `json:"type"` |
| 125 | ViewID string `json:"view_id"` |
| 126 | MessageTs string `json:"message_ts"` |
| 127 | ThreadTs string `json:"thread_ts,omitempty"` |
| 128 | AttachmentID json.Number `json:"attachment_id"` |
| 129 | ChannelID string `json:"channel_id"` |
| 130 | IsEphemeral bool `json:"is_ephemeral"` |
| 131 | IsAppUnfurl bool `json:"is_app_unfurl"` |
| 132 | } |
| 133 | |
| 134 | type Enterprise struct { |
| 135 | ID string `json:"id"` |
| 136 | Name string `json:"name"` |
| 137 | } |
| 138 | |
| 139 | type InteractionWorkflowStep struct { |
| 140 | WorkflowStepEditID string `json:"workflow_step_edit_id,omitempty"` |
| 141 | WorkflowID string `json:"workflow_id"` |
| 142 | StepID string `json:"step_id"` |
| 143 | Inputs *WorkflowStepInputs `json:"inputs,omitempty"` |
| 144 | Outputs *[]WorkflowStepOutput `json:"outputs,omitempty"` |
| 145 | } |
| 146 | |
| 147 | // ActionCallback is a convenience struct defined to allow dynamic unmarshalling of |
| 148 | // the "actions" value in Slack's JSON response, which varies depending on block type |
| 149 | type ActionCallbacks struct { |
| 150 | AttachmentActions []*AttachmentAction |
| 151 | BlockActions []*BlockAction |
| 152 | } |
| 153 | |
| 154 | // MarshalJSON implements the Marshaller interface in order to combine both |
| 155 | // action callback types back into a single array, like how the api responds. |
| 156 | // This makes Marshaling and Unmarshaling an InteractionCallback symmetrical |
| 157 | func (a ActionCallbacks) MarshalJSON() ([]byte, error) { |
| 158 | count := 0 |
| 159 | length := len(a.AttachmentActions) + len(a.BlockActions) |
| 160 | buffer := bytes.NewBufferString("[") |
| 161 | |
| 162 | f := func(obj interface{}) error { |
| 163 | js, err := json.Marshal(obj) |
| 164 | if err != nil { |
| 165 | return err |
| 166 | } |
| 167 | _, err = buffer.Write(js) |
| 168 | if err != nil { |
| 169 | return err |
| 170 | } |
| 171 | |
| 172 | count++ |
| 173 | if count < length { |
| 174 | _, err = buffer.WriteString(",") |
| 175 | return err |
| 176 | } |
| 177 | return nil |
| 178 | } |
| 179 | |
| 180 | for _, act := range a.AttachmentActions { |
| 181 | err := f(act) |
| 182 | if err != nil { |
| 183 | return nil, err |
| 184 | } |
| 185 | } |
| 186 | for _, blk := range a.BlockActions { |
| 187 | err := f(blk) |
| 188 | if err != nil { |
| 189 | return nil, err |
| 190 | } |
| 191 | } |
| 192 | buffer.WriteString("]") |
| 193 | return buffer.Bytes(), nil |
| 194 | } |
| 195 | |
| 196 | // UnmarshalJSON implements the Marshaller interface in order to delegate |
| 197 | // marshalling and allow for proper type assertion when decoding the response |
| 198 | func (a *ActionCallbacks) UnmarshalJSON(data []byte) error { |
| 199 | var raw []json.RawMessage |
| 200 | err := json.Unmarshal(data, &raw) |
| 201 | if err != nil { |
| 202 | return err |
| 203 | } |
| 204 | |
| 205 | for _, r := range raw { |
| 206 | var obj map[string]interface{} |
| 207 | err := json.Unmarshal(r, &obj) |
| 208 | if err != nil { |
| 209 | return err |
| 210 | } |
| 211 | |
| 212 | if _, ok := obj["block_id"].(string); ok { |
| 213 | action, err := unmarshalAction(r, &BlockAction{}) |
| 214 | if err != nil { |
| 215 | return err |
| 216 | } |
| 217 | |
| 218 | a.BlockActions = append(a.BlockActions, action.(*BlockAction)) |
| 219 | continue |
| 220 | } |
| 221 | |
| 222 | action, err := unmarshalAction(r, &AttachmentAction{}) |
| 223 | if err != nil { |
| 224 | return err |
| 225 | } |
| 226 | a.AttachmentActions = append(a.AttachmentActions, action.(*AttachmentAction)) |
| 227 | } |
| 228 | |
| 229 | return nil |
| 230 | } |
| 231 | |
| 232 | func unmarshalAction(r json.RawMessage, callbackAction action) (action, error) { |
| 233 | err := json.Unmarshal(r, callbackAction) |
| 234 | if err != nil { |
| 235 | return nil, err |
| 236 | } |
| 237 | return callbackAction, nil |
| 238 | } |