togetter.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const {
  2. inputBlock, actionsBlock, buttonElement, checkboxesElementOption,
  3. } = require('@growi/slack');
  4. const { fromUnixTime, format } = require('date-fns');
  5. module.exports = () => {
  6. const BaseSlackCommandHandler = require('./slack-command-handler');
  7. const handler = new BaseSlackCommandHandler();
  8. handler.handleCommand = async function(client, body, args, limit = 10) {
  9. // TODO GW-6721 Get the time from args
  10. const result = await client.conversations.history({
  11. channel: body.channel_id,
  12. limit,
  13. });
  14. // Return Checkbox Message
  15. client.chat.postEphemeral({
  16. channel: body.channel_id,
  17. user: body.user_id,
  18. text: 'Select messages to use.',
  19. blocks: this.togetterMessageBlocks(result.messages, body, args, limit),
  20. });
  21. return;
  22. };
  23. handler.togetterMessageBlocks = function(messages, body, args, limit) {
  24. return [
  25. inputBlock(this.togetterCheckboxesElement(messages), 'selected_messages', 'Select massages to use.'),
  26. actionsBlock(buttonElement({ text: 'Show more', actionId: 'togetterShowMore', value: JSON.stringify({ body, args, limit }) })),
  27. inputBlock(this.togetterInputBlockElement('page_path', '/'), 'page_path', 'Page path'),
  28. actionsBlock(
  29. buttonElement({ text: 'Cancel', actionId: 'togetterCancelPageCreation' }),
  30. buttonElement({ text: 'Create page', actionId: 'togetterCreatePage', color: 'primary' }),
  31. ),
  32. ];
  33. };
  34. handler.togetterCheckboxesElement = function(messages) {
  35. return {
  36. type: 'checkboxes',
  37. options: this.togetterCheckboxesElementOptions(messages),
  38. action_id: 'checkboxes_changed',
  39. };
  40. };
  41. handler.togetterCheckboxesElementOptions = function(messages) {
  42. const options = messages
  43. .sort((a, b) => { return a.ts - b.ts })
  44. .map((message, index) => {
  45. const date = fromUnixTime(message.ts);
  46. return checkboxesElementOption(`*${message.user}* ${format(new Date(date), 'yyyy/MM/dd HH:mm:ss')}`, message.text, `selected-${index}`);
  47. });
  48. return options;
  49. };
  50. /**
  51. * Plain-text input element
  52. * https://api.slack.com/reference/block-kit/block-elements#input
  53. */
  54. handler.togetterInputBlockElement = function(actionId, placeholderText = 'Write something ...') {
  55. return {
  56. type: 'plain_text_input',
  57. placeholder: {
  58. type: 'plain_text',
  59. text: placeholderText,
  60. },
  61. action_id: actionId,
  62. };
  63. };
  64. return handler;
  65. };