| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- import {
- BodyParams, Controller, Get, Inject, Post, Req, Res, UseBefore,
- } from '@tsed/common';
- import axios from 'axios';
- import { WebAPICallResult } from '@slack/web-api';
- import {
- generateMarkdownSectionBlock, GrowiCommand, parseSlashCommand, postEphemeralErrors, verifySlackRequest,
- } from '@growi/slack';
- import { Relation } from '~/entities/relation';
- import { SlackOauthReq } from '~/interfaces/slack-to-growi/slack-oauth-req';
- import { InstallationRepository } from '~/repositories/installation';
- import { RelationRepository } from '~/repositories/relation';
- import { OrderRepository } from '~/repositories/order';
- import { AddSigningSecretToReq } from '~/middlewares/slack-to-growi/add-signing-secret-to-req';
- import { AuthorizeCommandMiddleware, AuthorizeInteractionMiddleware } from '~/middlewares/slack-to-growi/authorizer';
- import { ExtractGrowiUriFromReq } from '~/middlewares/slack-to-growi/extract-growi-uri-from-req';
- import { InstallerService } from '~/services/InstallerService';
- import { SelectGrowiService } from '~/services/SelectGrowiService';
- import { RegisterService } from '~/services/RegisterService';
- import { UnregisterService } from '~/services/UnregisterService';
- import { InvalidUrlError } from '../models/errors';
- import loggerFactory from '~/utils/logger';
- const logger = loggerFactory('slackbot-proxy:controllers:slack');
- @Controller('/slack')
- export class SlackCtrl {
- @Inject()
- installerService: InstallerService;
- @Inject()
- installationRepository: InstallationRepository;
- @Inject()
- relationRepository: RelationRepository;
- @Inject()
- orderRepository: OrderRepository;
- @Inject()
- selectGrowiService: SelectGrowiService;
- @Inject()
- registerService: RegisterService;
- @Inject()
- unregisterService: UnregisterService;
- @Get('/install')
- async install(): Promise<string> {
- const url = await this.installerService.installer.generateInstallUrl({
- // Add the scopes your app needs
- scopes: [
- 'channels:history',
- 'commands',
- 'groups:history',
- 'im:history',
- 'mpim:history',
- 'chat:write',
- 'team:read',
- ],
- });
- return `<a href="${url}">`
- // eslint-disable-next-line max-len
- + '<img alt="Add to Slack" height="40" width="139" src="https://platform.slack-edge.com/img/add_to_slack.png" srcSet="https://platform.slack-edge.com/img/add_to_slack.png 1x, https://platform.slack-edge.com/img/add_to_slack@2x.png 2x" />'
- + '</a>';
- }
- /**
- * Send command to specified GROWIs
- * @param growiCommand
- * @param relations
- * @param body
- * @returns
- */
- private async sendCommand(growiCommand: GrowiCommand, relations: Relation[], body: any) {
- if (relations.length === 0) {
- throw new Error('relations must be set');
- }
- const botToken = relations[0].installation?.data.bot?.token; // relations[0] should be exist
- const promises = relations.map((relation: Relation) => {
- // generate API URL
- const url = new URL('/_api/v3/slack-integration/proxied/commands', relation.growiUri);
- return axios.post(url.toString(), {
- ...body,
- growiCommand,
- }, {
- headers: {
- 'x-growi-ptog-tokens': relation.tokenPtoG,
- },
- });
- });
- // pickup PromiseRejectedResult only
- const results = await Promise.allSettled(promises);
- const rejectedResults: PromiseRejectedResult[] = results.filter((result): result is PromiseRejectedResult => result.status === 'rejected');
- try {
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- return postEphemeralErrors(rejectedResults, body.channel_id, body.user_id, botToken!);
- }
- catch (err) {
- logger.error(err);
- }
- }
- @Post('/commands')
- @UseBefore(AddSigningSecretToReq, verifySlackRequest, AuthorizeCommandMiddleware)
- async handleCommand(@Req() req: SlackOauthReq, @Res() res: Res): Promise<void|string|Res|WebAPICallResult> {
- const { body, authorizeResult } = req;
- if (body.text == null) {
- return 'No text.';
- }
- const growiCommand = parseSlashCommand(body);
- // register
- if (growiCommand.growiCommandType === 'register') {
- // Send response immediately to avoid opelation_timeout error
- // See https://api.slack.com/apis/connections/events-api#the-events-api__responding-to-events
- res.send();
- return this.registerService.process(growiCommand, authorizeResult, body as {[key:string]:string});
- }
- // unregister
- if (growiCommand.growiCommandType === 'unregister') {
- if (growiCommand.growiCommandArgs.length === 0) {
- return 'GROWI Urls is required.';
- }
- if (!growiCommand.growiCommandArgs.every(v => v.match(/^(https?:\/\/)/))) {
- return 'GROWI Urls must be urls.';
- }
- // Send response immediately to avoid opelation_timeout error
- // See https://api.slack.com/apis/connections/events-api#the-events-api__responding-to-events
- res.send();
- return this.unregisterService.process(growiCommand, authorizeResult, body as {[key:string]:string});
- }
- const installationId = authorizeResult.enterpriseId || authorizeResult.teamId;
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- const installation = await this.installationRepository.findByTeamIdOrEnterpriseId(installationId!);
- // const relations = await this.relationRepository.find({ installation });
- const relations = await this.relationRepository.createQueryBuilder('relation')
- .where('relation.installationId = :id', { id: installation?.id })
- .leftJoinAndSelect('relation.installation', 'installation')
- .getMany();
- console.log('relations', relations);
- if (relations.length === 0) {
- return res.json({
- blocks: [
- generateMarkdownSectionBlock('*No relation found.*'),
- generateMarkdownSectionBlock('Run `/growi register` first.'),
- ],
- });
- }
- // status
- if (growiCommand.growiCommandType === 'status') {
- return res.json({
- blocks: [
- generateMarkdownSectionBlock('*Found Relations to GROWI.*'),
- ...relations.map(relation => generateMarkdownSectionBlock(`GROWI url: ${relation.growiUri}.`)),
- ],
- });
- }
- // Send response immediately to avoid opelation_timeout error
- // See https://api.slack.com/apis/connections/events-api#the-events-api__responding-to-events
- res.send();
- body.growiUris = [];
- relations.forEach((relation) => {
- if (relation.siglePostCommands.includes(growiCommand.growiCommandType)) {
- body.growiUris.push(relation.growiUri);
- }
- });
- if (body.growiUris != null && body.growiUris.length > 0) {
- return this.selectGrowiService.process(growiCommand, authorizeResult, body);
- }
- /*
- * forward to GROWI server
- */
- this.sendCommand(growiCommand, relations, body);
- }
- @Post('/interactions')
- @UseBefore(AuthorizeInteractionMiddleware, ExtractGrowiUriFromReq)
- async handleInteraction(@Req() req: SlackOauthReq, @Res() res: Res): Promise<void|string|Res|WebAPICallResult> {
- logger.info('receive interaction', req.body);
- logger.info('receive interaction', req.authorizeResult);
- const { body, authorizeResult } = req;
- // Send response immediately to avoid opelation_timeout error
- // See https://api.slack.com/apis/connections/events-api#the-events-api__responding-to-events
- res.send();
- // pass
- if (body.ssl_check != null) {
- return;
- }
- const installationId = authorizeResult.enterpriseId || authorizeResult.teamId;
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- const installation = await this.installationRepository.findByTeamIdOrEnterpriseId(installationId!);
- const payload = JSON.parse(body.payload);
- const callBackId = payload?.view?.callback_id;
- // register
- if (callBackId === 'register') {
- try {
- await this.registerService.insertOrderRecord(installation, authorizeResult.botToken, payload);
- }
- catch (err) {
- if (err instanceof InvalidUrlError) {
- logger.info(err.message);
- return;
- }
- logger.error(err);
- }
- await this.registerService.notifyServerUriToSlack(authorizeResult.botToken, payload);
- return;
- }
- // unregister
- if (callBackId === 'unregister') {
- await this.unregisterService.unregister(installation, authorizeResult, payload);
- return;
- }
- // forward to GROWI server
- if (callBackId === 'select_growi') {
- const selectedGrowiInformation = await this.selectGrowiService.handleSelectInteraction(installation, payload);
- return this.sendCommand(selectedGrowiInformation.growiCommand, [selectedGrowiInformation.relation], selectedGrowiInformation.sendCommandBody);
- }
- /*
- * forward to GROWI server
- */
- const relation = await this.relationRepository.findOne({ installation, growiUri: req.growiUri });
- if (relation == null) {
- logger.error('*No relation found.*');
- return;
- }
- try {
- // generate API URL
- const url = new URL('/_api/v3/slack-integration/proxied/interactions', req.growiUri);
- await axios.post(url.toString(), {
- ...body,
- }, {
- headers: {
- 'x-growi-ptog-tokens': relation.tokenPtoG,
- },
- });
- }
- catch (err) {
- logger.error(err);
- }
- }
- @Post('/events')
- async handleEvent(@BodyParams() body:{[key:string]:string}, @Res() res: Res): Promise<void|string> {
- // eslint-disable-next-line max-len
- // see: https://api.slack.com/apis/connections/events-api#the-events-api__subscribing-to-event-types__events-api-request-urls__request-url-configuration--verification
- if (body.type === 'url_verification') {
- return body.challenge;
- }
- logger.info('receive event', body);
- return;
- }
- @Get('/oauth_redirect')
- async handleOauthRedirect(@Req() req: Req, @Res() res: Res): Promise<void> {
- if (req.query.state === '') {
- res.writeHead(500, { 'Content-Type': 'text/html; charset=utf-8' });
- res.end('<html>'
- + '<head><meta name="viewport" content="width=device-width,initial-scale=1"></head>'
- + '<body style="text-align:center; padding-top:20%;">'
- + '<h1>Illegal state, try it again.</h1>'
- + '<a href="/slack/install">'
- + 'Go to install page'
- + '</a>'
- + '</body></html>');
- }
- await this.installerService.installer.handleCallback(req, res, {
- success: (installation, metadata, req, res) => {
- logger.info('Success to install', { installation, metadata });
- const appPageUrl = `https://slack.com/apps/${installation.appId}`;
- res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
- res.end('<html>'
- + '<head><meta name="viewport" content="width=device-width,initial-scale=1"></head>'
- + '<body style="text-align:center; padding-top:20%;">'
- + '<h1>Congratulations!</h1>'
- + '<p>GROWI Bot installation has succeeded.</p>'
- + `<a href="${appPageUrl}">`
- + 'Access to Slack App detail page.'
- + '</a>'
- + '</body></html>');
- },
- failure: (error, installOptions, req, res) => {
- res.writeHead(500, { 'Content-Type': 'text/html; charset=utf-8' });
- res.end('<html>'
- + '<head><meta name="viewport" content="width=device-width,initial-scale=1"></head>'
- + '<body style="text-align:center; padding-top:20%;">'
- + '<h1>GROWI Bot installation failed</h1>'
- + '<p>Please contact administrators of your workspace</p>'
- + 'Reference: <a href="https://slack.com/help/articles/222386767-Manage-app-installation-settings-for-your-workspace">'
- + 'Manage app installation settings for your workspace'
- + '</a>'
- + '</body></html>');
- },
- });
- }
- }
|