bolt.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. const logger = require('@alias/logger')('growi:service:BoltService');
  2. class BoltReciever {
  3. init(app) {
  4. this.bolt = app;
  5. }
  6. async requestHandler(req, res) {
  7. if (this.bolt === undefined) {
  8. throw new Error('Slack bot is not setup');
  9. }
  10. let ackCalled = false;
  11. // for verification request URL on Event Subscriptions
  12. if (req.body.challenge && req.body.type) {
  13. return res.send(req.body);
  14. }
  15. const event = {
  16. body: req.body,
  17. ack: (response) => {
  18. if (ackCalled) {
  19. return null;
  20. }
  21. ackCalled = true;
  22. if (response instanceof Error) {
  23. throw new Error();
  24. }
  25. else if (!response) {
  26. return;
  27. }
  28. else {
  29. return response;
  30. }
  31. },
  32. };
  33. await this.bolt.processEvent(event);
  34. }
  35. }
  36. const { App } = require('@slack/bolt');
  37. const { WebClient, LogLevel } = require('@slack/web-api');
  38. class BoltService {
  39. constructor(crowi) {
  40. this.crowi = crowi;
  41. this.receiver = new BoltReciever();
  42. const signingSecret = crowi.configManager.getConfig('crowi', 'slackbot:signingSecret');
  43. const token = crowi.configManager.getConfig('crowi', 'slackbot:token');
  44. const client = new WebClient(token, { logLevel: LogLevel.DEBUG });
  45. this.client = client;
  46. if (token != null || signingSecret != null) {
  47. logger.debug('TwitterStrategy: setup is done');
  48. this.bolt = new App({
  49. token,
  50. signingSecret,
  51. receiver: this.receiver,
  52. });
  53. this.init();
  54. }
  55. }
  56. init() {
  57. // Example of listening for event
  58. // See. https://github.com/slackapi/bolt-js#listening-for-events
  59. // or https://slack.dev/bolt-js/concepts#basic
  60. this.bolt.command('/growi-bot', async({ command, ack, say }) => { // demo
  61. await say('Hello');
  62. });
  63. // The echo command simply echoes on command
  64. this.bolt.command('/echo', async({ command, ack, say }) => {
  65. // Acknowledge command request
  66. await ack();
  67. await say(`${command.text}`);
  68. });
  69. this.bolt.command('/growi', async({
  70. command, client, body, ack,
  71. }) => {
  72. await ack();
  73. const args = command.text.split(' ');
  74. const firstArg = args[0];
  75. switch (firstArg) {
  76. case 'search':
  77. this.searchResults(command, args);
  78. break;
  79. case 'create':
  80. this.createModal(command, client, body);
  81. break;
  82. default:
  83. this.notCommand(command);
  84. break;
  85. }
  86. });
  87. }
  88. notCommand(command) {
  89. logger.error('Input first arguments');
  90. return this.client.chat.postEphemeral({
  91. channel: command.channel_id,
  92. user: command.user_id,
  93. blocks: [
  94. this.generateMarkdownSectionBlock('*コマンドが存在しません。*\n Hint\n `/growi [command] [keyword]`'),
  95. ],
  96. });
  97. }
  98. async searchResults(command, args) {
  99. const firstKeyword = args[1];
  100. if (firstKeyword == null) {
  101. return this.client.chat.postEphemeral({
  102. channel: command.channel_id,
  103. user: command.user_id,
  104. blocks: [
  105. this.generateMarkdownSectionBlock('*キーワードを入力してください。*\n Hint\n `/growi search [keyword]`'),
  106. ],
  107. });
  108. }
  109. // remove leading 'search'.
  110. args.shift();
  111. const keywords = args.join(' ');
  112. const { searchService } = this.crowi;
  113. const option = { limit: 10 };
  114. const results = await searchService.searchKeyword(keywords, null, {}, option);
  115. // no search results
  116. if (results.data.length === 0) {
  117. return this.client.chat.postEphemeral({
  118. channel: command.channel_id,
  119. user: command.user_id,
  120. blocks: [
  121. this.generateMarkdownSectionBlock('*キーワードに該当するページは存在しません。*'),
  122. ],
  123. });
  124. }
  125. const resultPaths = results.data.map((data) => {
  126. return data._source.path;
  127. });
  128. try {
  129. await this.client.chat.postEphemeral({
  130. channel: command.channel_id,
  131. user: command.user_id,
  132. blocks: [
  133. this.generateMarkdownSectionBlock('検索結果 10 件'),
  134. this.generateMarkdownSectionBlock(`${resultPaths.join('\n')}`),
  135. {
  136. type: 'actions',
  137. elements: [
  138. {
  139. type: 'button',
  140. text: {
  141. type: 'plain_text',
  142. text: '検索結果をこのチャンネルに共有する',
  143. },
  144. style: 'primary',
  145. },
  146. ],
  147. },
  148. ],
  149. });
  150. }
  151. catch {
  152. logger.error('Failed to get search results.');
  153. await this.client.chat.postEphemeral({
  154. channel: command.channel_id,
  155. user: command.user_id,
  156. blocks: [
  157. this.generateMarkdownSectionBlock('*検索に失敗しました。*\n Hint\n `/growi search [keyword]`'),
  158. ],
  159. });
  160. }
  161. }
  162. async createModal(command, client, body) {
  163. try {
  164. await client.views.open({
  165. trigger_id: body.trigger_id,
  166. view: {
  167. type: 'modal',
  168. callback_id: 'createPage',
  169. title: {
  170. type: 'plain_text',
  171. text: 'Create Page',
  172. },
  173. submit: {
  174. type: 'plain_text',
  175. text: 'Submit',
  176. },
  177. close: {
  178. type: 'plain_text',
  179. text: 'Cancel',
  180. },
  181. blocks: [
  182. this.generateMarkdownSectionBlock('ページを作成します'),
  183. this.generateInputSectionBlock('path', 'Path', 'path_input', false, '/path'),
  184. this.generateInputSectionBlock('contents', 'Contents', 'contents_input', true, 'Input with Markdown...'),
  185. ],
  186. },
  187. });
  188. }
  189. catch {
  190. logger.error('Failed to create page.');
  191. await this.client.chat.postEphemeral({
  192. channel: command.channel_id,
  193. user: command.user_id,
  194. blocks: [
  195. this.generateMarkdownSectionBlock('*ページ作成に失敗しました。*\n Hint\n `/growi create`'),
  196. ],
  197. });
  198. }
  199. }
  200. generateMarkdownSectionBlock(blocks) {
  201. return {
  202. type: 'section',
  203. text: {
  204. type: 'mrkdwn',
  205. text: blocks,
  206. },
  207. };
  208. }
  209. generateInputSectionBlock(blockId, labelText, actionId, isMultiline, placeholder) {
  210. return {
  211. type: 'input',
  212. block_id: blockId,
  213. label: {
  214. type: 'plain_text',
  215. text: labelText,
  216. },
  217. element: {
  218. type: 'plain_text_input',
  219. action_id: actionId,
  220. multiline: isMultiline,
  221. placeholder: {
  222. type: 'plain_text',
  223. text: placeholder,
  224. },
  225. },
  226. };
  227. }
  228. }
  229. module.exports = BoltService;