bolt.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. class BoltService {
  33. constructor(crowi) {
  34. this.crowi = crowi;
  35. this.receiver = new BoltReciever();
  36. const token = process.env.SLACK_BOT_TOKEN;
  37. const signingSecret = process.env.SLACK_SIGNING_SECRET;
  38. if (token != null || signingSecret != null) {
  39. logger.debug('TwitterStrategy: setup is done');
  40. this.bolt = new App({
  41. token,
  42. signingSecret,
  43. receiver: this.receiver,
  44. });
  45. this.init();
  46. }
  47. }
  48. init() {
  49. // Example of listening for event
  50. // See. https://github.com/slackapi/bolt-js#listening-for-events
  51. // or https://slack.dev/bolt-js/concepts#basic
  52. this.bolt.command('/growi-bot', async({ command, ack, say }) => { // demo
  53. await say('Hello');
  54. });
  55. // The echo command simply echoes on command
  56. this.bolt.command('/echo', async({ command, ack, say }) => {
  57. // Acknowledge command request
  58. await ack();
  59. await say(`${command.text}`);
  60. });
  61. }
  62. }
  63. module.exports = BoltService;