|
|
@@ -8,128 +8,23 @@ module.exports = function(crowi) {
|
|
|
const SLACK_URL = 'https://slack.com';
|
|
|
|
|
|
const debug = require('debug')('crowi:util:slack'),
|
|
|
+ config = crowi.getConfig(),
|
|
|
Config = crowi.model('Config'),
|
|
|
- SlackWebClient = require('@slack/client').WebClient,
|
|
|
- SlackIncomingWebhook = require('@slack/client').IncomingWebhook,
|
|
|
+ Slack = require('slack-node'),
|
|
|
slack = {};
|
|
|
|
|
|
- slack.client = undefined;
|
|
|
- slack.incomingWebhook = undefined;
|
|
|
-
|
|
|
- slack.getClient = function() {
|
|
|
- // alreay created
|
|
|
- if (slack.client) {
|
|
|
- return slack.client;
|
|
|
- }
|
|
|
-
|
|
|
- const config = crowi.getConfig();
|
|
|
-
|
|
|
- let client;
|
|
|
- if (Config.hasSlackToken(config)) {
|
|
|
- client = new SlackWebClient(config.notification['slack:token']);
|
|
|
- slack.client = client;
|
|
|
- }
|
|
|
-
|
|
|
- return slack.client;
|
|
|
- };
|
|
|
-
|
|
|
- // this is called to generate redirect_uri
|
|
|
- slack.getSlackAuthCallbackUrl = function()
|
|
|
- {
|
|
|
- var config = crowi.getConfig();
|
|
|
- // Web アクセスがきてないと app:url がセットされないので crowi.setupSlack 時にはできない
|
|
|
- // cli, bot 系作るときに問題なりそう
|
|
|
- return (config.crowi['app:url'] || '') + '/admin/notification/slackAuth';
|
|
|
+ const postWithIwh = function(messageObj, callback) {
|
|
|
+ const client = new Slack();
|
|
|
+ client.setWebhook(config.notification['slack:incomingWebhookUrl']);
|
|
|
+ client.webhook(messageObj, callback);
|
|
|
}
|
|
|
|
|
|
- // this is called to get the url for oauth screen
|
|
|
- slack.getAuthorizeURL = function () {
|
|
|
- const config = crowi.getConfig();
|
|
|
- if (Config.hasSlackWebClientConfig(config)) {
|
|
|
- const slackClientId = config.notification['slack:clientId'];
|
|
|
- const redirectUri = slack.getSlackAuthCallbackUrl();
|
|
|
-
|
|
|
- return `${SLACK_URL}/oauth/authorize?client_id=${slackClientId}&redirect_uri=${redirectUri}&scope=chat:write:bot`;
|
|
|
- } else {
|
|
|
-
|
|
|
- return '';
|
|
|
- }
|
|
|
+ const postWithWebApi = function(messageObj, callback) {
|
|
|
+ const client = new Slack(config.notification['slack:token']);
|
|
|
+ client.api('chat.postMessage', messageObj, callback);
|
|
|
}
|
|
|
|
|
|
- // this is called to get access token with code (oauth process)
|
|
|
- slack.getOauthAccessToken = function(code) {
|
|
|
-
|
|
|
- const client = new SlackWebClient();
|
|
|
-
|
|
|
- const config = crowi.getConfig();
|
|
|
- const clientId = config.notification['slack:clientId'];
|
|
|
- const clientSecret = config.notification['slack:clientSecret'];
|
|
|
- const redirectUri = slack.getSlackAuthCallbackUrl();
|
|
|
-
|
|
|
- return client.oauth.access(clientId, clientSecret, code, {redirect_uri: redirectUri});
|
|
|
- }
|
|
|
-
|
|
|
- slack.getIncomingWebhook = function() {
|
|
|
- // alreay created
|
|
|
- if (slack.incomingWebhook) {
|
|
|
- return slack.incomingWebhook;
|
|
|
- }
|
|
|
-
|
|
|
- const config = crowi.getConfig();
|
|
|
-
|
|
|
- let incomingWebhook;
|
|
|
- if (Config.hasSlackIwhUrl(config)) {
|
|
|
- incomingWebhook = new SlackIncomingWebhook(config.notification['slack:incomingWebhookUrl']);
|
|
|
- slack.incomingWebhook = incomingWebhook;
|
|
|
- }
|
|
|
-
|
|
|
- return slack.incomingWebhook;
|
|
|
- };
|
|
|
-
|
|
|
- slack.post = function (channel, message, opts) {
|
|
|
- const config = crowi.getConfig();
|
|
|
-
|
|
|
- return new Promise(function(resolve, reject) {
|
|
|
-
|
|
|
- // define callback function
|
|
|
- const callback = function(err, res) {
|
|
|
- if (err) {
|
|
|
- debug('Post error', err, res);
|
|
|
- debug('Sent data to slack is:', message);
|
|
|
- return reject(err);
|
|
|
- }
|
|
|
- resolve(res);
|
|
|
- };
|
|
|
-
|
|
|
- // when incoming Webhooks is prioritized
|
|
|
- if (Config.isIncomingWebhookPrioritized(config)) {
|
|
|
- if (Config.hasSlackIwhUrl(config)) {
|
|
|
- debug(`posting message with IncomingWebhook`);
|
|
|
- slack.getIncomingWebhook().send(opts, callback);
|
|
|
- }
|
|
|
- else if (Config.hasSlackToken(config)) {
|
|
|
- debug(`posting message with WebClient`);
|
|
|
- slack.getClient().chat.postMessage(channel, message, opts, callback);
|
|
|
- }
|
|
|
- }
|
|
|
- // else
|
|
|
- else {
|
|
|
- if (Config.hasSlackToken(config)) {
|
|
|
- debug(`posting message with WebClient`);
|
|
|
- slack.getClient().chat.postMessage(channel, message, opts, callback);
|
|
|
- }
|
|
|
- else if (Config.hasSlackIwhUrl(config)) {
|
|
|
- debug(`posting message with IncomingWebhook`);
|
|
|
- slack.getIncomingWebhook().send(opts, callback);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- resolve();
|
|
|
- });
|
|
|
- };
|
|
|
-
|
|
|
- slack.convertMarkdownToMrkdwn = function(body) {
|
|
|
- var config = crowi.getConfig();
|
|
|
+ const convertMarkdownToMrkdwn = function(body) {
|
|
|
var url = '';
|
|
|
if (config.crowi && config.crowi['app:url']) {
|
|
|
url = config.crowi['app:url'];
|
|
|
@@ -145,16 +40,16 @@ module.exports = function(crowi) {
|
|
|
return body;
|
|
|
};
|
|
|
|
|
|
- slack.prepareAttachmentTextForCreate = function(page, user) {
|
|
|
+ const prepareAttachmentTextForCreate = function(page, user) {
|
|
|
var body = page.revision.body;
|
|
|
if (body.length > 2000) {
|
|
|
body = body.substr(0, 2000) + '...';
|
|
|
}
|
|
|
|
|
|
- return this.convertMarkdownToMrkdwn(body);
|
|
|
+ return convertMarkdownToMrkdwn(body);
|
|
|
};
|
|
|
|
|
|
- slack.prepareAttachmentTextForUpdate = function(page, user, previousRevision) {
|
|
|
+ const prepareAttachmentTextForUpdate = function(page, user, previousRevision) {
|
|
|
var diff = require('diff');
|
|
|
var diffText = ''
|
|
|
|
|
|
@@ -179,15 +74,14 @@ module.exports = function(crowi) {
|
|
|
return diffText;
|
|
|
};
|
|
|
|
|
|
- slack.prepareSlackMessage = function(page, user, channel, updateType, previousRevision) {
|
|
|
- var config = crowi.getConfig();
|
|
|
+ const prepareSlackMessage = function(page, user, channel, updateType, previousRevision) {
|
|
|
var url = config.crowi['app:url'] || '';
|
|
|
var body = page.revision.body;
|
|
|
|
|
|
if (updateType == 'create') {
|
|
|
- body = this.prepareAttachmentTextForCreate(page, user);
|
|
|
+ body = prepareAttachmentTextForCreate(page, user);
|
|
|
} else {
|
|
|
- body = this.prepareAttachmentTextForUpdate(page, user, previousRevision);
|
|
|
+ body = prepareAttachmentTextForUpdate(page, user, previousRevision);
|
|
|
}
|
|
|
|
|
|
var attachment = {
|
|
|
@@ -207,16 +101,15 @@ module.exports = function(crowi) {
|
|
|
var message = {
|
|
|
channel: '#' + channel,
|
|
|
username: Config.appTitle(config),
|
|
|
- text: this.getSlackMessageText(page.path, user, updateType),
|
|
|
+ text: getSlackMessageText(page.path, user, updateType),
|
|
|
attachments: [attachment],
|
|
|
};
|
|
|
|
|
|
return message;
|
|
|
};
|
|
|
|
|
|
- slack.getSlackMessageText = function(path, user, updateType) {
|
|
|
+ const getSlackMessageText = function(path, user, updateType) {
|
|
|
let text;
|
|
|
- const config = crowi.getConfig();
|
|
|
const url = config.crowi['app:url'] || '';
|
|
|
|
|
|
const pageUrl = `<${url}${path}|${path}>`;
|
|
|
@@ -229,5 +122,80 @@ module.exports = function(crowi) {
|
|
|
return text;
|
|
|
};
|
|
|
|
|
|
+ // this is called to generate redirect_uri
|
|
|
+ slack.getSlackAuthCallbackUrl = function()
|
|
|
+ {
|
|
|
+ // Web アクセスがきてないと app:url がセットされないので crowi.setupSlack 時にはできない
|
|
|
+ // cli, bot 系作るときに問題なりそう
|
|
|
+ return (config.crowi['app:url'] || '') + '/admin/notification/slackAuth';
|
|
|
+ }
|
|
|
+
|
|
|
+ // this is called to get the url for oauth screen
|
|
|
+ slack.getAuthorizeURL = function () {
|
|
|
+ if (Config.hasSlackWebClientConfig(config)) {
|
|
|
+ const slackClientId = config.notification['slack:clientId'];
|
|
|
+ const redirectUri = slack.getSlackAuthCallbackUrl();
|
|
|
+
|
|
|
+ return `${SLACK_URL}/oauth/authorize?client_id=${slackClientId}&redirect_uri=${redirectUri}&scope=chat:write:bot`;
|
|
|
+ } else {
|
|
|
+
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // this is called to get access token with code (oauth process)
|
|
|
+ slack.getOauthAccessToken = function(code) {
|
|
|
+
|
|
|
+ const client = new SlackWebClient();
|
|
|
+
|
|
|
+ const clientId = config.notification['slack:clientId'];
|
|
|
+ const clientSecret = config.notification['slack:clientSecret'];
|
|
|
+ const redirectUri = slack.getSlackAuthCallbackUrl();
|
|
|
+
|
|
|
+ return client.oauth.access(clientId, clientSecret, code, {redirect_uri: redirectUri});
|
|
|
+ }
|
|
|
+
|
|
|
+ // slack.post = function (channel, message, opts) {
|
|
|
+ slack.post = (page, user, channel, updateType, previousRevision) => {
|
|
|
+ const messageObj = prepareSlackMessage(page, user, channel, updateType, previousRevision);
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ resolve();
|
|
|
+ });
|
|
|
+ };
|
|
|
+
|
|
|
return slack;
|
|
|
};
|