Просмотр исходного кода

ensure to be able to refresh token when invoking `slack.createBot`

Yuki Takei 8 лет назад
Родитель
Сommit
ae4eeaf7f9
2 измененных файлов с 15 добавлено и 7 удалено
  1. 8 3
      lib/routes/admin.js
  2. 7 4
      lib/util/slack.js

+ 8 - 3
lib/routes/admin.js

@@ -191,8 +191,13 @@ module.exports = function(crowi, app) {
     }
     }
 
 
     var slack = crowi.slack;
     var slack = crowi.slack;
-    var bot = slack.createBot();
-    bot.api.oauth.access({code}, function(err, data) {
+    var bot = slack.createBot(true, true);
+    var args = {
+      code,
+      client_id: config.notification['slack:clientId'],
+      client_secret: config.notification['slack:clientSecret'],
+    }
+    bot.api.oauth.access(args, function(err, data) {
       debug('oauth response', err, data);
       debug('oauth response', err, data);
       if (!data.ok || !data.access_token) {
       if (!data.ok || !data.access_token) {
         req.flash('errorMessage', ['Failed to fetch access_token. Please do connect again.']);
         req.flash('errorMessage', ['Failed to fetch access_token. Please do connect again.']);
@@ -206,7 +211,7 @@ module.exports = function(crowi, app) {
             req.flash('successMessage', ['Successfully Connected!']);
             req.flash('successMessage', ['Successfully Connected!']);
           }
           }
 
 
-          slack.createBot();
+          slack.createBot(true);
           return res.redirect('/admin/notification');
           return res.redirect('/admin/notification');
         });
         });
       }
       }

+ 7 - 4
lib/util/slack.js

@@ -12,9 +12,9 @@ module.exports = function(crowi) {
     slack = {};
     slack = {};
   slack.controller = undefined;
   slack.controller = undefined;
 
 
-  slack.createBot = function() {
+  slack.createBot = function(isForce, isClearToken) {
     // alreay created
     // alreay created
-    if (bot) {
+    if (!isForce && bot) {
       return bot;
       return bot;
     }
     }
 
 
@@ -28,7 +28,7 @@ module.exports = function(crowi) {
       return false;
       return false;
     }
     }
 
 
-    if (Config.hasSlackToken(config)) {
+    if (!isClearToken && Config.hasSlackToken(config)) {
       bot = slack.controller.spawn({token: config.notification['slack:token']});
       bot = slack.controller.spawn({token: config.notification['slack:token']});
     } else {
     } else {
       bot = slack.controller.spawn();
       bot = slack.controller.spawn();
@@ -39,8 +39,11 @@ module.exports = function(crowi) {
   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();
+      slack.controller = Botkit.slackbot({debug: isDebugSlackbot});
       slack.controller.configureSlackApp({
       slack.controller.configureSlackApp({
         clientId: config.notification['slack:clientId'],
         clientId: config.notification['slack:clientId'],
         clientSecret: config.notification['slack:clientSecret'],
         clientSecret: config.notification['slack:clientSecret'],