| 1 | package slack |
| 2 | |
| 3 | // https://api.slack.com/reference/messaging/block-elements |
| 4 | |
| 5 | const ( |
| 6 | METCheckboxGroups MessageElementType = "checkboxes" |
| 7 | METImage MessageElementType = "image" |
| 8 | METButton MessageElementType = "button" |
| 9 | METOverflow MessageElementType = "overflow" |
| 10 | METDatepicker MessageElementType = "datepicker" |
| 11 | METTimepicker MessageElementType = "timepicker" |
| 12 | METPlainTextInput MessageElementType = "plain_text_input" |
| 13 | METRadioButtons MessageElementType = "radio_buttons" |
| 14 | |
| 15 | MixedElementImage MixedElementType = "mixed_image" |
| 16 | MixedElementText MixedElementType = "mixed_text" |
| 17 | |
| 18 | OptTypeStatic string = "static_select" |
| 19 | OptTypeExternal string = "external_select" |
| 20 | OptTypeUser string = "users_select" |
| 21 | OptTypeConversations string = "conversations_select" |
| 22 | OptTypeChannels string = "channels_select" |
| 23 | |
| 24 | MultiOptTypeStatic string = "multi_static_select" |
| 25 | MultiOptTypeExternal string = "multi_external_select" |
| 26 | MultiOptTypeUser string = "multi_users_select" |
| 27 | MultiOptTypeConversations string = "multi_conversations_select" |
| 28 | MultiOptTypeChannels string = "multi_channels_select" |
| 29 | ) |
| 30 | |
| 31 | type MessageElementType string |
| 32 | type MixedElementType string |
| 33 | |
| 34 | // BlockElement defines an interface that all block element types should implement. |
| 35 | type BlockElement interface { |
| 36 | ElementType() MessageElementType |
| 37 | } |
| 38 | |
| 39 | type MixedElement interface { |
| 40 | MixedElementType() MixedElementType |
| 41 | } |
| 42 | |
| 43 | type Accessory struct { |
| 44 | ImageElement *ImageBlockElement |
| 45 | ButtonElement *ButtonBlockElement |
| 46 | OverflowElement *OverflowBlockElement |
| 47 | DatePickerElement *DatePickerBlockElement |
| 48 | TimePickerElement *TimePickerBlockElement |
| 49 | PlainTextInputElement *PlainTextInputBlockElement |
| 50 | RadioButtonsElement *RadioButtonsBlockElement |
| 51 | SelectElement *SelectBlockElement |
| 52 | MultiSelectElement *MultiSelectBlockElement |
| 53 | CheckboxGroupsBlockElement *CheckboxGroupsBlockElement |
| 54 | UnknownElement *UnknownBlockElement |
| 55 | } |
| 56 | |
| 57 | // NewAccessory returns a new Accessory for a given block element |
| 58 | func NewAccessory(element BlockElement) *Accessory { |
| 59 | switch element.(type) { |
| 60 | case *ImageBlockElement: |
| 61 | return &Accessory{ImageElement: element.(*ImageBlockElement)} |
| 62 | case *ButtonBlockElement: |
| 63 | return &Accessory{ButtonElement: element.(*ButtonBlockElement)} |
| 64 | case *OverflowBlockElement: |
| 65 | return &Accessory{OverflowElement: element.(*OverflowBlockElement)} |
| 66 | case *DatePickerBlockElement: |
| 67 | return &Accessory{DatePickerElement: element.(*DatePickerBlockElement)} |
| 68 | case *TimePickerBlockElement: |
| 69 | return &Accessory{TimePickerElement: element.(*TimePickerBlockElement)} |
| 70 | case *PlainTextInputBlockElement: |
| 71 | return &Accessory{PlainTextInputElement: element.(*PlainTextInputBlockElement)} |
| 72 | case *RadioButtonsBlockElement: |
| 73 | return &Accessory{RadioButtonsElement: element.(*RadioButtonsBlockElement)} |
| 74 | case *SelectBlockElement: |
| 75 | return &Accessory{SelectElement: element.(*SelectBlockElement)} |
| 76 | case *MultiSelectBlockElement: |
| 77 | return &Accessory{MultiSelectElement: element.(*MultiSelectBlockElement)} |
| 78 | case *CheckboxGroupsBlockElement: |
| 79 | return &Accessory{CheckboxGroupsBlockElement: element.(*CheckboxGroupsBlockElement)} |
| 80 | default: |
| 81 | return &Accessory{UnknownElement: element.(*UnknownBlockElement)} |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // BlockElements is a convenience struct defined to allow dynamic unmarshalling of |
| 86 | // the "elements" value in Slack's JSON response, which varies depending on BlockElement type |
| 87 | type BlockElements struct { |
| 88 | ElementSet []BlockElement `json:"elements,omitempty"` |
| 89 | } |
| 90 | |
| 91 | // UnknownBlockElement any block element that this library does not directly support. |
| 92 | // See the "Rich Elements" section at the following URL: |
| 93 | // https://api.slack.com/changelog/2019-09-what-they-see-is-what-you-get-and-more-and-less |
| 94 | // New block element types may be introduced by Slack at any time; this is a catch-all for any such block elements. |
| 95 | type UnknownBlockElement struct { |
| 96 | Type MessageElementType `json:"type"` |
| 97 | Elements BlockElements |
| 98 | } |
| 99 | |
| 100 | // ElementType returns the type of the Element |
| 101 | func (s UnknownBlockElement) ElementType() MessageElementType { |
| 102 | return s.Type |
| 103 | } |
| 104 | |
| 105 | // ImageBlockElement An element to insert an image - this element can be used |
| 106 | // in section and context blocks only. If you want a block with only an image |
| 107 | // in it, you're looking for the image block. |
| 108 | // |
| 109 | // More Information: https://api.slack.com/reference/messaging/block-elements#image |
| 110 | type ImageBlockElement struct { |
| 111 | Type MessageElementType `json:"type"` |
| 112 | ImageURL string `json:"image_url"` |
| 113 | AltText string `json:"alt_text"` |
| 114 | } |
| 115 | |
| 116 | // ElementType returns the type of the Element |
| 117 | func (s ImageBlockElement) ElementType() MessageElementType { |
| 118 | return s.Type |
| 119 | } |
| 120 | |
| 121 | func (s ImageBlockElement) MixedElementType() MixedElementType { |
| 122 | return MixedElementImage |
| 123 | } |
| 124 | |
| 125 | // NewImageBlockElement returns a new instance of an image block element |
| 126 | func NewImageBlockElement(imageURL, altText string) *ImageBlockElement { |
| 127 | return &ImageBlockElement{ |
| 128 | Type: METImage, |
| 129 | ImageURL: imageURL, |
| 130 | AltText: altText, |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Style is a style of Button element |
| 135 | // https://api.slack.com/reference/block-kit/block-elements#button__fields |
| 136 | type Style string |
| 137 | |
| 138 | const ( |
| 139 | StyleDefault Style = "" |
| 140 | StylePrimary Style = "primary" |
| 141 | StyleDanger Style = "danger" |
| 142 | ) |
| 143 | |
| 144 | // ButtonBlockElement defines an interactive element that inserts a button. The |
| 145 | // button can be a trigger for anything from opening a simple link to starting |
| 146 | // a complex workflow. |
| 147 | // |
| 148 | // More Information: https://api.slack.com/reference/messaging/block-elements#button |
| 149 | type ButtonBlockElement struct { |
| 150 | Type MessageElementType `json:"type,omitempty"` |
| 151 | Text *TextBlockObject `json:"text"` |
| 152 | ActionID string `json:"action_id,omitempty"` |
| 153 | URL string `json:"url,omitempty"` |
| 154 | Value string `json:"value,omitempty"` |
| 155 | Confirm *ConfirmationBlockObject `json:"confirm,omitempty"` |
| 156 | Style Style `json:"style,omitempty"` |
| 157 | } |
| 158 | |
| 159 | // ElementType returns the type of the element |
| 160 | func (s ButtonBlockElement) ElementType() MessageElementType { |
| 161 | return s.Type |
| 162 | } |
| 163 | |
| 164 | // WithStyle adds styling to the button object and returns the modified ButtonBlockElement |
| 165 | func (s *ButtonBlockElement) WithStyle(style Style) *ButtonBlockElement { |
| 166 | s.Style = style |
| 167 | return s |
| 168 | } |
| 169 | |
| 170 | // WithConfirm adds a confirmation dialogue to the button object and returns the modified ButtonBlockElement |
| 171 | func (s *ButtonBlockElement) WithConfirm(confirm *ConfirmationBlockObject) *ButtonBlockElement { |
| 172 | s.Confirm = confirm |
| 173 | return s |
| 174 | } |
| 175 | |
| 176 | // NewButtonBlockElement returns an instance of a new button element to be used within a block |
| 177 | func NewButtonBlockElement(actionID, value string, text *TextBlockObject) *ButtonBlockElement { |
| 178 | return &ButtonBlockElement{ |
| 179 | Type: METButton, |
| 180 | ActionID: actionID, |
| 181 | Text: text, |
| 182 | Value: value, |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // OptionsResponse defines the response used for select block typahead. |
| 187 | // |
| 188 | // More Information: https://api.slack.com/reference/block-kit/block-elements#external_multi_select |
| 189 | type OptionsResponse struct { |
| 190 | Options []*OptionBlockObject `json:"options,omitempty"` |
| 191 | } |
| 192 | |
| 193 | // OptionGroupsResponse defines the response used for select block typahead. |
| 194 | // |
| 195 | // More Information: https://api.slack.com/reference/block-kit/block-elements#external_multi_select |
| 196 | type OptionGroupsResponse struct { |
| 197 | OptionGroups []*OptionGroupBlockObject `json:"option_groups,omitempty"` |
| 198 | } |
| 199 | |
| 200 | // SelectBlockElement defines the simplest form of select menu, with a static list |
| 201 | // of options passed in when defining the element. |
| 202 | // |
| 203 | // More Information: https://api.slack.com/reference/messaging/block-elements#select |
| 204 | type SelectBlockElement struct { |
| 205 | Type string `json:"type,omitempty"` |
| 206 | Placeholder *TextBlockObject `json:"placeholder,omitempty"` |
| 207 | ActionID string `json:"action_id,omitempty"` |
| 208 | Options []*OptionBlockObject `json:"options,omitempty"` |
| 209 | OptionGroups []*OptionGroupBlockObject `json:"option_groups,omitempty"` |
| 210 | InitialOption *OptionBlockObject `json:"initial_option,omitempty"` |
| 211 | InitialUser string `json:"initial_user,omitempty"` |
| 212 | InitialConversation string `json:"initial_conversation,omitempty"` |
| 213 | InitialChannel string `json:"initial_channel,omitempty"` |
| 214 | DefaultToCurrentConversation bool `json:"default_to_current_conversation,omitempty"` |
| 215 | ResponseURLEnabled bool `json:"response_url_enabled,omitempty"` |
| 216 | Filter *SelectBlockElementFilter `json:"filter,omitempty"` |
| 217 | MinQueryLength *int `json:"min_query_length,omitempty"` |
| 218 | Confirm *ConfirmationBlockObject `json:"confirm,omitempty"` |
| 219 | } |
| 220 | |
| 221 | // SelectBlockElementFilter allows to filter select element conversation options by type. |
| 222 | // |
| 223 | // More Information: https://api.slack.com/reference/block-kit/composition-objects#filter_conversations |
| 224 | type SelectBlockElementFilter struct { |
| 225 | Include []string `json:"include,omitempty"` |
| 226 | ExcludeExternalSharedChannels bool `json:"exclude_external_shared_channels,omitempty"` |
| 227 | ExcludeBotUsers bool `json:"exclude_bot_users,omitempty"` |
| 228 | } |
| 229 | |
| 230 | // ElementType returns the type of the Element |
| 231 | func (s SelectBlockElement) ElementType() MessageElementType { |
| 232 | return MessageElementType(s.Type) |
| 233 | } |
| 234 | |
| 235 | // NewOptionsSelectBlockElement returns a new instance of SelectBlockElement for use with |
| 236 | // the Options object only. |
| 237 | func NewOptionsSelectBlockElement(optType string, placeholder *TextBlockObject, actionID string, options ...*OptionBlockObject) *SelectBlockElement { |
| 238 | return &SelectBlockElement{ |
| 239 | Type: optType, |
| 240 | Placeholder: placeholder, |
| 241 | ActionID: actionID, |
| 242 | Options: options, |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // NewOptionsGroupSelectBlockElement returns a new instance of SelectBlockElement for use with |
| 247 | // the Options object only. |
| 248 | func NewOptionsGroupSelectBlockElement( |
| 249 | optType string, |
| 250 | placeholder *TextBlockObject, |
| 251 | actionID string, |
| 252 | optGroups ...*OptionGroupBlockObject, |
| 253 | ) *SelectBlockElement { |
| 254 | return &SelectBlockElement{ |
| 255 | Type: optType, |
| 256 | Placeholder: placeholder, |
| 257 | ActionID: actionID, |
| 258 | OptionGroups: optGroups, |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | // MultiSelectBlockElement defines a multiselect menu, with a static list |
| 263 | // of options passed in when defining the element. |
| 264 | // |
| 265 | // More Information: https://api.slack.com/reference/messaging/block-elements#multi_select |
| 266 | type MultiSelectBlockElement struct { |
| 267 | Type string `json:"type,omitempty"` |
| 268 | Placeholder *TextBlockObject `json:"placeholder,omitempty"` |
| 269 | ActionID string `json:"action_id,omitempty"` |
| 270 | Options []*OptionBlockObject `json:"options,omitempty"` |
| 271 | OptionGroups []*OptionGroupBlockObject `json:"option_groups,omitempty"` |
| 272 | InitialOptions []*OptionBlockObject `json:"initial_options,omitempty"` |
| 273 | InitialUsers []string `json:"initial_users,omitempty"` |
| 274 | InitialConversations []string `json:"initial_conversations,omitempty"` |
| 275 | InitialChannels []string `json:"initial_channels,omitempty"` |
| 276 | MinQueryLength *int `json:"min_query_length,omitempty"` |
| 277 | MaxSelectedItems *int `json:"max_selected_items,omitempty"` |
| 278 | Confirm *ConfirmationBlockObject `json:"confirm,omitempty"` |
| 279 | } |
| 280 | |
| 281 | // ElementType returns the type of the Element |
| 282 | func (s MultiSelectBlockElement) ElementType() MessageElementType { |
| 283 | return MessageElementType(s.Type) |
| 284 | } |
| 285 | |
| 286 | // NewOptionsMultiSelectBlockElement returns a new instance of SelectBlockElement for use with |
| 287 | // the Options object only. |
| 288 | func NewOptionsMultiSelectBlockElement(optType string, placeholder *TextBlockObject, actionID string, options ...*OptionBlockObject) *MultiSelectBlockElement { |
| 289 | return &MultiSelectBlockElement{ |
| 290 | Type: optType, |
| 291 | Placeholder: placeholder, |
| 292 | ActionID: actionID, |
| 293 | Options: options, |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | // NewOptionsGroupMultiSelectBlockElement returns a new instance of MultiSelectBlockElement for use with |
| 298 | // the Options object only. |
| 299 | func NewOptionsGroupMultiSelectBlockElement( |
| 300 | optType string, |
| 301 | placeholder *TextBlockObject, |
| 302 | actionID string, |
| 303 | optGroups ...*OptionGroupBlockObject, |
| 304 | ) *MultiSelectBlockElement { |
| 305 | return &MultiSelectBlockElement{ |
| 306 | Type: optType, |
| 307 | Placeholder: placeholder, |
| 308 | ActionID: actionID, |
| 309 | OptionGroups: optGroups, |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | // OverflowBlockElement defines the fields needed to use an overflow element. |
| 314 | // And Overflow Element is like a cross between a button and a select menu - |
| 315 | // when a user clicks on this overflow button, they will be presented with a |
| 316 | // list of options to choose from. |
| 317 | // |
| 318 | // More Information: https://api.slack.com/reference/messaging/block-elements#overflow |
| 319 | type OverflowBlockElement struct { |
| 320 | Type MessageElementType `json:"type"` |
| 321 | ActionID string `json:"action_id,omitempty"` |
| 322 | Options []*OptionBlockObject `json:"options"` |
| 323 | Confirm *ConfirmationBlockObject `json:"confirm,omitempty"` |
| 324 | } |
| 325 | |
| 326 | // ElementType returns the type of the Element |
| 327 | func (s OverflowBlockElement) ElementType() MessageElementType { |
| 328 | return s.Type |
| 329 | } |
| 330 | |
| 331 | // NewOverflowBlockElement returns an instance of a new Overflow Block Element |
| 332 | func NewOverflowBlockElement(actionID string, options ...*OptionBlockObject) *OverflowBlockElement { |
| 333 | return &OverflowBlockElement{ |
| 334 | Type: METOverflow, |
| 335 | ActionID: actionID, |
| 336 | Options: options, |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | // DatePickerBlockElement defines an element which lets users easily select a |
| 341 | // date from a calendar style UI. Date picker elements can be used inside of |
| 342 | // section and actions blocks. |
| 343 | // |
| 344 | // More Information: https://api.slack.com/reference/messaging/block-elements#datepicker |
| 345 | type DatePickerBlockElement struct { |
| 346 | Type MessageElementType `json:"type"` |
| 347 | ActionID string `json:"action_id,omitempty"` |
| 348 | Placeholder *TextBlockObject `json:"placeholder,omitempty"` |
| 349 | InitialDate string `json:"initial_date,omitempty"` |
| 350 | Confirm *ConfirmationBlockObject `json:"confirm,omitempty"` |
| 351 | } |
| 352 | |
| 353 | // ElementType returns the type of the Element |
| 354 | func (s DatePickerBlockElement) ElementType() MessageElementType { |
| 355 | return s.Type |
| 356 | } |
| 357 | |
| 358 | // NewDatePickerBlockElement returns an instance of a date picker element |
| 359 | func NewDatePickerBlockElement(actionID string) *DatePickerBlockElement { |
| 360 | return &DatePickerBlockElement{ |
| 361 | Type: METDatepicker, |
| 362 | ActionID: actionID, |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | // TimePickerBlockElement defines an element which lets users easily select a |
| 367 | // time from nice UI. Time picker elements can be used inside of |
| 368 | // section and actions blocks. |
| 369 | // |
| 370 | // More Information: https://api.slack.com/reference/messaging/block-elements#timepicker |
| 371 | type TimePickerBlockElement struct { |
| 372 | Type MessageElementType `json:"type"` |
| 373 | ActionID string `json:"action_id,omitempty"` |
| 374 | Placeholder *TextBlockObject `json:"placeholder,omitempty"` |
| 375 | InitialTime string `json:"initial_time,omitempty"` |
| 376 | Confirm *ConfirmationBlockObject `json:"confirm,omitempty"` |
| 377 | } |
| 378 | |
| 379 | // ElementType returns the type of the Element |
| 380 | func (s TimePickerBlockElement) ElementType() MessageElementType { |
| 381 | return s.Type |
| 382 | } |
| 383 | |
| 384 | // NewTimePickerBlockElement returns an instance of a date picker element |
| 385 | func NewTimePickerBlockElement(actionID string) *TimePickerBlockElement { |
| 386 | return &TimePickerBlockElement{ |
| 387 | Type: METTimepicker, |
| 388 | ActionID: actionID, |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // PlainTextInputBlockElement creates a field where a user can enter freeform |
| 393 | // data. |
| 394 | // Plain-text input elements are currently only available in modals. |
| 395 | // |
| 396 | // More Information: https://api.slack.com/reference/block-kit/block-elements#input |
| 397 | type PlainTextInputBlockElement struct { |
| 398 | Type MessageElementType `json:"type"` |
| 399 | ActionID string `json:"action_id,omitempty"` |
| 400 | Placeholder *TextBlockObject `json:"placeholder,omitempty"` |
| 401 | InitialValue string `json:"initial_value,omitempty"` |
| 402 | Multiline bool `json:"multiline,omitempty"` |
| 403 | MinLength int `json:"min_length,omitempty"` |
| 404 | MaxLength int `json:"max_length,omitempty"` |
| 405 | DispatchActionConfig *DispatchActionConfig `json:"dispatch_action_config,omitempty"` |
| 406 | } |
| 407 | |
| 408 | type DispatchActionConfig struct { |
| 409 | TriggerActionsOn []string `json:"trigger_actions_on,omitempty"` |
| 410 | } |
| 411 | |
| 412 | // ElementType returns the type of the Element |
| 413 | func (s PlainTextInputBlockElement) ElementType() MessageElementType { |
| 414 | return s.Type |
| 415 | } |
| 416 | |
| 417 | // NewPlainTextInputBlockElement returns an instance of a plain-text input |
| 418 | // element |
| 419 | func NewPlainTextInputBlockElement(placeholder *TextBlockObject, actionID string) *PlainTextInputBlockElement { |
| 420 | return &PlainTextInputBlockElement{ |
| 421 | Type: METPlainTextInput, |
| 422 | ActionID: actionID, |
| 423 | Placeholder: placeholder, |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | // CheckboxGroupsBlockElement defines an element which allows users to choose |
| 428 | // one or more items from a list of possible options. |
| 429 | // |
| 430 | // More Information: https://api.slack.com/reference/block-kit/block-elements#checkboxes |
| 431 | type CheckboxGroupsBlockElement struct { |
| 432 | Type MessageElementType `json:"type"` |
| 433 | ActionID string `json:"action_id,omitempty"` |
| 434 | Options []*OptionBlockObject `json:"options"` |
| 435 | InitialOptions []*OptionBlockObject `json:"initial_options,omitempty"` |
| 436 | Confirm *ConfirmationBlockObject `json:"confirm,omitempty"` |
| 437 | } |
| 438 | |
| 439 | // ElementType returns the type of the Element |
| 440 | func (c CheckboxGroupsBlockElement) ElementType() MessageElementType { |
| 441 | return c.Type |
| 442 | } |
| 443 | |
| 444 | // NewCheckboxGroupsBlockElement returns an instance of a checkbox-group block element |
| 445 | func NewCheckboxGroupsBlockElement(actionID string, options ...*OptionBlockObject) *CheckboxGroupsBlockElement { |
| 446 | return &CheckboxGroupsBlockElement{ |
| 447 | Type: METCheckboxGroups, |
| 448 | ActionID: actionID, |
| 449 | Options: options, |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | // RadioButtonsBlockElement defines an element which lets users choose one item |
| 454 | // from a list of possible options. |
| 455 | // |
| 456 | // More Information: https://api.slack.com/reference/block-kit/block-elements#radio |
| 457 | type RadioButtonsBlockElement struct { |
| 458 | Type MessageElementType `json:"type"` |
| 459 | ActionID string `json:"action_id,omitempty"` |
| 460 | Options []*OptionBlockObject `json:"options"` |
| 461 | InitialOption *OptionBlockObject `json:"initial_option,omitempty"` |
| 462 | Confirm *ConfirmationBlockObject `json:"confirm,omitempty"` |
| 463 | } |
| 464 | |
| 465 | // ElementType returns the type of the Element |
| 466 | func (s RadioButtonsBlockElement) ElementType() MessageElementType { |
| 467 | return s.Type |
| 468 | } |
| 469 | |
| 470 | // NewRadioButtonsBlockElement returns an instance of a radio buttons element. |
| 471 | func NewRadioButtonsBlockElement(actionID string, options ...*OptionBlockObject) *RadioButtonsBlockElement { |
| 472 | return &RadioButtonsBlockElement{ |
| 473 | Type: METRadioButtons, |
| 474 | ActionID: actionID, |
| 475 | Options: options, |
| 476 | } |
| 477 | } |