|
|
@@ -5,114 +5,35 @@
|
|
|
module.exports = function(crowi) {
|
|
|
'use strict';
|
|
|
|
|
|
- var debug = require('debug')('crowi:util:slack'),
|
|
|
+ const SLACK_URL = 'https://slack.com';
|
|
|
+
|
|
|
+ const debug = require('debug')('crowi:util:slack'),
|
|
|
Config = crowi.model('Config'),
|
|
|
- Botkit = require('botkit'),
|
|
|
- isDebugSlackbot = false,
|
|
|
- appBot = null, // for Slack App
|
|
|
- iwhBot = null, // for Slack Incoming Webhooks
|
|
|
+ SlackWebClient = require('@slack/client').WebClient,
|
|
|
+ SlackIncomingWebhook = require('@slack/client').IncomingWebhook,
|
|
|
slack = {};
|
|
|
- slack.appController = undefined; // for Slack App
|
|
|
- slack.iwhController = undefined; // for Slack Incoming Webhooks
|
|
|
-
|
|
|
- // isDebugSlackbot = true; // for debug
|
|
|
|
|
|
- slack.getBot = function() {
|
|
|
- var config = crowi.getConfig();
|
|
|
+ slack.client = undefined;
|
|
|
+ slack.incomingWebhook = undefined;
|
|
|
|
|
|
- // when incoming Webhooks is prioritized
|
|
|
- if (Config.isIncomingWebhookPrioritized(config)) {
|
|
|
- if (Config.hasSlackIwhUrl(config)) {
|
|
|
- return iwhBot || slack.initIwhBot();
|
|
|
- }
|
|
|
- else if (Config.hasSlackToken(config)) {
|
|
|
- return appBot || slack.initAppBot();
|
|
|
- }
|
|
|
- }
|
|
|
- // else
|
|
|
- else {
|
|
|
- if (Config.hasSlackToken(config)) {
|
|
|
- return appBot || slack.initAppBot();
|
|
|
- }
|
|
|
- else if (Config.hasSlackIwhUrl(config)) {
|
|
|
- return iwhBot || slack.initIwhBot();
|
|
|
- }
|
|
|
+ slack.getClient = function() {
|
|
|
+ // alreay created
|
|
|
+ if (slack.client) {
|
|
|
+ return slack.client;
|
|
|
}
|
|
|
|
|
|
- return false;
|
|
|
- };
|
|
|
+ const config = crowi.getConfig();
|
|
|
|
|
|
- slack.initAppBot = function(isClearToken) {
|
|
|
- var config = crowi.getConfig();
|
|
|
-
|
|
|
- if (!slack.appController) {
|
|
|
- slack.configureSlackApp();
|
|
|
- }
|
|
|
-
|
|
|
- if (!slack.appController) {
|
|
|
- return false;
|
|
|
+ let client;
|
|
|
+ if (Config.hasSlackToken(config)) {
|
|
|
+ client = new SlackWebClient(config.notification['slack:token']);
|
|
|
+ slack.client = client;
|
|
|
}
|
|
|
|
|
|
- if (!isClearToken && Config.hasSlackToken(config)) {
|
|
|
- appBot = slack.appController.spawn({token: config.notification['slack:token']});
|
|
|
- } else {
|
|
|
- appBot = slack.appController.spawn();
|
|
|
- }
|
|
|
- return appBot;
|
|
|
+ return slack.client;
|
|
|
};
|
|
|
|
|
|
- slack.initIwhBot = function() {
|
|
|
- var config = crowi.getConfig();
|
|
|
-
|
|
|
- if (!slack.iwhController) {
|
|
|
- slack.configureSlackIwh();
|
|
|
- }
|
|
|
-
|
|
|
- if (!slack.iwhController) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- iwhBot = slack.iwhController.spawn({
|
|
|
- incoming_webhook: {
|
|
|
- url: config.notification['slack:incomingWebhookUrl']
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- return iwhBot;
|
|
|
- }
|
|
|
-
|
|
|
- slack.configureSlackApp = function ()
|
|
|
- {
|
|
|
- var config = crowi.getConfig();
|
|
|
-
|
|
|
- if (Config.hasSlackAppConfig(config)) {
|
|
|
- slack.appController = Botkit.slackbot({debug: isDebugSlackbot});
|
|
|
- slack.appController.configureSlackApp({
|
|
|
- clientId: config.notification['slack:clientId'],
|
|
|
- clientSecret: config.notification['slack:clientSecret'],
|
|
|
- redirectUri: slack.getSlackAuthCallbackUrl(),
|
|
|
- scopes: ['chat:write:bot']
|
|
|
- });
|
|
|
-
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- slack.configureSlackIwh = function ()
|
|
|
- {
|
|
|
- var config = crowi.getConfig();
|
|
|
-
|
|
|
- if (Config.hasSlackIwhUrl(config)) {
|
|
|
- slack.iwhController = Botkit.slackbot({debug: isDebugSlackbot});
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // hmmm
|
|
|
+ // this is called to generate redirect_uri
|
|
|
slack.getSlackAuthCallbackUrl = function()
|
|
|
{
|
|
|
var config = crowi.getConfig();
|
|
|
@@ -121,48 +42,89 @@ module.exports = function(crowi) {
|
|
|
return (config.crowi['app:url'] || '') + '/admin/notification/slackAuth';
|
|
|
}
|
|
|
|
|
|
+ // this is called to get the url for oauth screen
|
|
|
slack.getAuthorizeURL = function () {
|
|
|
- if (!slack.appController) {
|
|
|
- slack.configureSlackApp();
|
|
|
- }
|
|
|
+ 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 {
|
|
|
|
|
|
- if (!slack.appController) {
|
|
|
return '';
|
|
|
}
|
|
|
-
|
|
|
- return slack.appController.getAuthorizeURL();
|
|
|
}
|
|
|
|
|
|
- slack.post = function (message) {
|
|
|
- var bot = slack.getBot();
|
|
|
- let sendMethod = undefined;
|
|
|
+ // this is called to get access token with code (oauth process)
|
|
|
+ slack.getOauthAccessToken = function(code) {
|
|
|
|
|
|
- // use Slack App
|
|
|
- if (bot === appBot) {
|
|
|
- debug(`sendMethod: bot.api.chat.postMessage`);
|
|
|
- sendMethod = bot.api.chat.postMessage;
|
|
|
- }
|
|
|
- // use Slack Incoming Webhooks
|
|
|
- else if (bot === iwhBot) {
|
|
|
- debug(`sendMethod: bot.sendWebhook`);
|
|
|
- sendMethod = bot.sendWebhook;
|
|
|
+ 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;
|
|
|
}
|
|
|
|
|
|
- if (sendMethod === undefined) {
|
|
|
- debug(`sendMethod is undefined`);
|
|
|
- return Promise.resolve();
|
|
|
+ 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) {
|
|
|
- sendMethod(message, function(err, res) {
|
|
|
+
|
|
|
+ // 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();
|
|
|
});
|
|
|
};
|
|
|
|
|
|
@@ -244,7 +206,7 @@ module.exports = function(crowi) {
|
|
|
|
|
|
var message = {
|
|
|
channel: '#' + channel,
|
|
|
- username: 'Crowi',
|
|
|
+ username: config.crowi['app:title'],
|
|
|
text: this.getSlackMessageText(page.path, user, updateType),
|
|
|
attachments: [attachment],
|
|
|
};
|
|
|
@@ -253,12 +215,15 @@ module.exports = function(crowi) {
|
|
|
};
|
|
|
|
|
|
slack.getSlackMessageText = function(path, user, updateType) {
|
|
|
- var text;
|
|
|
+ let text;
|
|
|
+ const config = crowi.getConfig();
|
|
|
+ const url = config.crowi['app:url'] || '';
|
|
|
|
|
|
+ const pageUrl = `<${url}${path}|${path}>`;
|
|
|
if (updateType == 'create') {
|
|
|
- text = `:white_check_mark: ${user.username} created a new page! ${path}`;
|
|
|
+ text = `:white_check_mark: ${user.username} created a new page! ${pageUrl}`;
|
|
|
} else {
|
|
|
- text = `:up: ${user.username} updated ${path}`;
|
|
|
+ text = `:up: ${user.username} updated ${pageUrl}`;
|
|
|
}
|
|
|
|
|
|
return text;
|