refactor: Redesign slack actions

This commit is contained in:
Faruk AYDIN
2022-10-06 11:02:07 +03:00
parent 77624acc01
commit a58a59788d
4 changed files with 231 additions and 0 deletions

View File

@@ -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;

View File

@@ -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;
},
};

View File

@@ -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;
},
};

View File

@@ -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;