bolt.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const logger = require('@alias/logger')('growi:service:BoltService');
  2. class BoltReciever {
  3. init(app) {
  4. this.bolt = app;
  5. }
  6. async requestHandler(req, res) {
  7. let ackCalled = false;
  8. const event = {
  9. body: req.body,
  10. ack: (response) => {
  11. if (ackCalled) {
  12. return;
  13. }
  14. if (response instanceof Error) {
  15. res.status(500).send();
  16. }
  17. else if (!response) {
  18. res.send('');
  19. }
  20. else {
  21. res.send(response);
  22. }
  23. ackCalled = true;
  24. },
  25. };
  26. await this.bolt.processEvent(event);
  27. // for verification request URL on Event Subscriptions
  28. res.send(req.body);
  29. }
  30. }
  31. const { App } = require('@slack/bolt');
  32. const { WebClient, LogLevel } = require('@slack/web-api');
  33. class BoltService {
  34. constructor(crowi) {
  35. this.crowi = crowi;
  36. this.receiver = new BoltReciever();
  37. const signingSecret = crowi.configManager.getConfig('crowi', 'slack:signingSecret');
  38. const token = crowi.configManager.getConfig('crowi', 'slack:botToken');
  39. const client = new WebClient(token, { logLevel: LogLevel.DEBUG });
  40. this.client = client;
  41. if (token != null || signingSecret != null) {
  42. logger.debug('TwitterStrategy: setup is done');
  43. this.bolt = new App({
  44. token,
  45. signingSecret,
  46. receiver: this.receiver,
  47. });
  48. this.init();
  49. }
  50. }
  51. init() {
  52. // Example of listening for event
  53. // See. https://github.com/slackapi/bolt-js#listening-for-events
  54. // or https://slack.dev/bolt-js/concepts#basic
  55. this.bolt.command('/growi-bot', async({ command, ack, say }) => { // demo
  56. await say('Hello');
  57. });
  58. // The echo command simply echoes on command
  59. this.bolt.command('/echo', async({ command, ack, say }) => {
  60. // Acknowledge command request
  61. await ack();
  62. await say(`${command.text}`);
  63. });
  64. // TODO check if firstArg is the supported command(like "search")
  65. this.bolt.command('/growi', async({ command, ack }) => {
  66. await ack();
  67. const inputSlack = command.text.split(' ');
  68. const firstArg = inputSlack[0];
  69. const secondArg = inputSlack[1];
  70. let resultPaths;
  71. if (firstArg === 'search') {
  72. const { searchService } = this.crowi;
  73. const option = { limit: 10 };
  74. const searchResults = await searchService.searchKeyword(secondArg, null, {}, option);
  75. resultPaths = searchResults.data.map((data) => {
  76. return data._source.path;
  77. });
  78. }
  79. // TODO impl try-catch
  80. try {
  81. await this.client.chat.postEphemeral({
  82. channel: command.channel_id,
  83. user: command.user_id,
  84. blocks: [
  85. {
  86. type: 'section',
  87. text: {
  88. type: 'mrkdwn',
  89. text: '*検索結果 10 件*',
  90. },
  91. },
  92. {
  93. type: 'section',
  94. text: {
  95. type: 'mrkdwn',
  96. text: `${resultPaths.join('\n')}`,
  97. },
  98. },
  99. ],
  100. });
  101. }
  102. catch {
  103. console.log('This is error');
  104. }
  105. });
  106. }
  107. }
  108. module.exports = BoltService;