Explorar el Código

impl incoming webhooks integration

Yuki Takei hace 8 años
padre
commit
503b7e1286
Se han modificado 5 ficheros con 115 adiciones y 29 borrados
  1. 9 1
      lib/models/config.js
  2. 6 6
      lib/routes/admin.js
  3. 92 20
      lib/util/slack.js
  4. 1 1
      lib/util/swigFunctions.js
  5. 7 1
      lib/views/admin/notification.html

+ 9 - 1
lib/models/config.js

@@ -353,7 +353,7 @@ module.exports = function(crowi) {
   /**
   /**
    * for Slack Incoming Webhooks
    * for Slack Incoming Webhooks
    */
    */
-  configSchema.statics.hasSlackIwhConfig = function(config)
+  configSchema.statics.hasSlackIwhUrl = function(config)
   {
   {
     if (!config.notification) {
     if (!config.notification) {
       return false;
       return false;
@@ -361,6 +361,14 @@ module.exports = function(crowi) {
     return config.notification['slack:incomingWebhookUrl'];
     return config.notification['slack:incomingWebhookUrl'];
   };
   };
 
 
+  configSchema.statics.isIncomingWebhookPrioritized = function(config)
+  {
+    if (!config.notification) {
+      return false;
+    }
+    return config.notification['slack:isIncomingWebhookPrioritized'];
+  };
+
   configSchema.statics.hasSlackToken = function(config)
   configSchema.statics.hasSlackToken = function(config)
   {
   {
     if (!this.hasSlackConfig(config)) {
     if (!this.hasSlackConfig(config)) {

+ 6 - 6
lib/routes/admin.js

@@ -133,7 +133,7 @@ module.exports = function(crowi, app) {
     var UpdatePost = crowi.model('UpdatePost');
     var UpdatePost = crowi.model('UpdatePost');
     var slackSetting = Config.setupCofigFormData('notification', config);
     var slackSetting = Config.setupCofigFormData('notification', config);
     var hasSlackConfig = Config.hasSlackConfig(config);
     var hasSlackConfig = Config.hasSlackConfig(config);
-    var hasSlackIwhConfig = Config.hasSlackIwhConfig(config);
+    var hasSlackIwhUrl = Config.hasSlackIwhUrl(config);
     var hasSlackToken = Config.hasSlackToken(config);
     var hasSlackToken = Config.hasSlackToken(config);
     var slack = crowi.slack;
     var slack = crowi.slack;
     var slackAuthUrl = '';
     var slackAuthUrl = '';
@@ -142,7 +142,7 @@ module.exports = function(crowi, app) {
       slackSetting['slack:clientId'] = '';
       slackSetting['slack:clientId'] = '';
       slackSetting['slack:clientSecret'] = '';
       slackSetting['slack:clientSecret'] = '';
     }
     }
-    if (!Config.hasSlackIwhConfig(req.config)) {
+    if (!Config.hasSlackIwhUrl(req.config)) {
       slackSetting['slack:incomingWebhookUrl'] = '';
       slackSetting['slack:incomingWebhookUrl'] = '';
     }
     }
 
 
@@ -159,7 +159,7 @@ module.exports = function(crowi, app) {
         settings,
         settings,
         slackSetting,
         slackSetting,
         hasSlackConfig,
         hasSlackConfig,
-        hasSlackIwhConfig,
+        hasSlackIwhUrl,
         hasSlackToken,
         hasSlackToken,
         slackAuthUrl
         slackAuthUrl
       });
       });
@@ -197,7 +197,7 @@ module.exports = function(crowi, app) {
     }
     }
 
 
     var slack = crowi.slack;
     var slack = crowi.slack;
-    var bot = slack.createBot(true, true);
+    var bot = slack.initAppBot(true);
     var args = {
     var args = {
       code,
       code,
       client_id: config.notification['slack:clientId'],
       client_id: config.notification['slack:clientId'],
@@ -217,7 +217,7 @@ module.exports = function(crowi, app) {
             req.flash('successMessage', ['Successfully Connected!']);
             req.flash('successMessage', ['Successfully Connected!']);
           }
           }
 
 
-          slack.createBot(true);
+          slack.initAppBot();
           return res.redirect('/admin/notification');
           return res.redirect('/admin/notification');
         });
         });
       }
       }
@@ -233,7 +233,7 @@ module.exports = function(crowi, app) {
       Config.updateConfigCache('notification', config);
       Config.updateConfigCache('notification', config);
       req.flash('successMessage', ['Successfully Disconnected!']);
       req.flash('successMessage', ['Successfully Disconnected!']);
 
 
-      slack.createBot(true);
+      slack.initAppBot();
       return res.redirect('/admin/notification');
       return res.redirect('/admin/notification');
     });
     });
   };
   };

+ 92 - 20
lib/util/slack.js

@@ -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);

+ 1 - 1
lib/util/swigFunctions.js

@@ -72,7 +72,7 @@ module.exports = function(crowi, app, req, locals) {
 
 
   locals.slackConfigured = function() {
   locals.slackConfigured = function() {
     var config = crowi.getConfig()
     var config = crowi.getConfig()
-    if (Config.hasSlackToken(config)) {
+    if (Config.hasSlackToken(config) || Config.hasSlackIwhUrl(config)) {
       return true;
       return true;
     }
     }
     return false;
     return false;

+ 7 - 1
lib/views/admin/notification.html

@@ -201,6 +201,7 @@
                   <input type="checkbox" name="slackIwhSetting[slack:isIncomingWebhookPrioritized]" value="1"
                   <input type="checkbox" name="slackIwhSetting[slack:isIncomingWebhookPrioritized]" value="1"
                     {% if slackSetting['slack:isIncomingWebhookPrioritized'] %}checked{% endif %}>
                     {% if slackSetting['slack:isIncomingWebhookPrioritized'] %}checked{% endif %}>
                   Prioritize Incoming Webhook than Slack App
                   Prioritize Incoming Webhook than Slack App
+                  <p class="help-block">Check this option and crowi-plus use Incoming Webhooks even if Slack App settings are enabled.</p>
                 </div>
                 </div>
               </div>
               </div>
 
 
@@ -228,7 +229,12 @@
                 <li>Add.</li>
                 <li>Add.</li>
               </ol>
               </ol>
             </li>
             </li>
-
+            <li>
+              (At crowi-plus) Set Webhook URL
+              <ol>
+                <li>Input "Webhook URL" and submit on this page.</li>
+              </ol>
+            </li>
           </ol>
           </ol>
 
 
         </div><!-- /#slack-incoming-webhooks -->
         </div><!-- /#slack-incoming-webhooks -->