| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562 |
- import loggerFactory from '~/utils/logger';
- import { S2sMessagingService } from './s2s-messaging/base';
- import { S2sMessageHandlable } from './s2s-messaging/handlable';
- const logger = loggerFactory('growi:service:SlackBotService');
- const mongoose = require('mongoose');
- const axios = require('axios');
- const { markdownSectionBlock, divider } = require('@growi/slack');
- const { reshapeContentsBody } = require('@growi/slack');
- const { formatDistanceStrict, parse, format } = require('date-fns');
- const S2sMessage = require('../models/vo/s2s-message');
- const PAGINGLIMIT = 10;
- class SlackBotService implements S2sMessageHandlable {
- crowi!: any;
- s2sMessagingService!: S2sMessagingService;
- lastLoadedAt?: Date;
- constructor(crowi) {
- this.crowi = crowi;
- this.s2sMessagingService = crowi.s2sMessagingService;
- this.initialize();
- }
- initialize() {
- this.lastLoadedAt = new Date();
- }
- /**
- * @inheritdoc
- */
- shouldHandleS2sMessage(s2sMessage) {
- const { eventName, updatedAt } = s2sMessage;
- if (eventName !== 'slackBotServiceUpdated' || updatedAt == null) {
- return false;
- }
- return this.lastLoadedAt == null || this.lastLoadedAt < new Date(s2sMessage.updatedAt);
- }
- /**
- * @inheritdoc
- */
- async handleS2sMessage() {
- const { configManager } = this.crowi;
- logger.info('Reset slack bot by pubsub notification');
- await configManager.loadConfigs();
- this.initialize();
- }
- async publishUpdatedMessage() {
- const { s2sMessagingService } = this;
- if (s2sMessagingService != null) {
- const s2sMessage = new S2sMessage('slackBotServiceUpdated', { updatedAt: new Date() });
- try {
- await s2sMessagingService.publish(s2sMessage);
- }
- catch (e) {
- logger.error('Failed to publish update message with S2sMessagingService: ', e.message);
- }
- }
- }
- /**
- * Handle /commands endpoint
- */
- async handleCommand(command, client, body, ...opt) {
- const module = `./slack-command-handler/${command}`;
- try {
- const handler = require(module)(this.crowi);
- await handler.handleCommand(client, body, ...opt);
- }
- catch (err) {
- this.notCommand(client, body);
- }
- }
- async notCommand(client, body) {
- logger.error('Invalid first argument');
- client.chat.postEphemeral({
- channel: body.channel_id,
- user: body.user_id,
- text: 'No command',
- blocks: [
- markdownSectionBlock('*No command.*\n Hint\n `/growi [command] [keyword]`'),
- ],
- });
- return;
- }
- generatePageLinkMrkdwn(pathname, href) {
- return `<${decodeURI(href)} | ${decodeURI(pathname)}>`;
- }
- appendSpeechBaloon(mrkdwn, commentCount) {
- return (commentCount != null && commentCount > 0)
- ? `${mrkdwn} :speech_balloon: ${commentCount}`
- : mrkdwn;
- }
- generateLastUpdateMrkdwn(updatedAt, baseDate) {
- if (updatedAt != null) {
- // cast to date
- const date = new Date(updatedAt);
- return formatDistanceStrict(date, baseDate);
- }
- return '';
- }
- async shareSinglePage(client, payload) {
- const { channel, user, actions } = payload;
- const appUrl = this.crowi.appService.getSiteUrl();
- const appTitle = this.crowi.appService.getAppTitle();
- const channelId = channel.id;
- const action = actions[0]; // shareSinglePage action must have button action
- // restore page data from value
- const { page, href, pathname } = JSON.parse(action.value);
- const { updatedAt, commentCount } = page;
- // share
- const now = new Date();
- return client.chat.postMessage({
- channel: channelId,
- blocks: [
- { type: 'divider' },
- markdownSectionBlock(`${this.appendSpeechBaloon(`*${this.generatePageLinkMrkdwn(pathname, href)}*`, commentCount)}`),
- {
- type: 'context',
- elements: [
- {
- type: 'mrkdwn',
- text: `<${decodeURI(appUrl)}|*${appTitle}*> | Last updated: ${this.generateLastUpdateMrkdwn(updatedAt, now)} | Shared by *${user.username}*`,
- },
- ],
- },
- ],
- });
- }
- async dismissSearchResults(client, payload) {
- const { response_url: responseUrl } = payload;
- return axios.post(responseUrl, {
- delete_original: true,
- });
- }
- async showEphemeralSearchResults(client, body, args, offsetNum) {
- let searchResult;
- try {
- searchResult = await this.retrieveSearchResults(client, body, args, offsetNum);
- }
- catch (err) {
- logger.error('Failed to get search results.', err);
- await client.chat.postEphemeral({
- channel: body.channel_id,
- user: body.user_id,
- text: 'Failed To Search',
- blocks: [
- markdownSectionBlock('*Failed to search.*\n Hint\n `/growi search [keyword]`'),
- ],
- });
- throw new Error('/growi command:search: Failed to search');
- }
- const appUrl = this.crowi.appService.getSiteUrl();
- const appTitle = this.crowi.appService.getAppTitle();
- const {
- pages, offset, resultsTotal,
- } = searchResult;
- const keywords = this.getKeywords(args);
- let searchResultsDesc;
- switch (resultsTotal) {
- case 1:
- searchResultsDesc = `*${resultsTotal}* page is found.`;
- break;
- default:
- searchResultsDesc = `*${resultsTotal}* pages are found.`;
- break;
- }
- const contextBlock = {
- type: 'context',
- elements: [
- {
- type: 'mrkdwn',
- text: `keyword(s) : *"${keywords}"* | Current: ${offset + 1} - ${offset + pages.length} | Total ${resultsTotal} pages`,
- },
- ],
- };
- const now = new Date();
- const blocks = [
- markdownSectionBlock(`:mag: <${decodeURI(appUrl)}|*${appTitle}*>\n${searchResultsDesc}`),
- contextBlock,
- { type: 'divider' },
- // create an array by map and extract
- ...pages.map((page) => {
- const { path, updatedAt, commentCount } = page;
- // generate URL
- const url = new URL(path, appUrl);
- const { href, pathname } = url;
- return {
- type: 'section',
- text: {
- type: 'mrkdwn',
- text: `${this.appendSpeechBaloon(`*${this.generatePageLinkMrkdwn(pathname, href)}*`, commentCount)}`
- + `\n Last updated: ${this.generateLastUpdateMrkdwn(updatedAt, now)}`,
- },
- accessory: {
- type: 'button',
- action_id: 'shareSingleSearchResult',
- text: {
- type: 'plain_text',
- text: 'Share',
- },
- value: JSON.stringify({ page, href, pathname }),
- },
- };
- }),
- { type: 'divider' },
- contextBlock,
- ];
- // DEFAULT show "Share" button
- // const actionBlocks = {
- // type: 'actions',
- // elements: [
- // {
- // type: 'button',
- // text: {
- // type: 'plain_text',
- // text: 'Share',
- // },
- // style: 'primary',
- // action_id: 'shareSearchResults',
- // },
- // ],
- // };
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- const actionBlocks: any = {
- type: 'actions',
- elements: [
- {
- type: 'button',
- text: {
- type: 'plain_text',
- text: 'Dismiss',
- },
- style: 'danger',
- action_id: 'dismissSearchResults',
- },
- ],
- };
- // show "Next" button if next page exists
- if (resultsTotal > offset + PAGINGLIMIT) {
- actionBlocks.elements.unshift(
- {
- type: 'button',
- text: {
- type: 'plain_text',
- text: 'Next',
- },
- action_id: 'showNextResults',
- value: JSON.stringify({ offset, body, args }),
- },
- );
- }
- blocks.push(actionBlocks);
- try {
- await client.chat.postEphemeral({
- channel: body.channel_id,
- user: body.user_id,
- text: 'Successed To Search',
- blocks,
- });
- }
- catch (err) {
- logger.error('Failed to post ephemeral message.', err);
- await client.chat.postEphemeral({
- channel: body.channel_id,
- user: body.user_id,
- text: 'Failed to post ephemeral message.',
- blocks: [
- markdownSectionBlock(err.toString()),
- ],
- });
- throw new Error(err);
- }
- }
- async retrieveSearchResults(client, body, args, offset = 0) {
- const firstKeyword = args[1];
- if (firstKeyword == null) {
- client.chat.postEphemeral({
- channel: body.channel_id,
- user: body.user_id,
- text: 'Input keywords',
- blocks: [
- markdownSectionBlock('*Input keywords.*\n Hint\n `/growi search [keyword]`'),
- ],
- });
- return;
- }
- const keywords = this.getKeywords(args);
- const { searchService } = this.crowi;
- const options = { limit: 10, offset };
- const results = await searchService.searchKeyword(keywords, null, {}, options);
- const resultsTotal = results.meta.total;
- // no search results
- if (results.data.length === 0) {
- logger.info(`No page found with "${keywords}"`);
- client.chat.postEphemeral({
- channel: body.channel_id,
- user: body.user_id,
- text: `No page found with "${keywords}"`,
- blocks: [
- markdownSectionBlock(`*No page that matches your keyword(s) "${keywords}".*`),
- markdownSectionBlock(':mag: *Help: Searching*'),
- divider(),
- markdownSectionBlock('`word1` `word2` (divide with space) \n Search pages that include both word1, word2 in the title or body'),
- divider(),
- markdownSectionBlock('`"This is GROWI"` (surround with double quotes) \n Search pages that include the phrase "This is GROWI"'),
- divider(),
- markdownSectionBlock('`-keyword` \n Exclude pages that include keyword in the title or body'),
- divider(),
- markdownSectionBlock('`prefix:/user/` \n Search only the pages that the title start with /user/'),
- divider(),
- markdownSectionBlock('`-prefix:/user/` \n Exclude the pages that the title start with /user/'),
- divider(),
- markdownSectionBlock('`tag:wiki` \n Search for pages with wiki tag'),
- divider(),
- markdownSectionBlock('`-tag:wiki` \n Exclude pages with wiki tag'),
- ],
- });
- return { pages: [] };
- }
- const pages = results.data.map((data) => {
- const { path, updated_at: updatedAt, comment_count: commentCount } = data._source;
- return { path, updatedAt, commentCount };
- });
- return {
- pages, offset, resultsTotal,
- };
- }
- getKeywords(args) {
- const keywordsArr = args.slice(1);
- const keywords = keywordsArr.join(' ');
- return keywords;
- }
- // Submit action in create Modal
- async createPage(client, payload, path, channelId, contentsBody) {
- const Page = this.crowi.model('Page');
- const pathUtils = require('growi-commons').pathUtils;
- const reshapedContentsBody = reshapeContentsBody(contentsBody);
- try {
- // sanitize path
- const sanitizedPath = this.crowi.xss.process(path);
- const normalizedPath = pathUtils.normalizePath(sanitizedPath);
- // generate a dummy id because Operation to create a page needs ObjectId
- const dummyObjectIdOfUser = new mongoose.Types.ObjectId();
- const page = await Page.create(normalizedPath, reshapedContentsBody, dummyObjectIdOfUser, {});
- // Send a message when page creation is complete
- const growiUri = this.crowi.appService.getSiteUrl();
- await client.chat.postEphemeral({
- channel: channelId,
- user: payload.user.id,
- text: `The page <${decodeURI(`${growiUri}/${page._id} | ${decodeURI(growiUri + normalizedPath)}`)}> has been created.`,
- });
- }
- catch (err) {
- client.chat.postMessage({
- channel: payload.user.id,
- blocks: [
- markdownSectionBlock(`Cannot create new page to existed path\n *Contents* :memo:\n ${reshapedContentsBody}`)],
- });
- logger.error('Failed to create page in GROWI.');
- throw err;
- }
- }
- async createPageInGrowi(client, payload) {
- const path = payload.view.state.values.path.path_input.value;
- const channelId = JSON.parse(payload.view.private_metadata).channelId;
- const contentsBody = payload.view.state.values.contents.contents_input.value;
- await this.createPage(client, payload, path, channelId, contentsBody);
- }
- async togetterCreatePageInGrowi(client, payload) {
- let result = [];
- const channel = payload.channel.id;
- try {
- // validate form
- const { path, oldest, latest } = await this.togetterValidateForm(client, payload);
- // get messages
- result = await this.togetterGetMessages(client, payload, channel, path, latest, oldest);
- // clean messages
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- const cleanedContents = await this.togetterCleanMessages((result as any).messages);
- const contentsBody = cleanedContents.join('');
- // create and send url message
- await this.togetterCreatePageAndSendPreview(client, payload, path, channel, contentsBody);
- }
- catch (err) {
- await client.chat.postMessage({
- channel: payload.user.id,
- text: err.message,
- blocks: [
- markdownSectionBlock(err.message),
- ],
- });
- return;
- }
- }
- async togetterGetMessages(client, payload, channel, path, latest, oldest) {
- const result = await client.conversations.history({
- channel,
- latest,
- oldest,
- limit: 100,
- inclusive: true,
- });
- // return if no message found
- if (!result.messages.length) {
- throw new Error('No message found from togetter command. Try again.');
- }
- return result;
- }
- async togetterValidateForm(client, payload) {
- const grwTzoffset = this.crowi.appService.getTzoffset() * 60;
- const path = payload.state.values.page_path.page_path.value;
- let oldest = payload.state.values.oldest.oldest.value;
- let latest = payload.state.values.latest.latest.value;
- oldest = oldest.trim();
- latest = latest.trim();
- if (!path) {
- throw new Error('Page path is required.');
- }
- /**
- * RegExp for datetime yyyy/MM/dd-HH:mm
- * @see https://regex101.com/r/XbxdNo/1
- */
- const regexpDatetime = new RegExp(/^[12]\d\d\d\/(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])-([01][0-9]|2[0123]):[0-5][0-9]$/);
- if (!regexpDatetime.test(oldest)) {
- throw new Error('Datetime format for oldest must be yyyy/MM/dd-HH:mm');
- }
- if (!regexpDatetime.test(latest)) {
- throw new Error('Datetime format for latest must be yyyy/MM/dd-HH:mm');
- }
- oldest = parse(oldest, 'yyyy/MM/dd-HH:mm', new Date()).getTime() / 1000 + grwTzoffset;
- // + 60s in order to include messages between hh:mm.00s and hh:mm.59s
- latest = parse(latest, 'yyyy/MM/dd-HH:mm', new Date()).getTime() / 1000 + grwTzoffset + 60;
- if (oldest > latest) {
- throw new Error('Oldest datetime must be older than the latest date time.');
- }
- return { path, oldest, latest };
- }
- async togetterCleanMessages(messages) {
- const cleanedContents: string[] = [];
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- let lastMessage: any = {};
- const grwTzoffset = this.crowi.appService.getTzoffset() * 60;
- messages
- .sort((a, b) => {
- return a.ts - b.ts;
- })
- .forEach((message) => {
- // increment contentsBody while removing the same headers
- // exclude header
- const lastMessageTs = Math.floor(lastMessage.ts / 60);
- const messageTs = Math.floor(message.ts / 60);
- if (lastMessage.user === message.user && lastMessageTs === messageTs) {
- cleanedContents.push(`${message.text}\n`);
- }
- // include header
- else {
- const ts = (parseInt(message.ts) - grwTzoffset) * 1000;
- const time = format(new Date(ts), 'h:mm a');
- cleanedContents.push(`${message.user} ${time}\n${message.text}\n`);
- lastMessage = message;
- }
- });
- return cleanedContents;
- }
- async togetterCreatePageAndSendPreview(client, payload, path, channel, contentsBody) {
- try {
- await this.createPage(client, payload, path, channel, contentsBody);
- // send preview to dm
- await client.chat.postMessage({
- channel: payload.user.id,
- text: 'Preview from togetter command',
- blocks: [
- markdownSectionBlock('*Preview*'),
- divider(),
- markdownSectionBlock(contentsBody),
- divider(),
- ],
- });
- // dismiss message
- const responseUrl = payload.response_url;
- axios.post(responseUrl, {
- delete_original: true,
- });
- }
- catch (err) {
- throw new Error('Error occurred while creating a page.');
- }
- }
- async togetterCancel(client, payload) {
- const responseUrl = payload.response_url;
- axios.post(responseUrl, {
- delete_original: true,
- });
- }
- }
- module.exports = SlackBotService;
|