|
@@ -8,11 +8,17 @@ 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'),
|
|
|
|
|
+ sprintf = require('sprintf'),
|
|
|
|
|
+ bot = null,
|
|
|
slack = {};
|
|
slack = {};
|
|
|
slack.controller = undefined;
|
|
slack.controller = undefined;
|
|
|
|
|
|
|
|
slack.createBot = function() {
|
|
slack.createBot = function() {
|
|
|
- var bot;
|
|
|
|
|
|
|
+ // alreay created
|
|
|
|
|
+ if (bot) {
|
|
|
|
|
+ return bot;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
var config = crowi.getConfig();
|
|
var config = crowi.getConfig();
|
|
|
|
|
|
|
|
if (!slack.controller) {
|
|
if (!slack.controller) {
|
|
@@ -28,7 +34,6 @@ module.exports = function(crowi) {
|
|
|
} else {
|
|
} else {
|
|
|
bot = slack.controller.spawn();
|
|
bot = slack.controller.spawn();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
return bot;
|
|
return bot;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -71,5 +76,65 @@ module.exports = function(crowi) {
|
|
|
return slack.controller.getAuthorizeURL();
|
|
return slack.controller.getAuthorizeURL();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ slack.post = function (message) {
|
|
|
|
|
+ var bot = slack.createBot();
|
|
|
|
|
+
|
|
|
|
|
+ return new Promise(function(resolve, reject) {
|
|
|
|
|
+ bot.api.chat.postMessage(message, function(err, res) {
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ debug('Post error', err, res);
|
|
|
|
|
+ debug('Sent data to slack is:', message);
|
|
|
|
|
+ return reject(err);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ resolve(res);
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ slack.prepareSlackMessage = function(page, user, channel, updateType) {
|
|
|
|
|
+ var config = crowi.getConfig();
|
|
|
|
|
+ var url = config.crowi['app:url'] || '';
|
|
|
|
|
+
|
|
|
|
|
+ var body = page.revision.body;
|
|
|
|
|
+ if (body.length > 2000) {
|
|
|
|
|
+ body = body.substr(0, 2000) + '...';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var attachment = {
|
|
|
|
|
+ color: '#263a3c',
|
|
|
|
|
+ author_name: '@' + user.username,
|
|
|
|
|
+ author_link: url + '/user/' + user.username,
|
|
|
|
|
+ author_icon: user.image,
|
|
|
|
|
+ title: page.path,
|
|
|
|
|
+ title_link: url + page.path,
|
|
|
|
|
+ text: body,
|
|
|
|
|
+ mrkdwn_in: ["text"],
|
|
|
|
|
+ };
|
|
|
|
|
+ if (user.image) {
|
|
|
|
|
+ attachment.author_icon = user.image;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var message = {
|
|
|
|
|
+ channel: '#' + channel,
|
|
|
|
|
+ username: 'Crowi',
|
|
|
|
|
+ text: this.getSlackMessageText(page.path, user, updateType),
|
|
|
|
|
+ attachments: [attachment],
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ return message;
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ slack.getSlackMessageText = function(path, user, updateType) {
|
|
|
|
|
+ var text;
|
|
|
|
|
+
|
|
|
|
|
+ if (updateType == 'create') {
|
|
|
|
|
+ text = sprintf('%s created a new page! %s', user.username, path);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ text = sprintf('%s updated %s', user.username, path);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return text;
|
|
|
|
|
+ };
|
|
|
return slack;
|
|
return slack;
|
|
|
};
|
|
};
|