|
@@ -5,55 +5,32 @@
|
|
|
module.exports = function(crowi) {
|
|
module.exports = function(crowi) {
|
|
|
'use strict';
|
|
'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'),
|
|
Config = crowi.model('Config'),
|
|
|
- Botkit = require('botkit'),
|
|
|
|
|
|
|
+ SlackWebClient = require('@slack/client').WebClient,
|
|
|
sprintf = require('sprintf'),
|
|
sprintf = require('sprintf'),
|
|
|
- bot = null,
|
|
|
|
|
slack = {};
|
|
slack = {};
|
|
|
- slack.controller = undefined;
|
|
|
|
|
|
|
|
|
|
- slack.createBot = function() {
|
|
|
|
|
- // alreay created
|
|
|
|
|
- if (bot) {
|
|
|
|
|
- return bot;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ slack.client = undefined;
|
|
|
|
|
|
|
|
- var config = crowi.getConfig();
|
|
|
|
|
-
|
|
|
|
|
- if (!slack.controller) {
|
|
|
|
|
- slack.configureSlackApp();
|
|
|
|
|
|
|
+ slack.getClient = function() {
|
|
|
|
|
+ // alreay created
|
|
|
|
|
+ if (slack.client) {
|
|
|
|
|
+ return slack.client;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (!slack.controller) {
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const config = crowi.getConfig();
|
|
|
|
|
|
|
|
|
|
+ let client;
|
|
|
if (Config.hasSlackToken(config)) {
|
|
if (Config.hasSlackToken(config)) {
|
|
|
- bot = slack.controller.spawn({token: config.notification['slack:token']});
|
|
|
|
|
- } else {
|
|
|
|
|
- bot = slack.controller.spawn();
|
|
|
|
|
- }
|
|
|
|
|
- return bot;
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- slack.configureSlackApp = function ()
|
|
|
|
|
- {
|
|
|
|
|
- var config = crowi.getConfig();
|
|
|
|
|
- if (Config.hasSlackConfig(config)) {
|
|
|
|
|
- slack.controller = Botkit.slackbot();
|
|
|
|
|
- slack.controller.configureSlackApp({
|
|
|
|
|
- clientId: config.notification['slack:clientId'],
|
|
|
|
|
- clientSecret: config.notification['slack:clientSecret'],
|
|
|
|
|
- redirectUri: slack.getSlackAuthCallbackUrl(),
|
|
|
|
|
- scopes: ['chat:write:bot']
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- return true;
|
|
|
|
|
|
|
+ client = new SlackWebClient(config.notification['slack:token']);
|
|
|
|
|
+ slack.client = client;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return slack.client;
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
// hmmm
|
|
// hmmm
|
|
|
slack.getSlackAuthCallbackUrl = function()
|
|
slack.getSlackAuthCallbackUrl = function()
|
|
@@ -65,22 +42,35 @@ module.exports = function(crowi) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
slack.getAuthorizeURL = function () {
|
|
slack.getAuthorizeURL = function () {
|
|
|
- if (!slack.controller) {
|
|
|
|
|
- slack.configureSlackApp();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const config = crowi.getConfig();
|
|
|
|
|
+ if (Config.hasSlackConfig(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.controller) {
|
|
|
|
|
return '';
|
|
return '';
|
|
|
}
|
|
}
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ 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 slack.controller.getAuthorizeURL();
|
|
|
|
|
|
|
+ return client.oauth.access(clientId, clientSecret, code, {redirect_uri: redirectUri});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- slack.post = function (message) {
|
|
|
|
|
- var bot = slack.createBot();
|
|
|
|
|
|
|
+ slack.post = function (channel, message, opts) {
|
|
|
|
|
+ const client = slack.getClient();
|
|
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
return new Promise(function(resolve, reject) {
|
|
|
- bot.api.chat.postMessage(message, function(err, res) {
|
|
|
|
|
|
|
+ client.chat.postMessage(channel, message, opts, function(err, res) {
|
|
|
if (err) {
|
|
if (err) {
|
|
|
debug('Post error', err, res);
|
|
debug('Post error', err, res);
|
|
|
debug('Sent data to slack is:', message);
|
|
debug('Sent data to slack is:', message);
|
|
@@ -179,12 +169,15 @@ module.exports = function(crowi) {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
slack.getSlackMessageText = function(path, user, updateType) {
|
|
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') {
|
|
if (updateType == 'create') {
|
|
|
- text = sprintf(':white_check_mark: %s created a new page! %s', user.username, path);
|
|
|
|
|
|
|
+ text = sprintf(':white_check_mark: %s created a new page! %s', user.username, pageUrl);
|
|
|
} else {
|
|
} else {
|
|
|
- text = sprintf(':up: %s updated %s', user.username, path);
|
|
|
|
|
|
|
+ text = sprintf(':up: %s updated %s', user.username, pageUrl);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
return text;
|