bolt.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. // for verification request URL on Event Subscriptions
  9. if (req.body.challenge && req.body.type) {
  10. return res.send(req.body);
  11. }
  12. const event = {
  13. body: req.body,
  14. ack: (response) => {
  15. if (ackCalled) {
  16. return;
  17. }
  18. if (response instanceof Error) {
  19. res.status(500).send();
  20. }
  21. else if (!response) {
  22. res.send('');
  23. }
  24. else {
  25. res.send(response);
  26. }
  27. ackCalled = true;
  28. },
  29. };
  30. await this.bolt.processEvent(event);
  31. // payload action. click "Submit" etc.
  32. if (req.body.payload == null) {
  33. return;
  34. }
  35. const payload = JSON.parse(req.body.payload);
  36. const { type } = payload;
  37. if (type === 'view_submission') {
  38. // avoid an error
  39. res.send('');
  40. this.bolt.view('view_1', async({ ack, view, say }) => {
  41. await ack();
  42. console.log('view');
  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. createPageInGrowi(payload) {
  110. const Page = this.crowi.model('Page');
  111. const path = payload.view.state.values.path.path_input.value;
  112. const body = payload.view.state.values.contents.contents_input.value;
  113. return Page.create(path, body, {}, {});
  114. }
  115. async searchResults(command, args) {
  116. const firstKeyword = args[1];
  117. if (firstKeyword == null) {
  118. return this.client.chat.postEphemeral({
  119. channel: command.channel_id,
  120. user: command.user_id,
  121. blocks: [
  122. this.generateMarkdownSectionBlock('*キーワードを入力してください。*\n Hint\n `/growi search [keyword]`'),
  123. ],
  124. });
  125. }
  126. // remove leading 'search'.
  127. args.shift();
  128. const keywords = args.join(' ');
  129. const { searchService } = this.crowi;
  130. const option = { limit: 10 };
  131. const results = await searchService.searchKeyword(keywords, null, {}, option);
  132. // no search results
  133. if (results.data.length === 0) {
  134. return this.client.chat.postEphemeral({
  135. channel: command.channel_id,
  136. user: command.user_id,
  137. blocks: [
  138. this.generateMarkdownSectionBlock('*キーワードに該当するページは存在しません。*'),
  139. ],
  140. });
  141. }
  142. const resultPaths = results.data.map((data) => {
  143. return data._source.path;
  144. });
  145. try {
  146. await this.client.chat.postEphemeral({
  147. channel: command.channel_id,
  148. user: command.user_id,
  149. blocks: [
  150. this.generateMarkdownSectionBlock('検索結果 10 件'),
  151. this.generateMarkdownSectionBlock(`${resultPaths.join('\n')}`),
  152. {
  153. type: 'actions',
  154. elements: [
  155. {
  156. type: 'button',
  157. text: {
  158. type: 'plain_text',
  159. text: '検索結果をこのチャンネルに共有する',
  160. },
  161. style: 'primary',
  162. },
  163. ],
  164. },
  165. ],
  166. });
  167. }
  168. catch {
  169. logger.error('Failed to get search results.');
  170. await this.client.chat.postEphemeral({
  171. channel: command.channel_id,
  172. user: command.user_id,
  173. blocks: [
  174. this.generateMarkdownSectionBlock('*検索に失敗しました。*\n Hint\n `/growi search [keyword]`'),
  175. ],
  176. });
  177. }
  178. }
  179. async createModal(command, client, body) {
  180. try {
  181. await client.views.open({
  182. trigger_id: body.trigger_id,
  183. view: {
  184. type: 'modal',
  185. callback_id: 'view_1',
  186. title: {
  187. type: 'plain_text',
  188. text: 'Create Page',
  189. },
  190. submit: {
  191. type: 'plain_text',
  192. text: 'Submit',
  193. },
  194. close: {
  195. type: 'plain_text',
  196. text: 'Cancel',
  197. },
  198. blocks: [
  199. this.generateMarkdownSectionBlock('ページを作成します'),
  200. this.generateInputSectionBlock('path', 'Path', 'path_input', false, '/path'),
  201. this.generateInputSectionBlock('contents', 'Contents', 'contents_input', true, 'Input with Markdown...'),
  202. ],
  203. },
  204. });
  205. }
  206. catch {
  207. logger.error('Failed to create page.');
  208. await this.client.chat.postEphemeral({
  209. channel: command.channel_id,
  210. user: command.user_id,
  211. blocks: [
  212. this.generateMarkdownSectionBlock('*ページ作成に失敗しました。*\n Hint\n `/growi create`'),
  213. ],
  214. });
  215. }
  216. }
  217. generateMarkdownSectionBlock(blocks) {
  218. return {
  219. type: 'section',
  220. text: {
  221. type: 'mrkdwn',
  222. text: blocks,
  223. },
  224. };
  225. }
  226. generateInputSectionBlock(blockId, labelText, actionId, isMultiline, placeholder) {
  227. return {
  228. type: 'input',
  229. block_id: blockId,
  230. label: {
  231. type: 'plain_text',
  232. text: labelText,
  233. },
  234. element: {
  235. type: 'plain_text_input',
  236. action_id: actionId,
  237. multiline: isMultiline,
  238. placeholder: {
  239. type: 'plain_text',
  240. text: placeholder,
  241. },
  242. },
  243. };
  244. }
  245. }
  246. module.exports = BoltService;