From a58a59788d21b930e9a578a606cd82eaf23b96e0 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Thu, 6 Oct 2022 11:02:07 +0300 Subject: [PATCH] refactor: Redesign slack actions --- .../actions/find-message/find-message.ts | 45 ++++++++++ .../apps/slack2/actions/find-message/index.ts | 90 +++++++++++++++++++ .../send-a-message-to-channel/index.ts | 59 ++++++++++++ .../send-a-message-to-channel/post-message.ts | 37 ++++++++ 4 files changed, 231 insertions(+) create mode 100644 packages/backend/src/apps/slack2/actions/find-message/find-message.ts create mode 100644 packages/backend/src/apps/slack2/actions/find-message/index.ts create mode 100644 packages/backend/src/apps/slack2/actions/send-a-message-to-channel/index.ts create mode 100644 packages/backend/src/apps/slack2/actions/send-a-message-to-channel/post-message.ts diff --git a/packages/backend/src/apps/slack2/actions/find-message/find-message.ts b/packages/backend/src/apps/slack2/actions/find-message/find-message.ts new file mode 100644 index 00000000..d6d55409 --- /dev/null +++ b/packages/backend/src/apps/slack2/actions/find-message/find-message.ts @@ -0,0 +1,45 @@ +import { IGlobalVariable } from '@automatisch/types'; + +type FindMessageOptions = { + query: string; + sortBy: string; + sortDirection: string; + count: number; +}; + +const findMessage = async ($: IGlobalVariable, options: FindMessageOptions) => { + const headers = { + Authorization: `Bearer ${$.auth.data.accessToken}`, + }; + + const params = { + query: options.query, + sort: options.sortBy, + sort_dir: options.sortDirection, + count: options.count || 1, + }; + + const response = await $.http.get('/search.messages', { + headers, + params, + }); + + const data = response.data; + + if (!data.ok) { + if (data.error === 'missing_scope') { + throw new Error( + `Error occured while finding messages; ${data.error}: ${data.needed}` + ); + } + + throw new Error(`Error occured while finding messages; ${data.error}`); + } + + const messages = data.messages.matches; + const message = messages?.[0]; + + return message; +}; + +export default findMessage; diff --git a/packages/backend/src/apps/slack2/actions/find-message/index.ts b/packages/backend/src/apps/slack2/actions/find-message/index.ts new file mode 100644 index 00000000..c6132e2c --- /dev/null +++ b/packages/backend/src/apps/slack2/actions/find-message/index.ts @@ -0,0 +1,90 @@ +import { IGlobalVariable } from '@automatisch/types'; +import findMessage from './find-message'; + +export default { + name: 'Find message', + key: 'findMessage', + description: 'Find a Slack message using the Slack Search feature.', + substeps: [ + { + key: 'chooseConnection', + name: 'Choose connection', + }, + { + key: 'setupAction', + name: 'Set up action', + arguments: [ + { + label: 'Search Query', + key: 'query', + type: 'string', + required: true, + description: + 'Search query to use for finding matching messages. See the Slack Search Documentation for more information on constructing a query.', + variables: true, + }, + { + label: 'Sort by', + key: 'sortBy', + type: 'dropdown', + description: + 'Sort messages by their match strength or by their date. Default is score.', + required: true, + value: 'score', + variables: false, + options: [ + { + label: 'Match strength', + value: 'score', + }, + { + label: 'Message date time', + value: 'timestamp', + }, + ], + }, + { + label: 'Sort direction', + key: 'sortDirection', + type: 'dropdown', + description: + 'Sort matching messages in ascending or descending order. Default is descending.', + required: true, + value: 'desc', + variables: false, + options: [ + { + label: 'Descending (newest or best match first)', + value: 'desc', + }, + { + label: 'Ascending (oldest or worst match first)', + value: 'asc', + }, + ], + }, + ], + }, + { + key: 'testStep', + name: 'Test action', + }, + ], + + async run($: IGlobalVariable) { + const parameters = $.db.step.parameters; + const query = parameters.query as string; + const sortBy = parameters.sortBy as string; + const sortDirection = parameters.sortDirection as string; + const count = 1; + + const messages = await findMessage($, { + query, + sortBy, + sortDirection, + count, + }); + + return messages; + }, +}; diff --git a/packages/backend/src/apps/slack2/actions/send-a-message-to-channel/index.ts b/packages/backend/src/apps/slack2/actions/send-a-message-to-channel/index.ts new file mode 100644 index 00000000..d2033c1e --- /dev/null +++ b/packages/backend/src/apps/slack2/actions/send-a-message-to-channel/index.ts @@ -0,0 +1,59 @@ +import { IGlobalVariable } from '@automatisch/types'; +import postMessage from './post-message'; + +export default { + name: 'Send a message to channel', + key: 'sendMessageToChannel', + description: 'Send a message to a specific channel you specify.', + substeps: [ + { + key: 'chooseConnection', + name: 'Choose connection', + }, + { + key: 'setupAction', + name: 'Set up action', + arguments: [ + { + label: 'Channel', + key: 'channel', + type: 'dropdown', + required: true, + description: 'Pick a channel to send the message to.', + variables: false, + source: { + type: 'query', + name: 'getData', + arguments: [ + { + name: 'key', + value: 'listChannels', + }, + ], + }, + }, + { + label: 'Message text', + key: 'message', + type: 'string', + required: true, + description: 'The content of your new message.', + variables: true, + }, + ], + }, + { + key: 'testStep', + name: 'Test action', + }, + ], + + async run($: IGlobalVariable) { + const channelId = $.db.step.parameters.channel as string; + const text = $.db.step.parameters.message as string; + + const message = await postMessage($, channelId, text); + + return message; + }, +}; diff --git a/packages/backend/src/apps/slack2/actions/send-a-message-to-channel/post-message.ts b/packages/backend/src/apps/slack2/actions/send-a-message-to-channel/post-message.ts new file mode 100644 index 00000000..8a499767 --- /dev/null +++ b/packages/backend/src/apps/slack2/actions/send-a-message-to-channel/post-message.ts @@ -0,0 +1,37 @@ +import { IGlobalVariable, IJSONObject } from '@automatisch/types'; + +const postMessage = async ( + $: IGlobalVariable, + channelId: string, + text: string +) => { + const message: { + data: IJSONObject | null | undefined; + error: IJSONObject | null | undefined; + } = { + data: null, + error: null, + }; + + const headers = { + Authorization: `Bearer ${$.auth.data.accessToken}`, + }; + + const params = { + channel: channelId, + text, + }; + + const response = await $.http.post('/chat.postMessage', params, { headers }); + + message.error = response?.integrationError; + message.data = response?.data?.message; + + if (response.data.ok === false) { + message.error = response.data; + } + + return message; +}; + +export default postMessage;