| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- const express = require('express');
- const loggerFactory = require('@alias/logger');
- const logger = loggerFactory('growi:routes:apiv3:slack-bot');
- const router = express.Router();
- const { verificationSlackRequest } = require('@growi/slack');
- module.exports = (crowi) => {
- this.app = crowi.express;
- // Check if the access token is correct
- function verificationAccessToken(req, res, next) {
- const slackBotAccessToken = req.body.slack_bot_access_token || null;
- if (slackBotAccessToken == null || slackBotAccessToken !== this.crowi.configManager.getConfig('crowi', 'slackbot:access-token')) {
- logger.error('slack_bot_access_token is invalid.');
- return res.send('*Access token is inValid*');
- }
- return next();
- }
- function verificationRequestUrl(req, res, next) {
- // for verification request URL on Event Subscriptions
- if (req.body.type === 'url_verification') {
- return res.send(req.body);
- }
- return next();
- }
- const addSlackBotSigningSecret = (req, res, next) => {
- req.signingSecret = crowi.configManager.getConfig('crowi', 'slackbot:signingSecret');
- return next();
- };
- router.post('/commands', verificationRequestUrl, addSlackBotSigningSecret, verificationSlackRequest, verificationAccessToken, async(req, res) => {
- // 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();
- const { body } = req;
- const args = body.text.split(' ');
- const command = args[0];
- try {
- switch (command) {
- case 'search':
- await crowi.slackBotService.showEphemeralSearchResults(body, args);
- break;
- case 'create':
- await crowi.slackBotService.createModal(body);
- break;
- default:
- await crowi.slackBotService.notCommand(body);
- break;
- }
- }
- catch (error) {
- logger.error(error);
- return res.send(error.message);
- }
- });
- const handleBlockActions = async(payload) => {
- const { action_id: actionId } = payload.actions[0];
- switch (actionId) {
- case 'shareSearchResults': {
- await crowi.slackBotService.shareSearchResults(payload);
- break;
- }
- case 'showNextResults': {
- const parsedValue = JSON.parse(payload.actions[0].value);
- const { body, args, offset } = parsedValue;
- const newOffset = offset + 10;
- await crowi.slackBotService.showEphemeralSearchResults(body, args, newOffset);
- break;
- }
- default:
- break;
- }
- };
- const handleViewSubmission = async(payload) => {
- const { callback_id: callbackId } = payload.view;
- switch (callbackId) {
- case 'createPage':
- await crowi.slackBotService.createPageInGrowi(payload);
- break;
- default:
- break;
- }
- };
- router.post('/interactions', verificationRequestUrl, addSlackBotSigningSecret, verificationSlackRequest, async(req, res) => {
- // 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();
- const payload = JSON.parse(req.body.payload);
- const { type } = payload;
- try {
- switch (type) {
- case 'block_actions':
- await handleBlockActions(payload);
- break;
- case 'view_submission':
- await handleViewSubmission(payload);
- break;
- default:
- break;
- }
- }
- catch (error) {
- logger.error(error);
- return res.send(error.message);
- }
- });
- return router;
- };
|