|
|
@@ -8,34 +8,17 @@ const urljoin = require('url-join');
|
|
|
/* eslint-disable no-use-before-define */
|
|
|
|
|
|
module.exports = function(crowi) {
|
|
|
- const Slack = require('slack-node');
|
|
|
- const { configManager } = crowi;
|
|
|
+ const { WebClient, LogLevel } = require('@slack/web-api');
|
|
|
|
|
|
+ const { configManager } = crowi;
|
|
|
const slack = {};
|
|
|
|
|
|
- const postWithIwh = function(messageObj) {
|
|
|
+ const postWithSlackBot = function(messageObj) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
- const client = new Slack();
|
|
|
- client.setWebhook(configManager.getConfig('notification', 'slack:incomingWebhookUrl'));
|
|
|
- client.webhook(messageObj, (err, res) => {
|
|
|
- if (err) {
|
|
|
- debug('Post error', err, res);
|
|
|
- debug('Sent data to slack is:', messageObj);
|
|
|
- return reject(err);
|
|
|
- }
|
|
|
- resolve(res);
|
|
|
+ const client = new WebClient(configManager.getConfig('crowi', 'slackbot:token'), {
|
|
|
+ logLevel: LogLevel.DEBUG,
|
|
|
});
|
|
|
- });
|
|
|
- };
|
|
|
-
|
|
|
- const postWithWebApi = function(messageObj) {
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- const client = new Slack(configManager.getConfig('notification', 'slack:token'));
|
|
|
- // stringify attachments
|
|
|
- if (messageObj.attachments != null) {
|
|
|
- messageObj.attachments = JSON.stringify(messageObj.attachments);
|
|
|
- }
|
|
|
- client.api('chat.postMessage', messageObj, (err, res) => {
|
|
|
+ client.chat.postMessage(messageObj, (err, res) => {
|
|
|
if (err) {
|
|
|
debug('Post error', err, res);
|
|
|
debug('Sent data to slack is:', messageObj);
|
|
|
@@ -105,7 +88,7 @@ module.exports = function(crowi) {
|
|
|
return body;
|
|
|
};
|
|
|
|
|
|
- const prepareSlackMessageForPage = function(page, user, channel, updateType, previousRevision) {
|
|
|
+ slack.prepareSlackMessageForPage = (page, user, channel, updateType, previousRevision) => {
|
|
|
const appTitle = crowi.appService.getAppTitle();
|
|
|
const url = crowi.appService.getSiteUrl();
|
|
|
let body = page.revision.body;
|
|
|
@@ -141,7 +124,7 @@ module.exports = function(crowi) {
|
|
|
return message;
|
|
|
};
|
|
|
|
|
|
- const prepareSlackMessageForComment = function(comment, user, channel, path) {
|
|
|
+ slack.prepareSlackMessageForComment = (comment, user, channel, path) => {
|
|
|
const appTitle = crowi.appService.getAppTitle();
|
|
|
const url = crowi.appService.getSiteUrl();
|
|
|
const body = prepareAttachmentTextForComment(comment);
|
|
|
@@ -175,7 +158,7 @@ module.exports = function(crowi) {
|
|
|
* @param {string} attachmentBody
|
|
|
* @param {string} slackChannel
|
|
|
*/
|
|
|
- const prepareSlackMessageForGlobalNotification = async(messageBody, attachmentBody, slackChannel) => {
|
|
|
+ slack.prepareSlackMessageForGlobalNotification = async(messageBody, attachmentBody, slackChannel) => {
|
|
|
const appTitle = crowi.appService.getAppTitle();
|
|
|
|
|
|
const attachment = {
|
|
|
@@ -217,48 +200,25 @@ module.exports = function(crowi) {
|
|
|
return text;
|
|
|
};
|
|
|
|
|
|
- // slack.post = function (channel, message, opts) {
|
|
|
slack.postPage = (page, user, channel, updateType, previousRevision) => {
|
|
|
- const messageObj = prepareSlackMessageForPage(page, user, channel, updateType, previousRevision);
|
|
|
+ const messageObj = slack.prepareSlackMessageForPage(page, user, channel, updateType, previousRevision);
|
|
|
|
|
|
return slackPost(messageObj);
|
|
|
};
|
|
|
|
|
|
slack.postComment = (comment, user, channel, path) => {
|
|
|
- const messageObj = prepareSlackMessageForComment(comment, user, channel, path);
|
|
|
+ const messageObj = slack.prepareSlackMessageForComment(comment, user, channel, path);
|
|
|
|
|
|
return slackPost(messageObj);
|
|
|
};
|
|
|
|
|
|
slack.sendGlobalNotification = async(messageBody, attachmentBody, slackChannel) => {
|
|
|
- const messageObj = await prepareSlackMessageForGlobalNotification(messageBody, attachmentBody, slackChannel);
|
|
|
-
|
|
|
+ const messageObj = await slack.prepareSlackMessageForGlobalNotification(messageBody, attachmentBody, slackChannel);
|
|
|
return slackPost(messageObj);
|
|
|
};
|
|
|
|
|
|
const slackPost = (messageObj) => {
|
|
|
- // when incoming Webhooks is prioritized
|
|
|
- if (configManager.getConfig('notification', 'slack:isIncomingWebhookPrioritized')) {
|
|
|
- if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
|
|
|
- debug('posting message with IncomingWebhook');
|
|
|
- return postWithIwh(messageObj);
|
|
|
- }
|
|
|
- if (configManager.getConfig('notification', 'slack:token')) {
|
|
|
- debug('posting message with Web API');
|
|
|
- return postWithWebApi(messageObj);
|
|
|
- }
|
|
|
- }
|
|
|
- // else
|
|
|
- else {
|
|
|
- if (configManager.getConfig('notification', 'slack:token')) {
|
|
|
- debug('posting message with Web API');
|
|
|
- return postWithWebApi(messageObj);
|
|
|
- }
|
|
|
- if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
|
|
|
- debug('posting message with IncomingWebhook');
|
|
|
- return postWithIwh(messageObj);
|
|
|
- }
|
|
|
- }
|
|
|
+ return postWithSlackBot(messageObj);
|
|
|
};
|
|
|
|
|
|
return slack;
|