|
@@ -6,42 +6,48 @@ class BoltReciever {
|
|
|
this.bolt = app;
|
|
this.bolt = app;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async requestHandler(req, res) {
|
|
|
|
|
|
|
+ async requestHandler(body) {
|
|
|
|
|
+ if (this.bolt === undefined) {
|
|
|
|
|
+ throw new Error('Slack Bot service is not setup');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
let ackCalled = false;
|
|
let ackCalled = false;
|
|
|
|
|
|
|
|
// for verification request URL on Event Subscriptions
|
|
// for verification request URL on Event Subscriptions
|
|
|
- if (req.body.challenge && req.body.type) {
|
|
|
|
|
- return res.send(req.body);
|
|
|
|
|
|
|
+ if (body.type === 'url_verification') {
|
|
|
|
|
+ return body;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const payload = req.body.payload;
|
|
|
|
|
|
|
+ const payload = body.payload;
|
|
|
let reqBody;
|
|
let reqBody;
|
|
|
|
|
|
|
|
if (payload != null) {
|
|
if (payload != null) {
|
|
|
reqBody = JSON.parse(payload);
|
|
reqBody = JSON.parse(payload);
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
- reqBody = req.body;
|
|
|
|
|
|
|
+ reqBody = body;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const event = {
|
|
const event = {
|
|
|
body: reqBody,
|
|
body: reqBody,
|
|
|
ack: (response) => {
|
|
ack: (response) => {
|
|
|
if (ackCalled) {
|
|
if (ackCalled) {
|
|
|
- return;
|
|
|
|
|
|
|
+ return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ ackCalled = true;
|
|
|
|
|
+
|
|
|
if (response instanceof Error) {
|
|
if (response instanceof Error) {
|
|
|
- res.status(500).send();
|
|
|
|
|
|
|
+ const message = response.message || 'Error occurred';
|
|
|
|
|
+ throw new Error(message);
|
|
|
}
|
|
}
|
|
|
else if (!response) {
|
|
else if (!response) {
|
|
|
- res.send('');
|
|
|
|
|
|
|
+ return null;
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
- res.send(response);
|
|
|
|
|
|
|
+ return response;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- ackCalled = true;
|
|
|
|
|
},
|
|
},
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -66,7 +72,7 @@ class BoltService {
|
|
|
this.client = client;
|
|
this.client = client;
|
|
|
|
|
|
|
|
if (token != null || signingSecret != null) {
|
|
if (token != null || signingSecret != null) {
|
|
|
- logger.debug('TwitterStrategy: setup is done');
|
|
|
|
|
|
|
+ logger.debug('SlackBot: setup is done');
|
|
|
this.bolt = new App({
|
|
this.bolt = new App({
|
|
|
token,
|
|
token,
|
|
|
signingSecret,
|
|
signingSecret,
|
|
@@ -101,11 +107,11 @@ class BoltService {
|
|
|
|
|
|
|
|
switch (firstArg) {
|
|
switch (firstArg) {
|
|
|
case 'search':
|
|
case 'search':
|
|
|
- this.searchResults(command, args);
|
|
|
|
|
|
|
+ await this.searchResults(command, args);
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
case 'create':
|
|
case 'create':
|
|
|
- this.createModal(command, client, body);
|
|
|
|
|
|
|
+ await this.createModal(command, client, body);
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
default:
|
|
@@ -114,9 +120,11 @@ class BoltService {
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- this.bolt.view('createPage', async({ ack, view }) => {
|
|
|
|
|
|
|
+ this.bolt.view('createPage', async({
|
|
|
|
|
+ ack, view, body, client,
|
|
|
|
|
+ }) => {
|
|
|
await ack();
|
|
await ack();
|
|
|
- return this.createPageInGrowi(view);
|
|
|
|
|
|
|
+ await this.createPageInGrowi(view, body);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
this.bolt.action('shareSearchResults', async({
|
|
this.bolt.action('shareSearchResults', async({
|
|
@@ -129,27 +137,28 @@ class BoltService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
notCommand(command) {
|
|
notCommand(command) {
|
|
|
- logger.error('Input first arguments');
|
|
|
|
|
- return this.client.chat.postEphemeral({
|
|
|
|
|
|
|
+ logger.error('Invalid first argument');
|
|
|
|
|
+ this.client.chat.postEphemeral({
|
|
|
channel: command.channel_id,
|
|
channel: command.channel_id,
|
|
|
user: command.user_id,
|
|
user: command.user_id,
|
|
|
blocks: [
|
|
blocks: [
|
|
|
this.generateMarkdownSectionBlock('*No command.*\n Hint\n `/growi [command] [keyword]`'),
|
|
this.generateMarkdownSectionBlock('*No command.*\n Hint\n `/growi [command] [keyword]`'),
|
|
|
],
|
|
],
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+ throw new Error('/growi command: Invalid first argument');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async getSearchResultPaths(command, args) {
|
|
async getSearchResultPaths(command, args) {
|
|
|
const firstKeyword = args[1];
|
|
const firstKeyword = args[1];
|
|
|
if (firstKeyword == null) {
|
|
if (firstKeyword == null) {
|
|
|
- return this.client.chat.postEphemeral({
|
|
|
|
|
|
|
+ this.client.chat.postEphemeral({
|
|
|
channel: command.channel_id,
|
|
channel: command.channel_id,
|
|
|
user: command.user_id,
|
|
user: command.user_id,
|
|
|
blocks: [
|
|
blocks: [
|
|
|
this.generateMarkdownSectionBlock('*Input keywords.*\n Hint\n `/growi search [keyword]`'),
|
|
this.generateMarkdownSectionBlock('*Input keywords.*\n Hint\n `/growi search [keyword]`'),
|
|
|
],
|
|
],
|
|
|
});
|
|
});
|
|
|
|
|
+ throw new Error('/growi command:search: Invalid keyword');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// remove leading 'search'.
|
|
// remove leading 'search'.
|
|
@@ -161,13 +170,13 @@ class BoltService {
|
|
|
|
|
|
|
|
// no search results
|
|
// no search results
|
|
|
if (results.data.length === 0) {
|
|
if (results.data.length === 0) {
|
|
|
- return this.client.chat.postEphemeral({
|
|
|
|
|
|
|
+ logger.info(`No page found with "${keywords}"`);
|
|
|
|
|
+ this.client.chat.postEphemeral({
|
|
|
channel: command.channel_id,
|
|
channel: command.channel_id,
|
|
|
user: command.user_id,
|
|
user: command.user_id,
|
|
|
- blocks: [
|
|
|
|
|
- this.generateMarkdownSectionBlock('*No page that match your keywords.*'),
|
|
|
|
|
- ],
|
|
|
|
|
|
|
+ blocks: [this.generateMarkdownSectionBlock('*No page that match your keywords.*')],
|
|
|
});
|
|
});
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const resultPaths = results.data.map((data) => {
|
|
const resultPaths = results.data.map((data) => {
|
|
@@ -214,20 +223,26 @@ class BoltService {
|
|
|
this.generateMarkdownSectionBlock('*Failed to search.*\n Hint\n `/growi search [keyword]`'),
|
|
this.generateMarkdownSectionBlock('*Failed to search.*\n Hint\n `/growi search [keyword]`'),
|
|
|
],
|
|
],
|
|
|
});
|
|
});
|
|
|
|
|
+ throw new Error('/growi command:search: Failed to search');
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async createModal(command, client, body) {
|
|
async createModal(command, client, body) {
|
|
|
const User = this.crowi.model('User');
|
|
const User = this.crowi.model('User');
|
|
|
|
|
+ const slackUser = await User.findUserByUsername('slackUser');
|
|
|
|
|
|
|
|
- try {
|
|
|
|
|
- const slackUser = await User.findUserByUsername('slackUser');
|
|
|
|
|
-
|
|
|
|
|
- // if "slackUser" is null, don't show create Modal
|
|
|
|
|
- if (slackUser == null) {
|
|
|
|
|
- throw new Error('userNull');
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // if "slackUser" is null, don't show create Modal
|
|
|
|
|
+ if (slackUser == null) {
|
|
|
|
|
+ logger.error('Failed to create a page because slackUser is not found.');
|
|
|
|
|
+ this.client.chat.postEphemeral({
|
|
|
|
|
+ channel: command.channel_id,
|
|
|
|
|
+ user: command.user_id,
|
|
|
|
|
+ blocks: [this.generateMarkdownSectionBlock('*slackUser does not exist.*')],
|
|
|
|
|
+ });
|
|
|
|
|
+ throw new Error('/growi command:create: slackUser is not found');
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ try {
|
|
|
await client.views.open({
|
|
await client.views.open({
|
|
|
trigger_id: body.trigger_id,
|
|
trigger_id: body.trigger_id,
|
|
|
|
|
|
|
@@ -254,18 +269,8 @@ class BoltService {
|
|
|
},
|
|
},
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
- catch (e) {
|
|
|
|
|
- if (e instanceof Error) {
|
|
|
|
|
- return this.client.chat.postEphemeral({
|
|
|
|
|
- channel: command.channel_id,
|
|
|
|
|
- user: command.user_id,
|
|
|
|
|
- blocks: [
|
|
|
|
|
- this.generateMarkdownSectionBlock('*slackUser does not exist.*'),
|
|
|
|
|
- ],
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- logger.error('Failed to create page.');
|
|
|
|
|
|
|
+ catch (err) {
|
|
|
|
|
+ logger.error('Failed to create a page.');
|
|
|
await this.client.chat.postEphemeral({
|
|
await this.client.chat.postEphemeral({
|
|
|
channel: command.channel_id,
|
|
channel: command.channel_id,
|
|
|
user: command.user_id,
|
|
user: command.user_id,
|
|
@@ -273,31 +278,38 @@ class BoltService {
|
|
|
this.generateMarkdownSectionBlock('*Failed to create new page.*\n Hint\n `/growi create`'),
|
|
this.generateMarkdownSectionBlock('*Failed to create new page.*\n Hint\n `/growi create`'),
|
|
|
],
|
|
],
|
|
|
});
|
|
});
|
|
|
|
|
+ throw err;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Submit action in create Modal
|
|
// Submit action in create Modal
|
|
|
- async createPageInGrowi(view) {
|
|
|
|
|
|
|
+ async createPageInGrowi(view, body) {
|
|
|
const User = this.crowi.model('User');
|
|
const User = this.crowi.model('User');
|
|
|
const Page = this.crowi.model('Page');
|
|
const Page = this.crowi.model('Page');
|
|
|
const pathUtils = require('growi-commons').pathUtils;
|
|
const pathUtils = require('growi-commons').pathUtils;
|
|
|
|
|
|
|
|
|
|
+ const contentsBody = view.state.values.contents.contents_input.value;
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
// search "slackUser" to create page in slack
|
|
// search "slackUser" to create page in slack
|
|
|
const slackUser = await User.findUserByUsername('slackUser');
|
|
const slackUser = await User.findUserByUsername('slackUser');
|
|
|
|
|
|
|
|
let path = view.state.values.path.path_input.value;
|
|
let path = view.state.values.path.path_input.value;
|
|
|
- const body = view.state.values.contents.contents_input.value;
|
|
|
|
|
-
|
|
|
|
|
// sanitize path
|
|
// sanitize path
|
|
|
path = this.crowi.xss.process(path);
|
|
path = this.crowi.xss.process(path);
|
|
|
path = pathUtils.normalizePath(path);
|
|
path = pathUtils.normalizePath(path);
|
|
|
|
|
|
|
|
const user = slackUser._id;
|
|
const user = slackUser._id;
|
|
|
- return Page.create(path, body, user, {});
|
|
|
|
|
|
|
+ await Page.create(path, contentsBody, user, {});
|
|
|
}
|
|
}
|
|
|
- catch {
|
|
|
|
|
|
|
+ catch (err) {
|
|
|
|
|
+ this.client.chat.postMessage({
|
|
|
|
|
+ channel: body.user.id,
|
|
|
|
|
+ blocks: [
|
|
|
|
|
+ this.generateMarkdownSectionBlock(`Cannot create new page to existed path\n *Contents* :memo:\n ${contentsBody}`)],
|
|
|
|
|
+ });
|
|
|
logger.error('Failed to create page in GROWI.');
|
|
logger.error('Failed to create page in GROWI.');
|
|
|
|
|
+ throw err;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|