block-kit-builder.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import type {
  2. SectionBlock, HeaderBlock, InputBlock, DividerBlock, ActionsBlock,
  3. Button, Overflow, Datepicker, Select, RadioButtons, Checkboxes, Action, MultiSelect, PlainTextInput, Option,
  4. } from '@slack/types';
  5. export function divider(): DividerBlock {
  6. return {
  7. type: 'divider',
  8. };
  9. }
  10. export function markdownHeaderBlock(text: string): HeaderBlock {
  11. return {
  12. type: 'header',
  13. text: {
  14. type: 'plain_text',
  15. text,
  16. },
  17. };
  18. }
  19. export function markdownSectionBlock(text: string): SectionBlock {
  20. return {
  21. type: 'section',
  22. text: {
  23. type: 'mrkdwn',
  24. text,
  25. },
  26. };
  27. }
  28. export function inputSectionBlock(blockId: string, labelText: string, actionId: string, isMultiline: boolean, placeholder: string): InputBlock {
  29. return {
  30. type: 'input',
  31. block_id: blockId,
  32. label: {
  33. type: 'plain_text',
  34. text: labelText,
  35. },
  36. element: {
  37. type: 'plain_text_input',
  38. action_id: actionId,
  39. multiline: isMultiline,
  40. placeholder: {
  41. type: 'plain_text',
  42. text: placeholder,
  43. },
  44. },
  45. };
  46. }
  47. export function actionsBlock(...elements: (Button | Overflow | Datepicker | Select | RadioButtons | Checkboxes | Action)[]): ActionsBlock {
  48. return {
  49. type: 'actions',
  50. elements: [
  51. ...elements,
  52. ],
  53. };
  54. }
  55. export function inputBlock(
  56. element: Select | MultiSelect | Datepicker | PlainTextInput | RadioButtons | Checkboxes, blockId: string, labelText: string,
  57. ): InputBlock {
  58. return {
  59. type: 'input',
  60. block_id: blockId,
  61. element,
  62. label: {
  63. type: 'plain_text',
  64. text: labelText,
  65. },
  66. };
  67. }
  68. type ButtonElement = {
  69. text: string,
  70. actionId: string,
  71. style?: string,
  72. value?:string
  73. }
  74. /**
  75. * Button element
  76. * https://api.slack.com/reference/block-kit/block-elements#button
  77. */
  78. export function buttonElement({
  79. text, actionId, style, value,
  80. }:ButtonElement): Button {
  81. const button: Button = {
  82. type: 'button',
  83. text: {
  84. type: 'plain_text',
  85. text,
  86. },
  87. action_id: actionId,
  88. value,
  89. };
  90. if (style === 'primary' || style === 'danger') {
  91. button.style = style;
  92. }
  93. return button;
  94. }
  95. /**
  96. * Option object
  97. * https://api.slack.com/reference/block-kit/composition-objects#option
  98. */
  99. export function checkboxesElementOption(text: string, description: string, value: string): Option {
  100. return {
  101. text: {
  102. type: 'mrkdwn',
  103. text,
  104. },
  105. description: {
  106. type: 'plain_text',
  107. text: description,
  108. },
  109. value,
  110. };
  111. }