bolt.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 token = process.env.SLACK_BOT_TOKEN;
  38. const signingSecret = process.env.SLACK_SIGNING_SECRET;
  39. const client = new WebClient(token, { logLevel: LogLevel.DEBUG });
  40. const channelId = 'C01JZJP1J58';
  41. const userId = 'U015018DXL3';
  42. this.client = client;
  43. this.channelId = channelId;
  44. this.userId = userId;
  45. if (token != null || signingSecret != null) {
  46. logger.debug('TwitterStrategy: setup is done');
  47. this.bolt = new App({
  48. token,
  49. signingSecret,
  50. receiver: this.receiver,
  51. });
  52. this.init();
  53. }
  54. }
  55. init() {
  56. // Example of listening for event
  57. // See. https://github.com/slackapi/bolt-js#listening-for-events
  58. // or https://slack.dev/bolt-js/concepts#basic
  59. this.bolt.command('/growi-bot', async({ command, ack, say }) => { // demo
  60. await say('Hello');
  61. });
  62. // The echo command simply echoes on command
  63. this.bolt.command('/echo', async({ command, ack, say }) => {
  64. // Acknowledge command request
  65. await ack();
  66. await say(`${command.text}`);
  67. });
  68. // TODO check if firstArg is the supported command(like "search")
  69. this.bolt.command('/growi', async({ command }) => {
  70. const inputSlack = command.text.split(' ');
  71. const firstArg = inputSlack[0];
  72. const secondArg = inputSlack[1];
  73. let searchResults;
  74. if (firstArg === 'search') {
  75. const { searchService } = this.crowi;
  76. const option = { limit: 10 };
  77. searchResults = await searchService.searchKeyword(secondArg, null, {}, option);
  78. }
  79. // TODO impl try-catch
  80. try {
  81. const result = await this.client.chat.postEphemeral({
  82. channel: this.channelId,
  83. user: this.userId,
  84. text: searchResults,
  85. });
  86. console.log(result);
  87. }
  88. catch {
  89. console.log('errorだよ');
  90. }
  91. });
  92. }
  93. }
  94. module.exports = BoltService;