bolt.js 6.7 KB

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