slack-integration.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import mongoose from 'mongoose';
  2. import { IncomingWebhookSendArguments } from '@slack/webhook';
  3. import { ChatPostMessageArguments, WebClient } from '@slack/web-api';
  4. import { generateWebClient, markdownSectionBlock } from '@growi/slack';
  5. import loggerFactory from '~/utils/logger';
  6. import S2sMessage from '../models/vo/s2s-message';
  7. import ConfigManager from './config-manager';
  8. import { S2sMessagingService } from './s2s-messaging/base';
  9. import { S2sMessageHandlable } from './s2s-messaging/handlable';
  10. const logger = loggerFactory('growi:service:SlackBotService');
  11. type S2sMessageForSlackIntegration = S2sMessage & { updatedAt: Date };
  12. export class SlackIntegrationService implements S2sMessageHandlable {
  13. crowi!: any;
  14. configManager!: ConfigManager;
  15. s2sMessagingService!: S2sMessagingService;
  16. lastLoadedAt?: Date;
  17. constructor(crowi) {
  18. this.crowi = crowi;
  19. this.configManager = crowi.configManager;
  20. this.s2sMessagingService = crowi.s2sMessagingService;
  21. this.initialize();
  22. }
  23. initialize() {
  24. this.lastLoadedAt = new Date();
  25. }
  26. /**
  27. * @inheritdoc
  28. */
  29. shouldHandleS2sMessage(s2sMessage: S2sMessageForSlackIntegration): boolean {
  30. const { eventName, updatedAt } = s2sMessage;
  31. if (eventName !== 'slackIntegrationServiceUpdated' || updatedAt == null) {
  32. return false;
  33. }
  34. return this.lastLoadedAt == null || this.lastLoadedAt < new Date(s2sMessage.updatedAt);
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. async handleS2sMessage(): Promise<void> {
  40. const { configManager } = this.crowi;
  41. logger.info('Reset slack bot by pubsub notification');
  42. await configManager.loadConfigs();
  43. this.initialize();
  44. }
  45. async publishUpdatedMessage(): Promise<void> {
  46. const { s2sMessagingService } = this;
  47. if (s2sMessagingService != null) {
  48. const s2sMessage = new S2sMessage('slackIntegrationServiceUpdated', { updatedAt: new Date() });
  49. try {
  50. await s2sMessagingService.publish(s2sMessage);
  51. }
  52. catch (e) {
  53. logger.error('Failed to publish update message with S2sMessagingService: ', e.message);
  54. }
  55. }
  56. }
  57. get isSlackConfigured(): boolean {
  58. return this.isSlackbotConfigured || this.isSlackLegacyConfigured;
  59. }
  60. get isSlackbotConfigured(): boolean {
  61. const hasSlackbotType = !!this.configManager.getConfig('crowi', 'slackbot:currentBotType');
  62. return hasSlackbotType;
  63. }
  64. get isSlackLegacyConfigured(): boolean {
  65. // for legacy util
  66. const hasSlackToken = !!this.configManager.getConfig('notification', 'slack:token');
  67. const hasSlackIwhUrl = !!this.configManager.getConfig('notification', 'slack:incomingWebhookUrl');
  68. return hasSlackToken || hasSlackIwhUrl;
  69. }
  70. private isCheckTypeValid(): boolean {
  71. const currentBotType = this.configManager.getConfig('crowi', 'slackbot:currentBotType');
  72. if (currentBotType == null) {
  73. throw new Error('The config \'SLACK_BOT_TYPE\'(ns: \'crowi\', key: \'slackbot:currentBotType\') must be set.');
  74. }
  75. return true;
  76. }
  77. /**
  78. * generate WebClient instance for 'customBotWithoutProxy' type
  79. */
  80. async generateClientForCustomBotWithoutProxy(): Promise<WebClient> {
  81. this.isCheckTypeValid();
  82. const token = this.configManager.getConfig('crowi', 'slackbot:token');
  83. if (token == null) {
  84. throw new Error('The config \'SLACK_BOT_TOKEN\'(ns: \'crowi\', key: \'slackbot:token\') must be set.');
  85. }
  86. return generateWebClient(token);
  87. }
  88. /**
  89. * generate WebClient instance by tokenPtoG
  90. * @param tokenPtoG
  91. */
  92. async generateClientByTokenPtoG(tokenPtoG: string): Promise<WebClient> {
  93. this.isCheckTypeValid();
  94. const SlackAppIntegration = mongoose.model('SlackAppIntegration');
  95. const slackAppIntegration = await SlackAppIntegration.findOne({ tokenPtoG });
  96. if (slackAppIntegration == null) {
  97. throw new Error('No SlackAppIntegration exists that corresponds to the tokenPtoG specified.');
  98. }
  99. return this.generateClientBySlackAppIntegration(slackAppIntegration as unknown as { tokenGtoP: string; });
  100. }
  101. /**
  102. * generate WebClient instance by tokenPtoG
  103. * @param tokenPtoG
  104. */
  105. async generateClientForPrimaryWorkspace(): Promise<WebClient> {
  106. this.isCheckTypeValid();
  107. const currentBotType = this.configManager.getConfig('crowi', 'slackbot:currentBotType');
  108. if (currentBotType === 'customBotWithoutProxy') {
  109. return this.generateClientForCustomBotWithoutProxy();
  110. }
  111. // retrieve primary SlackAppIntegration
  112. const SlackAppIntegration = mongoose.model('SlackAppIntegration');
  113. const slackAppIntegration = await SlackAppIntegration.findOne({ isPrimary: true });
  114. if (slackAppIntegration == null) {
  115. throw new Error('None of the primary SlackAppIntegration exists.');
  116. }
  117. return this.generateClientBySlackAppIntegration(slackAppIntegration as unknown as { tokenGtoP: string; });
  118. }
  119. /**
  120. * generate WebClient instance by SlackAppIntegration
  121. * @param slackAppIntegration
  122. */
  123. async generateClientBySlackAppIntegration(slackAppIntegration: { tokenGtoP: string }): Promise<WebClient> {
  124. this.isCheckTypeValid();
  125. // connect to proxy
  126. const proxyServerUri = this.configManager.getConfig('crowi', 'slackbot:proxyServerUri');
  127. const serverUri = new URL('/g2s', proxyServerUri);
  128. const headers = {
  129. 'x-growi-gtop-tokens': slackAppIntegration.tokenGtoP,
  130. };
  131. return generateWebClient(undefined, serverUri.toString(), headers);
  132. }
  133. async postMessage(messageArgs: ChatPostMessageArguments, slackAppIntegration?: { tokenGtoP: string; }): Promise<void> {
  134. // use legacy slack configuration
  135. if (this.isSlackLegacyConfigured && !this.isSlackbotConfigured) {
  136. return this.postMessageWithLegacyUtil(messageArgs);
  137. }
  138. const client = slackAppIntegration == null
  139. ? await this.generateClientForPrimaryWorkspace()
  140. : await this.generateClientBySlackAppIntegration(slackAppIntegration);
  141. try {
  142. await client.chat.postMessage(messageArgs);
  143. }
  144. catch (error) {
  145. logger.debug('Post error', error);
  146. logger.debug('Sent data to slack is:', messageArgs);
  147. throw error;
  148. }
  149. }
  150. private async postMessageWithLegacyUtil(messageArgs: ChatPostMessageArguments | IncomingWebhookSendArguments): Promise<void> {
  151. const slackLegacyUtil = require('../util/slack-legacy')(this.crowi);
  152. try {
  153. await slackLegacyUtil.postMessage(messageArgs);
  154. }
  155. catch (error) {
  156. logger.debug('Post error', error);
  157. logger.debug('Sent data to slack is:', messageArgs);
  158. throw error;
  159. }
  160. }
  161. /**
  162. * Handle /commands endpoint
  163. */
  164. async handleCommandRequest(command, client, body, ...opt) {
  165. let module;
  166. try {
  167. module = `./slack-command-handler/${command}`;
  168. }
  169. catch (err) {
  170. await this.notCommand(client, body);
  171. }
  172. try {
  173. const handler = require(module)(this.crowi);
  174. await handler.handleCommand(client, body, ...opt);
  175. }
  176. catch (err) {
  177. throw err;
  178. }
  179. }
  180. async handleBlockActionsRequest(client, payload) {
  181. const { action_id: actionId } = payload.actions[0];
  182. const commandName = actionId.split(':')[0];
  183. const handlerMethodName = actionId.split(':')[1];
  184. const module = `./slack-command-handler/${commandName}`;
  185. try {
  186. const handler = require(module)(this.crowi);
  187. await handler.handleBlockActions(client, payload, handlerMethodName);
  188. }
  189. catch (err) {
  190. throw err;
  191. }
  192. return;
  193. }
  194. async handleViewSubmissionRequest(client, payload) {
  195. const { callback_id: callbackId } = payload.view;
  196. const commandName = callbackId.split(':')[0];
  197. const handlerMethodName = callbackId.split(':')[1];
  198. const module = `./slack-command-handler/${commandName}`;
  199. try {
  200. const handler = require(module)(this.crowi);
  201. await handler.handleBlockActions(client, payload, handlerMethodName);
  202. }
  203. catch (err) {
  204. throw err;
  205. }
  206. return;
  207. }
  208. async notCommand(client, body) {
  209. logger.error('Invalid first argument');
  210. client.chat.postEphemeral({
  211. channel: body.channel_id,
  212. user: body.user_id,
  213. text: 'No command',
  214. blocks: [
  215. markdownSectionBlock('*No command.*\n Hint\n `/growi [command] [keyword]`'),
  216. ],
  217. });
  218. return;
  219. }
  220. }