block-kit-builder.ts 2.5 KB

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