|
|
@@ -11,19 +11,37 @@ module.exports = function(crowi) {
|
|
|
Slack = require('slack-node'),
|
|
|
slack = {};
|
|
|
|
|
|
- const postWithIwh = function(messageObj, callback) {
|
|
|
- const client = new Slack();
|
|
|
- client.setWebhook(config.notification['slack:incomingWebhookUrl']);
|
|
|
- client.webhook(messageObj, callback);
|
|
|
+ const postWithIwh = function(messageObj) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ const client = new Slack();
|
|
|
+ client.setWebhook(config.notification['slack:incomingWebhookUrl']);
|
|
|
+ client.webhook(messageObj, function(err, res) {
|
|
|
+ if (err) {
|
|
|
+ debug('Post error', err, res);
|
|
|
+ debug('Sent data to slack is:', messageObj);
|
|
|
+ return reject(err);
|
|
|
+ }
|
|
|
+ resolve(res);
|
|
|
+ });
|
|
|
+ });
|
|
|
};
|
|
|
|
|
|
- const postWithWebApi = function(messageObj, callback) {
|
|
|
- const client = new Slack(config.notification['slack:token']);
|
|
|
- // stringify attachments
|
|
|
- if (messageObj.attachments != null) {
|
|
|
- messageObj.attachments = JSON.stringify(messageObj.attachments);
|
|
|
- }
|
|
|
- client.api('chat.postMessage', messageObj, callback);
|
|
|
+ const postWithWebApi = function(messageObj) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ const client = new Slack(config.notification['slack:token']);
|
|
|
+ // stringify attachments
|
|
|
+ if (messageObj.attachments != null) {
|
|
|
+ messageObj.attachments = JSON.stringify(messageObj.attachments);
|
|
|
+ }
|
|
|
+ client.api('chat.postMessage', messageObj, function(err, res) {
|
|
|
+ if (err) {
|
|
|
+ debug('Post error', err, res);
|
|
|
+ debug('Sent data to slack is:', messageObj);
|
|
|
+ return reject(err);
|
|
|
+ }
|
|
|
+ resolve(res);
|
|
|
+ });
|
|
|
+ });
|
|
|
};
|
|
|
|
|
|
const convertMarkdownToMrkdwn = function(body) {
|
|
|
@@ -182,52 +200,38 @@ module.exports = function(crowi) {
|
|
|
slack.postPage = (page, user, channel, updateType, previousRevision) => {
|
|
|
const messageObj = prepareSlackMessageForPage(page, user, channel, updateType, previousRevision);
|
|
|
|
|
|
- return promisifiedSlackPost(messageObj);
|
|
|
+ return slackPost(messageObj);
|
|
|
};
|
|
|
|
|
|
slack.postComment = (comment, user, channel, path) => {
|
|
|
const messageObj = prepareSlackMessageForComment(comment, user, channel, path);
|
|
|
|
|
|
- return promisifiedSlackPost(messageObj);
|
|
|
+ return slackPost(messageObj);
|
|
|
};
|
|
|
|
|
|
- const promisifiedSlackPost = (messageObj) => {
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- // define callback function for Promise
|
|
|
- const callback = function(err, res) {
|
|
|
- if (err) {
|
|
|
- debug('Post error', err, res);
|
|
|
- debug('Sent data to slack is:', messageObj);
|
|
|
- return reject(err);
|
|
|
- }
|
|
|
- resolve(res);
|
|
|
- };
|
|
|
-
|
|
|
- // when incoming Webhooks is prioritized
|
|
|
- if (Config.isIncomingWebhookPrioritized(config)) {
|
|
|
- if (Config.hasSlackIwhUrl(config)) {
|
|
|
- debug('posting message with IncomingWebhook');
|
|
|
- postWithIwh(messageObj, callback);
|
|
|
- }
|
|
|
- else if (Config.hasSlackToken(config)) {
|
|
|
- debug('posting message with Web API');
|
|
|
- postWithWebApi(messageObj, callback);
|
|
|
- }
|
|
|
+ const slackPost = (messageObj) => {
|
|
|
+ // when incoming Webhooks is prioritized
|
|
|
+ if (Config.isIncomingWebhookPrioritized(config)) {
|
|
|
+ if (Config.hasSlackIwhUrl(config)) {
|
|
|
+ debug('posting message with IncomingWebhook');
|
|
|
+ return postWithIwh(messageObj);
|
|
|
}
|
|
|
- // else
|
|
|
- else {
|
|
|
- if (Config.hasSlackToken(config)) {
|
|
|
- debug('posting message with Web API');
|
|
|
- postWithWebApi(messageObj, callback);
|
|
|
- }
|
|
|
- else if (Config.hasSlackIwhUrl(config)) {
|
|
|
- debug('posting message with IncomingWebhook');
|
|
|
- postWithIwh(messageObj, callback);
|
|
|
- }
|
|
|
+ else if (Config.hasSlackToken(config)) {
|
|
|
+ debug('posting message with Web API');
|
|
|
+ return postWithWebApi(messageObj);
|
|
|
}
|
|
|
-
|
|
|
- resolve();
|
|
|
- });
|
|
|
+ }
|
|
|
+ // else
|
|
|
+ else {
|
|
|
+ if (Config.hasSlackToken(config)) {
|
|
|
+ debug('posting message with Web API');
|
|
|
+ return postWithWebApi(messageObj);
|
|
|
+ }
|
|
|
+ else if (Config.hasSlackIwhUrl(config)) {
|
|
|
+ debug('posting message with IncomingWebhook');
|
|
|
+ return postWithIwh(messageObj);
|
|
|
+ }
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
return slack;
|