slack-legacy.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. const debug = require('debug')('growi:util:slack');
  2. // const slack = require('./slack');
  3. /**
  4. * slack
  5. */
  6. /* eslint-disable no-use-before-define */
  7. module.exports = function(crowi) {
  8. const Slack = require('slack-node');
  9. const { configManager } = crowi;
  10. const slack = crowi.getSlack();
  11. const slackLegacy = {};
  12. const postWithIwh = function(messageObj) {
  13. return new Promise((resolve, reject) => {
  14. const client = new Slack();
  15. client.setWebhook(configManager.getConfig('notification', 'slack:incomingWebhookUrl'));
  16. client.webhook(messageObj, (err, res) => {
  17. if (err) {
  18. debug('Post error', err, res);
  19. debug('Sent data to slack is:', messageObj);
  20. return reject(err);
  21. }
  22. resolve(res);
  23. });
  24. });
  25. };
  26. const postWithWebApi = function(messageObj) {
  27. return new Promise((resolve, reject) => {
  28. const client = new Slack(configManager.getConfig('notification', 'slack:token'));
  29. // stringify attachments
  30. if (messageObj.attachments != null) {
  31. messageObj.attachments = JSON.stringify(messageObj.attachments);
  32. }
  33. client.api('chat.postMessage', messageObj, (err, res) => {
  34. if (err) {
  35. debug('Post error', err, res);
  36. debug('Sent data to slack is:', messageObj);
  37. return reject(err);
  38. }
  39. resolve(res);
  40. });
  41. });
  42. };
  43. // slackLegacy.post = function (channel, message, opts) {
  44. slackLegacy.postPage = (page, user, channel, updateType, previousRevision) => {
  45. const messageObj = slack.prepareSlackMessageForPage(page, user, channel, updateType, previousRevision);
  46. return slackPost(messageObj);
  47. };
  48. slackLegacy.postComment = (comment, user, channel, path) => {
  49. const messageObj = slack.prepareSlackMessageForComment(comment, user, channel, path);
  50. return slackPost(messageObj);
  51. };
  52. slackLegacy.sendGlobalNotification = async(messageBody, attachmentBody, slackChannel) => {
  53. const messageObj = await slack.prepareSlackMessageForGlobalNotification(messageBody, attachmentBody, slackChannel);
  54. return slackPost(messageObj);
  55. };
  56. const slackPost = (messageObj) => {
  57. // when incoming Webhooks is prioritized
  58. if (configManager.getConfig('notification', 'slack:isIncomingWebhookPrioritized')) {
  59. if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
  60. debug('posting message with IncomingWebhook');
  61. return postWithIwh(messageObj);
  62. }
  63. if (configManager.getConfig('notification', 'slack:token')) {
  64. debug('posting message with Web API');
  65. return postWithWebApi(messageObj);
  66. }
  67. }
  68. // else
  69. else {
  70. if (configManager.getConfig('notification', 'slack:token')) {
  71. debug('posting message with Web API');
  72. return postWithWebApi(messageObj);
  73. }
  74. if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
  75. debug('posting message with IncomingWebhook');
  76. return postWithIwh(messageObj);
  77. }
  78. }
  79. };
  80. return slackLegacy;
  81. };