slack-legacy.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { IncomingWebhook } from '@slack/webhook';
  2. import { WebClient } from '@slack/web-api';
  3. import loggerFactory from '~/utils/logger';
  4. const logger = loggerFactory('growi:util:slack-legacy');
  5. module.exports = function(crowi) {
  6. const { configManager } = crowi;
  7. const slackUtilLegacy = {};
  8. const postWithIwh = async(messageObj) => {
  9. const webhook = new IncomingWebhook(configManager.getConfig('notification', 'slack:incomingWebhookUrl'));
  10. try {
  11. await webhook.send(messageObj);
  12. }
  13. catch (error) {
  14. logger.debug('Post error', error);
  15. logger.debug('Sent data to slack is:', messageObj);
  16. throw error;
  17. }
  18. };
  19. const postWithWebApi = async(messageObj) => {
  20. const client = new WebClient(configManager.getConfig('notification', 'slack:token'));
  21. try {
  22. await client.chat.postMessage(messageObj);
  23. }
  24. catch (error) {
  25. logger.debug('Post error', error);
  26. logger.debug('Sent data to slack is:', messageObj);
  27. throw error;
  28. }
  29. };
  30. slackUtilLegacy.postMessage = async(messageObj) => {
  31. // when incoming Webhooks is prioritized
  32. if (configManager.getConfig('notification', 'slack:isIncomingWebhookPrioritized')) {
  33. if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
  34. logger.debug('posting message with IncomingWebhook');
  35. return postWithIwh(messageObj);
  36. }
  37. if (configManager.getConfig('notification', 'slack:token')) {
  38. logger.debug('posting message with Web API');
  39. return postWithWebApi(messageObj);
  40. }
  41. }
  42. // else
  43. else {
  44. if (configManager.getConfig('notification', 'slack:token')) {
  45. logger.debug('posting message with Web API');
  46. return postWithWebApi(messageObj);
  47. }
  48. if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
  49. logger.debug('posting message with IncomingWebhook');
  50. return postWithIwh(messageObj);
  51. }
  52. }
  53. };
  54. return slackUtilLegacy;
  55. };