block-kit-builder.ts 2.4 KB

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