bolt.js 7.0 KB

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