|
|
@@ -10,6 +10,9 @@ import { OrderRepository } from '~/repositories/order';
|
|
|
import { InstallerService } from '~/services/InstallerService';
|
|
|
import { RegisterService } from '~/services/RegisterService';
|
|
|
|
|
|
+import loggerFactory from '~/utils/logger';
|
|
|
+
|
|
|
+const logger = loggerFactory('slackbot-proxy:controllers:slack');
|
|
|
|
|
|
@Controller('/slack')
|
|
|
export class SlackCtrl {
|
|
|
@@ -74,8 +77,11 @@ export class SlackCtrl {
|
|
|
|
|
|
@Post('/events')
|
|
|
async handleEvent(@BodyParams() body:{[key:string]:string}, @Res() res: Res): Promise<string> {
|
|
|
- // Send response immediately to avoid opelation_timeout error
|
|
|
- // See https://api.slack.com/apis/connections/events-api#the-events-api__responding-to-events
|
|
|
+ // 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;
|
|
|
+ }
|
|
|
|
|
|
if (body.text == null) {
|
|
|
return 'No text.';
|
|
|
@@ -87,9 +93,13 @@ export class SlackCtrl {
|
|
|
if (executeGrowiCommand == null) {
|
|
|
return 'No executeGrowiCommand';
|
|
|
}
|
|
|
- await executeGrowiCommand(body);
|
|
|
+
|
|
|
+ // 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();
|
|
|
|
|
|
+ await executeGrowiCommand(body);
|
|
|
+
|
|
|
const installation = await this.installationRepository.findByID('1');
|
|
|
if (installation == null) {
|
|
|
throw new Error('installation is reqiured');
|
|
|
@@ -114,22 +124,35 @@ export class SlackCtrl {
|
|
|
@Get('/oauth_redirect')
|
|
|
async handleOauthRedirect(@Req() req: Req, @Res() res: Res): Promise<void> {
|
|
|
|
|
|
- // illegal state
|
|
|
- // TODO: https://youtrack.weseek.co.jp/issue/GW-5543
|
|
|
- if (req.query.state !== 'init') {
|
|
|
+ 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'
|
|
|
+ + 'Go to install page'
|
|
|
+ '</a>'
|
|
|
+ '</body></html>');
|
|
|
}
|
|
|
|
|
|
- this.installerService.installer.handleCallback(req, res, {
|
|
|
- // success: (installation, metadata, req, res) => {},
|
|
|
+ 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>'
|