|
@@ -8,43 +8,86 @@ module.exports = function(crowi) {
|
|
|
var debug = require('debug')('crowi:util:slack'),
|
|
var debug = require('debug')('crowi:util:slack'),
|
|
|
Config = crowi.model('Config'),
|
|
Config = crowi.model('Config'),
|
|
|
Botkit = require('botkit'),
|
|
Botkit = require('botkit'),
|
|
|
- bot = null,
|
|
|
|
|
|
|
+ isDebugSlackbot = false,
|
|
|
|
|
+ appBot = null, // for Slack App
|
|
|
|
|
+ iwhBot = null, // for Slack Incoming Webhooks
|
|
|
slack = {};
|
|
slack = {};
|
|
|
- slack.controller = undefined;
|
|
|
|
|
|
|
+ slack.appController = undefined; // for Slack App
|
|
|
|
|
+ slack.iwhController = undefined; // for Slack Incoming Webhooks
|
|
|
|
|
|
|
|
- slack.createBot = function(isForce, isClearToken) {
|
|
|
|
|
- // alreay created
|
|
|
|
|
- if (!isForce && bot) {
|
|
|
|
|
- return bot;
|
|
|
|
|
|
|
+ isDebugSlackbot = true; // for debug
|
|
|
|
|
+
|
|
|
|
|
+ slack.getBot = function() {
|
|
|
|
|
+ var config = crowi.getConfig();
|
|
|
|
|
+
|
|
|
|
|
+ // 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();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ return false;
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ slack.initAppBot = function(isClearToken) {
|
|
|
var config = crowi.getConfig();
|
|
var config = crowi.getConfig();
|
|
|
|
|
|
|
|
- if (!slack.controller) {
|
|
|
|
|
|
|
+ if (!slack.appController) {
|
|
|
slack.configureSlackApp();
|
|
slack.configureSlackApp();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (!slack.controller) {
|
|
|
|
|
|
|
+ if (!slack.appController) {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (!isClearToken && Config.hasSlackToken(config)) {
|
|
if (!isClearToken && Config.hasSlackToken(config)) {
|
|
|
- bot = slack.controller.spawn({token: config.notification['slack:token']});
|
|
|
|
|
|
|
+ appBot = slack.appController.spawn({token: config.notification['slack:token']});
|
|
|
} else {
|
|
} else {
|
|
|
- bot = slack.controller.spawn();
|
|
|
|
|
|
|
+ appBot = slack.appController.spawn();
|
|
|
}
|
|
}
|
|
|
- return bot;
|
|
|
|
|
|
|
+ return appBot;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+ 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 ()
|
|
slack.configureSlackApp = function ()
|
|
|
{
|
|
{
|
|
|
var config = crowi.getConfig();
|
|
var config = crowi.getConfig();
|
|
|
- var isDebugSlackbot = false;
|
|
|
|
|
- isDebugSlackbot = true;
|
|
|
|
|
|
|
|
|
|
if (Config.hasSlackConfig(config)) {
|
|
if (Config.hasSlackConfig(config)) {
|
|
|
- slack.controller = Botkit.slackbot({debug: isDebugSlackbot});
|
|
|
|
|
- slack.controller.configureSlackApp({
|
|
|
|
|
|
|
+ slack.appController = Botkit.slackbot({debug: isDebugSlackbot});
|
|
|
|
|
+ slack.appController.configureSlackApp({
|
|
|
clientId: config.notification['slack:clientId'],
|
|
clientId: config.notification['slack:clientId'],
|
|
|
clientSecret: config.notification['slack:clientSecret'],
|
|
clientSecret: config.notification['slack:clientSecret'],
|
|
|
redirectUri: slack.getSlackAuthCallbackUrl(),
|
|
redirectUri: slack.getSlackAuthCallbackUrl(),
|
|
@@ -57,6 +100,18 @@ module.exports = function(crowi) {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ slack.configureSlackIwh = function ()
|
|
|
|
|
+ {
|
|
|
|
|
+ var config = crowi.getConfig();
|
|
|
|
|
+
|
|
|
|
|
+ if (Config.hasSlackIwhUrl(config)) {
|
|
|
|
|
+ slack.iwhController = Botkit.slackbot({debug: isDebugSlackbot});
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// hmmm
|
|
// hmmm
|
|
|
slack.getSlackAuthCallbackUrl = function()
|
|
slack.getSlackAuthCallbackUrl = function()
|
|
|
{
|
|
{
|
|
@@ -67,22 +122,39 @@ module.exports = function(crowi) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
slack.getAuthorizeURL = function () {
|
|
slack.getAuthorizeURL = function () {
|
|
|
- if (!slack.controller) {
|
|
|
|
|
|
|
+ if (!slack.appController) {
|
|
|
slack.configureSlackApp();
|
|
slack.configureSlackApp();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (!slack.controller) {
|
|
|
|
|
|
|
+ if (!slack.appController) {
|
|
|
return '';
|
|
return '';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return slack.controller.getAuthorizeURL();
|
|
|
|
|
|
|
+ return slack.appController.getAuthorizeURL();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
slack.post = function (message) {
|
|
slack.post = function (message) {
|
|
|
- var bot = slack.createBot();
|
|
|
|
|
|
|
+ var bot = slack.getBot();
|
|
|
|
|
+ let sendMethod = undefined;
|
|
|
|
|
+
|
|
|
|
|
+ // 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;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (sendMethod === undefined) {
|
|
|
|
|
+ debug(`sendMethod is undefined`);
|
|
|
|
|
+ return Promise.resolve();
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
return new Promise(function(resolve, reject) {
|
|
|
- bot.api.chat.postMessage(message, function(err, res) {
|
|
|
|
|
|
|
+ sendMethod(message, 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);
|