slack.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * slack
  3. */
  4. module.exports = function(crowi) {
  5. 'use strict';
  6. var debug = require('debug')('crowi:util:slack'),
  7. Config = crowi.model('Config'),
  8. Botkit = require('botkit'),
  9. sprintf = require('sprintf'),
  10. bot = null,
  11. slack = {};
  12. slack.controller = undefined;
  13. slack.createBot = function() {
  14. // alreay created
  15. if (bot) {
  16. return bot;
  17. }
  18. var config = crowi.getConfig();
  19. if (!slack.controller) {
  20. slack.configureSlackApp();
  21. }
  22. if (!slack.controller) {
  23. return false;
  24. }
  25. if (Config.hasSlackToken(config)) {
  26. bot = slack.controller.spawn({token: config.notification['slack:token']});
  27. } else {
  28. bot = slack.controller.spawn();
  29. }
  30. return bot;
  31. };
  32. slack.configureSlackApp = function ()
  33. {
  34. var config = crowi.getConfig();
  35. if (Config.hasSlackConfig(config)) {
  36. slack.controller = Botkit.slackbot();
  37. slack.controller.configureSlackApp({
  38. clientId: config.notification['slack:clientId'],
  39. clientSecret: config.notification['slack:clientSecret'],
  40. redirectUri: slack.getSlackAuthCallbackUrl(),
  41. scopes: ['chat:write:bot']
  42. });
  43. return true;
  44. }
  45. return false;
  46. }
  47. // hmmm
  48. slack.getSlackAuthCallbackUrl = function()
  49. {
  50. var config = crowi.getConfig();
  51. // Web アクセスがきてないと app:url がセットされないので crowi.setupSlack 時にはできない
  52. // cli, bot 系作るときに問題なりそう
  53. return (config.crowi['app:url'] || '') + '/admin/notification/slackAuth';
  54. }
  55. slack.getAuthorizeURL = function () {
  56. if (!slack.controller) {
  57. slack.configureSlackApp();
  58. }
  59. if (!slack.controller) {
  60. return '';
  61. }
  62. return slack.controller.getAuthorizeURL();
  63. }
  64. slack.post = function (message) {
  65. var bot = slack.createBot();
  66. return new Promise(function(resolve, reject) {
  67. bot.api.chat.postMessage(message, function(err, res) {
  68. if (err) {
  69. debug('Post error', err, res);
  70. debug('Sent data to slack is:', message);
  71. return reject(err);
  72. }
  73. resolve(res);
  74. });
  75. });
  76. };
  77. slack.prepareSlackMessage = function(page, user, channel, updateType) {
  78. var config = crowi.getConfig();
  79. var url = config.crowi['app:url'] || '';
  80. var body = page.revision.body;
  81. if (body.length > 2000) {
  82. body = body.substr(0, 2000) + '...';
  83. }
  84. var attachment = {
  85. color: '#263a3c',
  86. author_name: '@' + user.username,
  87. author_link: url + '/user/' + user.username,
  88. author_icon: user.image,
  89. title: page.path,
  90. title_link: url + page.path,
  91. text: body,
  92. mrkdwn_in: ["text"],
  93. };
  94. if (user.image) {
  95. attachment.author_icon = user.image;
  96. }
  97. var message = {
  98. channel: '#' + channel,
  99. username: 'Crowi',
  100. text: this.getSlackMessageText(page.path, user, updateType),
  101. attachments: [attachment],
  102. };
  103. return message;
  104. };
  105. slack.getSlackMessageText = function(path, user, updateType) {
  106. var text;
  107. if (updateType == 'create') {
  108. text = sprintf('%s created a new page! %s', user.username, path);
  109. } else {
  110. text = sprintf('%s updated %s', user.username, path);
  111. }
  112. return text;
  113. };
  114. return slack;
  115. };