2020-03-05 15:25:18 -08:00
|
|
|
import {
|
2020-03-08 15:22:33 -07:00
|
|
|
BINARY_ENCODING,
|
2020-05-05 10:22:02 -07:00
|
|
|
IExecuteFunctions,
|
2020-05-11 16:56:27 -07:00
|
|
|
} from 'n8n-core';
|
2020-05-05 10:22:02 -07:00
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
import {
|
2021-09-11 01:15:36 -07:00
|
|
|
ICredentialsDecrypted,
|
|
|
|
ICredentialTestFunctions,
|
2019-06-23 03:35:23 -07:00
|
|
|
IDataObject,
|
2020-03-05 15:25:18 -08:00
|
|
|
ILoadOptionsFunctions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeExecutionData,
|
2020-03-05 15:25:18 -08:00
|
|
|
INodePropertyOptions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2021-09-11 01:15:36 -07:00
|
|
|
NodeCredentialTestResult,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeOperationError,
|
2019-06-23 03:35:23 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2020-03-05 15:25:18 -08:00
|
|
|
import {
|
|
|
|
channelFields,
|
2020-03-15 07:51:49 -07:00
|
|
|
channelOperations,
|
2020-03-05 15:25:18 -08:00
|
|
|
} from './ChannelDescription';
|
2020-10-01 22:31:41 -07:00
|
|
|
|
2020-03-05 15:25:18 -08:00
|
|
|
import {
|
|
|
|
messageFields,
|
2020-03-15 07:51:49 -07:00
|
|
|
messageOperations,
|
2020-03-05 15:25:18 -08:00
|
|
|
} from './MessageDescription';
|
2020-10-01 22:31:41 -07:00
|
|
|
|
2020-03-05 15:25:18 -08:00
|
|
|
import {
|
2020-03-08 15:22:33 -07:00
|
|
|
starFields,
|
2020-03-15 07:51:49 -07:00
|
|
|
starOperations,
|
2020-03-08 15:22:33 -07:00
|
|
|
} from './StarDescription';
|
2020-10-01 22:31:41 -07:00
|
|
|
|
2020-03-08 15:22:33 -07:00
|
|
|
import {
|
|
|
|
fileFields,
|
2020-03-15 07:51:49 -07:00
|
|
|
fileOperations,
|
2020-03-08 15:22:33 -07:00
|
|
|
} from './FileDescription';
|
2020-10-01 22:31:41 -07:00
|
|
|
|
2020-12-13 01:47:52 -08:00
|
|
|
import {
|
|
|
|
reactionFields,
|
|
|
|
reactionOperations,
|
|
|
|
} from './ReactionDescription';
|
|
|
|
|
2021-11-03 17:55:04 -07:00
|
|
|
import {
|
|
|
|
userGroupFields,
|
|
|
|
userGroupOperations,
|
|
|
|
} from './UserGroupDescription';
|
|
|
|
|
2020-12-13 01:47:52 -08:00
|
|
|
import {
|
|
|
|
userFields,
|
|
|
|
userOperations,
|
|
|
|
} from './UserDescription';
|
|
|
|
|
2020-10-01 22:31:41 -07:00
|
|
|
import {
|
|
|
|
userProfileFields,
|
|
|
|
userProfileOperations,
|
|
|
|
} from './UserProfileDescription';
|
|
|
|
|
2020-03-05 15:25:18 -08:00
|
|
|
import {
|
|
|
|
slackApiRequest,
|
2020-03-15 07:51:49 -07:00
|
|
|
slackApiRequestAllItems,
|
2020-05-05 10:22:02 -07:00
|
|
|
validateJSON,
|
2020-03-05 15:25:18 -08:00
|
|
|
} from './GenericFunctions';
|
2020-10-01 22:31:41 -07:00
|
|
|
|
2020-03-05 15:25:18 -08:00
|
|
|
import {
|
|
|
|
IAttachment,
|
|
|
|
} from './MessageInterface';
|
2020-12-13 01:47:52 -08:00
|
|
|
|
2020-10-01 22:31:41 -07:00
|
|
|
import moment = require('moment');
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
interface Attachment {
|
|
|
|
fields: {
|
|
|
|
item?: object[];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-05 10:22:02 -07:00
|
|
|
interface Text {
|
|
|
|
type?: string;
|
|
|
|
text?: string;
|
|
|
|
emoji?: boolean;
|
|
|
|
verbatim?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Confirm {
|
|
|
|
title?: Text;
|
|
|
|
text?: Text;
|
|
|
|
confirm?: Text;
|
|
|
|
deny?: Text;
|
|
|
|
style?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Element {
|
|
|
|
type?: string;
|
|
|
|
text?: Text;
|
|
|
|
action_id?: string;
|
|
|
|
url?: string;
|
|
|
|
value?: string;
|
|
|
|
style?: string;
|
|
|
|
confirm?: Confirm;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Block {
|
|
|
|
type?: string;
|
|
|
|
elements?: Element[];
|
|
|
|
block_id?: string;
|
|
|
|
text?: Text;
|
|
|
|
fields?: Text[];
|
|
|
|
accessory?: Element;
|
|
|
|
}
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
export class Slack implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Slack',
|
|
|
|
name: 'slack',
|
2021-01-22 12:23:04 -08:00
|
|
|
icon: 'file:slack.svg',
|
2019-06-23 03:35:23 -07:00
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
2019-07-26 04:13:01 -07:00
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2020-03-08 15:22:33 -07:00
|
|
|
description: 'Consume Slack API',
|
2019-06-23 03:35:23 -07:00
|
|
|
defaults: {
|
|
|
|
name: 'Slack',
|
2021-01-22 12:23:04 -08:00
|
|
|
color: '#E01E5A',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'slackApi',
|
|
|
|
required: true,
|
2019-07-14 09:43:58 -07:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2020-03-05 15:25:18 -08:00
|
|
|
authentication: [
|
|
|
|
'accessToken',
|
2019-07-14 09:43:58 -07:00
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
2021-09-11 01:15:36 -07:00
|
|
|
testedBy: 'testSlackTokenAuth',
|
2019-07-14 09:43:58 -07:00
|
|
|
},
|
|
|
|
{
|
2020-03-05 15:25:18 -08:00
|
|
|
name: 'slackOAuth2Api',
|
2019-06-23 03:35:23 -07:00
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2020-03-05 15:25:18 -08:00
|
|
|
authentication: [
|
2020-05-31 10:24:14 -07:00
|
|
|
'oAuth2',
|
2019-07-14 09:43:58 -07:00
|
|
|
],
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
},
|
2020-03-08 15:22:33 -07:00
|
|
|
},
|
2020-03-05 15:25:18 -08:00
|
|
|
],
|
|
|
|
properties: [
|
2019-06-23 03:35:23 -07:00
|
|
|
{
|
2020-03-05 15:25:18 -08:00
|
|
|
displayName: 'Authentication',
|
|
|
|
name: 'authentication',
|
|
|
|
type: 'options',
|
2019-06-23 03:35:23 -07:00
|
|
|
options: [
|
|
|
|
{
|
2020-03-05 15:25:18 -08:00
|
|
|
name: 'Access Token',
|
|
|
|
value: 'accessToken',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
2020-03-05 15:25:18 -08:00
|
|
|
name: 'OAuth2',
|
2020-05-31 10:24:14 -07:00
|
|
|
value: 'oAuth2',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
],
|
2020-03-05 15:25:18 -08:00
|
|
|
default: 'accessToken',
|
|
|
|
description: 'The resource to operate on.',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
2020-05-11 16:56:27 -07:00
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
{
|
2020-03-05 15:25:18 -08:00
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2019-06-23 03:35:23 -07:00
|
|
|
options: [
|
|
|
|
{
|
2020-03-05 15:25:18 -08:00
|
|
|
name: 'Channel',
|
|
|
|
value: 'channel',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
2020-03-08 15:22:33 -07:00
|
|
|
{
|
|
|
|
name: 'File',
|
|
|
|
value: 'file',
|
|
|
|
},
|
2019-06-23 03:35:23 -07:00
|
|
|
{
|
2020-03-05 15:25:18 -08:00
|
|
|
name: 'Message',
|
|
|
|
value: 'message',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
2020-12-13 01:47:52 -08:00
|
|
|
{
|
|
|
|
name: 'Reaction',
|
|
|
|
value: 'reaction',
|
|
|
|
},
|
2020-03-08 15:22:33 -07:00
|
|
|
{
|
|
|
|
name: 'Star',
|
|
|
|
value: 'star',
|
|
|
|
},
|
2020-12-13 01:47:52 -08:00
|
|
|
{
|
|
|
|
name: 'User',
|
|
|
|
value: 'user',
|
|
|
|
},
|
2021-11-03 17:55:04 -07:00
|
|
|
{
|
|
|
|
name: 'User Group',
|
|
|
|
value: 'userGroup',
|
|
|
|
},
|
2020-10-01 22:31:41 -07:00
|
|
|
{
|
|
|
|
name: 'User Profile',
|
|
|
|
value: 'userProfile',
|
|
|
|
},
|
2019-06-23 03:35:23 -07:00
|
|
|
],
|
2020-03-05 15:25:18 -08:00
|
|
|
default: 'message',
|
|
|
|
description: 'The resource to operate on.',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
2020-05-11 16:56:27 -07:00
|
|
|
|
2020-03-05 15:25:18 -08:00
|
|
|
...channelOperations,
|
|
|
|
...channelFields,
|
|
|
|
...messageOperations,
|
|
|
|
...messageFields,
|
2020-03-08 15:22:33 -07:00
|
|
|
...starOperations,
|
|
|
|
...starFields,
|
|
|
|
...fileOperations,
|
|
|
|
...fileFields,
|
2020-12-13 01:47:52 -08:00
|
|
|
...reactionOperations,
|
|
|
|
...reactionFields,
|
|
|
|
...userOperations,
|
|
|
|
...userFields,
|
2021-11-03 17:55:04 -07:00
|
|
|
...userGroupOperations,
|
|
|
|
...userGroupFields,
|
2020-10-01 22:31:41 -07:00
|
|
|
...userProfileOperations,
|
|
|
|
...userProfileFields,
|
2019-06-23 03:35:23 -07:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2020-03-05 15:25:18 -08:00
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
// Get all the users to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getUsers(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2020-03-15 07:51:49 -07:00
|
|
|
const users = await slackApiRequestAllItems.call(this, 'members', 'GET', '/users.list');
|
2020-03-05 15:25:18 -08:00
|
|
|
for (const user of users) {
|
|
|
|
const userName = user.name;
|
|
|
|
const userId = user.id;
|
|
|
|
returnData.push({
|
|
|
|
name: userName,
|
|
|
|
value: userId,
|
|
|
|
});
|
|
|
|
}
|
2020-03-15 07:51:49 -07:00
|
|
|
|
|
|
|
returnData.sort((a, b) => {
|
|
|
|
if (a.name < b.name) { return -1; }
|
|
|
|
if (a.name > b.name) { return 1; }
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
2020-03-08 15:22:33 -07:00
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
// Get all the users to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getChannels(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2020-12-13 01:47:52 -08:00
|
|
|
const qs = { types: 'public_channel,private_channel', limit: 1000 };
|
2020-12-02 03:54:19 -08:00
|
|
|
const channels = await slackApiRequestAllItems.call(this, 'channels', 'GET', '/conversations.list', {}, qs);
|
2020-03-08 15:22:33 -07:00
|
|
|
for (const channel of channels) {
|
|
|
|
const channelName = channel.name;
|
|
|
|
const channelId = channel.id;
|
|
|
|
returnData.push({
|
|
|
|
name: channelName,
|
|
|
|
value: channelId,
|
|
|
|
});
|
|
|
|
}
|
2020-03-15 07:51:49 -07:00
|
|
|
|
|
|
|
returnData.sort((a, b) => {
|
|
|
|
if (a.name < b.name) { return -1; }
|
|
|
|
if (a.name > b.name) { return 1; }
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
2020-03-05 15:25:18 -08:00
|
|
|
return returnData;
|
|
|
|
},
|
2020-10-01 22:31:41 -07:00
|
|
|
// Get all the team fields to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getTeamFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const { profile: { fields } } = await slackApiRequest.call(this, 'GET', '/team.profile.get');
|
|
|
|
for (const field of fields) {
|
|
|
|
const fieldName = field.label;
|
|
|
|
const fieldId = field.id;
|
|
|
|
returnData.push({
|
|
|
|
name: fieldName,
|
|
|
|
value: fieldId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2021-09-11 01:15:36 -07:00
|
|
|
credentialTest: {
|
|
|
|
async testSlackTokenAuth(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise<NodeCredentialTestResult> {
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json; charset=utf-8',
|
|
|
|
Authorization: `Bearer ${credential.data!.accessToken}`,
|
|
|
|
},
|
|
|
|
uri: 'https://slack.com/api/users.profile.get',
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await this.helpers.request(options);
|
2021-11-03 17:55:04 -07:00
|
|
|
|
2021-09-11 01:15:36 -07:00
|
|
|
if (!response.ok) {
|
|
|
|
return {
|
|
|
|
status: 'Error',
|
|
|
|
message: `${response.error}`,
|
|
|
|
};
|
|
|
|
}
|
2021-11-03 17:55:04 -07:00
|
|
|
} catch (err) {
|
2021-09-11 01:15:36 -07:00
|
|
|
return {
|
|
|
|
status: 'Error',
|
|
|
|
message: `${err.message}`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
status: 'OK',
|
|
|
|
message: 'Connection successful!',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
2020-03-05 15:25:18 -08:00
|
|
|
};
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: IDataObject[] = [];
|
2020-03-05 15:25:18 -08:00
|
|
|
const length = items.length as unknown as number;
|
2019-06-23 03:35:23 -07:00
|
|
|
let qs: IDataObject;
|
2020-03-05 15:25:18 -08:00
|
|
|
let responseData;
|
2020-03-15 07:51:49 -07:00
|
|
|
const authentication = this.getNodeParameter('authentication', 0) as string;
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
|
2020-03-05 15:25:18 -08:00
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
responseData = { error: 'Resource ' + resource + ' / operation ' + operation + ' not found!' };
|
|
|
|
qs = {};
|
|
|
|
if (resource === 'channel') {
|
|
|
|
//https://api.slack.com/methods/conversations.archive
|
|
|
|
if (operation === 'archive') {
|
|
|
|
const channel = this.getNodeParameter('channelId', i) as string;
|
|
|
|
const body: IDataObject = {
|
|
|
|
channel,
|
|
|
|
};
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/conversations.archive', body, qs);
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/conversations.close
|
|
|
|
if (operation === 'close') {
|
|
|
|
const channel = this.getNodeParameter('channelId', i) as string;
|
|
|
|
const body: IDataObject = {
|
|
|
|
channel,
|
|
|
|
};
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/conversations.close', body, qs);
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/conversations.create
|
|
|
|
if (operation === 'create') {
|
|
|
|
const channel = this.getNodeParameter('channelId', i) as string;
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
const body: IDataObject = {
|
|
|
|
name: channel,
|
|
|
|
};
|
|
|
|
if (additionalFields.isPrivate) {
|
|
|
|
body.is_private = additionalFields.isPrivate as boolean;
|
|
|
|
}
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/conversations.create', body, qs);
|
|
|
|
responseData = responseData.channel;
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/conversations.kick
|
|
|
|
if (operation === 'kick') {
|
|
|
|
const channel = this.getNodeParameter('channelId', i) as string;
|
|
|
|
const userId = this.getNodeParameter('userId', i) as string;
|
|
|
|
const body: IDataObject = {
|
|
|
|
name: channel,
|
|
|
|
user: userId,
|
|
|
|
};
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/conversations.kick', body, qs);
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/conversations.join
|
|
|
|
if (operation === 'join') {
|
|
|
|
const channel = this.getNodeParameter('channelId', i) as string;
|
|
|
|
const body: IDataObject = {
|
|
|
|
channel,
|
|
|
|
};
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/conversations.join', body, qs);
|
|
|
|
responseData = responseData.channel;
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/conversations.info
|
|
|
|
if (operation === 'get') {
|
|
|
|
const channel = this.getNodeParameter('channelId', i) as string;
|
|
|
|
qs.channel = channel;
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/conversations.info', {}, qs);
|
|
|
|
responseData = responseData.channel;
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/conversations.list
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
const filters = this.getNodeParameter('filters', i) as IDataObject;
|
|
|
|
if (filters.types) {
|
|
|
|
qs.types = (filters.types as string[]).join(',');
|
|
|
|
}
|
|
|
|
if (filters.excludeArchived) {
|
|
|
|
qs.exclude_archived = filters.excludeArchived as boolean;
|
|
|
|
}
|
|
|
|
if (returnAll === true) {
|
|
|
|
responseData = await slackApiRequestAllItems.call(this, 'channels', 'GET', '/conversations.list', {}, qs);
|
|
|
|
} else {
|
|
|
|
qs.limit = this.getNodeParameter('limit', i) as number;
|
|
|
|
responseData = await slackApiRequest.call(this, 'GET', '/conversations.list', {}, qs);
|
|
|
|
responseData = responseData.channels;
|
|
|
|
}
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/conversations.history
|
|
|
|
if (operation === 'history') {
|
|
|
|
const channel = this.getNodeParameter('channelId', i) as string;
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
const filters = this.getNodeParameter('filters', i) as IDataObject;
|
|
|
|
qs.channel = channel;
|
|
|
|
if (filters.inclusive) {
|
|
|
|
qs.inclusive = filters.inclusive as boolean;
|
|
|
|
}
|
|
|
|
if (filters.latest) {
|
2021-11-03 17:55:04 -07:00
|
|
|
qs.latest = new Date(filters.latest as string).getTime() / 1000;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (filters.oldest) {
|
2021-11-03 17:55:04 -07:00
|
|
|
qs.oldest = new Date(filters.oldest as string).getTime() / 1000;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (returnAll === true) {
|
|
|
|
responseData = await slackApiRequestAllItems.call(this, 'messages', 'GET', '/conversations.history', {}, qs);
|
|
|
|
} else {
|
|
|
|
qs.limit = this.getNodeParameter('limit', i) as number;
|
|
|
|
responseData = await slackApiRequest.call(this, 'GET', '/conversations.history', {}, qs);
|
|
|
|
responseData = responseData.messages;
|
|
|
|
}
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/conversations.invite
|
|
|
|
if (operation === 'invite') {
|
|
|
|
const channel = this.getNodeParameter('channelId', i) as string;
|
|
|
|
const userIds = (this.getNodeParameter('userIds', i) as string[]).join(',');
|
|
|
|
const body: IDataObject = {
|
|
|
|
channel,
|
|
|
|
users: userIds,
|
|
|
|
};
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/conversations.invite', body, qs);
|
|
|
|
responseData = responseData.channel;
|
2020-12-13 01:47:52 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/conversations.leave
|
|
|
|
if (operation === 'leave') {
|
|
|
|
const channel = this.getNodeParameter('channelId', i) as string;
|
|
|
|
const body: IDataObject = {
|
|
|
|
channel,
|
|
|
|
};
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/conversations.leave', body, qs);
|
|
|
|
}
|
|
|
|
//https://api.slack.com/methods/conversations.members
|
|
|
|
if (operation === 'member') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
|
|
|
|
const resolveData = this.getNodeParameter('resolveData', 0) as boolean;
|
|
|
|
qs.channel = this.getNodeParameter('channelId', i) as string;
|
|
|
|
if (returnAll) {
|
|
|
|
responseData = await slackApiRequestAllItems.call(this, 'members', 'GET', '/conversations.members', {}, qs);
|
|
|
|
responseData = responseData.map((member: string) => ({ member }));
|
|
|
|
} else {
|
|
|
|
qs.limit = this.getNodeParameter('limit', i) as number;
|
|
|
|
responseData = await slackApiRequest.call(this, 'GET', '/conversations.members', {}, qs);
|
|
|
|
responseData = responseData.members.map((member: string) => ({ member }));
|
|
|
|
}
|
2020-12-13 01:47:52 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resolveData) {
|
|
|
|
const data: IDataObject[] = [];
|
|
|
|
for (const { member } of responseData) {
|
|
|
|
const { user } = await slackApiRequest.call(this, 'GET', '/users.info', {}, { user: member });
|
|
|
|
data.push(user);
|
|
|
|
}
|
|
|
|
responseData = data;
|
2020-12-13 01:47:52 -08:00
|
|
|
}
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/conversations.open
|
|
|
|
if (operation === 'open') {
|
|
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
const body: IDataObject = {};
|
|
|
|
if (options.channelId) {
|
|
|
|
body.channel = options.channelId as string;
|
|
|
|
}
|
|
|
|
if (options.returnIm) {
|
|
|
|
body.return_im = options.returnIm as boolean;
|
|
|
|
}
|
|
|
|
if (options.users) {
|
|
|
|
body.users = (options.users as string[]).join(',');
|
|
|
|
}
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/conversations.open', body, qs);
|
|
|
|
responseData = responseData.channel;
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/conversations.rename
|
|
|
|
if (operation === 'rename') {
|
|
|
|
const channel = this.getNodeParameter('channelId', i) as IDataObject;
|
|
|
|
const name = this.getNodeParameter('name', i) as IDataObject;
|
|
|
|
const body: IDataObject = {
|
|
|
|
channel,
|
|
|
|
name,
|
|
|
|
};
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/conversations.rename', body, qs);
|
|
|
|
responseData = responseData.channel;
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/conversations.replies
|
|
|
|
if (operation === 'replies') {
|
|
|
|
const channel = this.getNodeParameter('channelId', i) as string;
|
|
|
|
const ts = this.getNodeParameter('ts', i) as string;
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
const filters = this.getNodeParameter('filters', i) as IDataObject;
|
|
|
|
qs.channel = channel;
|
|
|
|
qs.ts = ts;
|
|
|
|
if (filters.inclusive) {
|
|
|
|
qs.inclusive = filters.inclusive as boolean;
|
|
|
|
}
|
|
|
|
if (filters.latest) {
|
2021-11-03 17:55:04 -07:00
|
|
|
qs.latest = new Date(filters.latest as string).getTime() / 1000;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (filters.oldest) {
|
2021-11-03 17:55:04 -07:00
|
|
|
qs.oldest = new Date(filters.oldest as string).getTime() / 1000;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (returnAll === true) {
|
|
|
|
responseData = await slackApiRequestAllItems.call(this, 'messages', 'GET', '/conversations.replies', {}, qs);
|
|
|
|
} else {
|
|
|
|
qs.limit = this.getNodeParameter('limit', i) as number;
|
|
|
|
responseData = await slackApiRequest.call(this, 'GET', '/conversations.replies', {}, qs);
|
|
|
|
responseData = responseData.messages;
|
|
|
|
}
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/conversations.setPurpose
|
|
|
|
if (operation === 'setPurpose') {
|
|
|
|
const channel = this.getNodeParameter('channelId', i) as IDataObject;
|
|
|
|
const purpose = this.getNodeParameter('purpose', i) as IDataObject;
|
|
|
|
const body: IDataObject = {
|
|
|
|
channel,
|
|
|
|
purpose,
|
|
|
|
};
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/conversations.setPurpose', body, qs);
|
|
|
|
responseData = responseData.channel;
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/conversations.setTopic
|
|
|
|
if (operation === 'setTopic') {
|
|
|
|
const channel = this.getNodeParameter('channelId', i) as IDataObject;
|
|
|
|
const topic = this.getNodeParameter('topic', i) as IDataObject;
|
|
|
|
const body: IDataObject = {
|
|
|
|
channel,
|
|
|
|
topic,
|
|
|
|
};
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/conversations.setTopic', body, qs);
|
|
|
|
responseData = responseData.channel;
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/conversations.unarchive
|
|
|
|
if (operation === 'unarchive') {
|
|
|
|
const channel = this.getNodeParameter('channelId', i) as string;
|
|
|
|
const body: IDataObject = {
|
|
|
|
channel,
|
|
|
|
};
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/conversations.unarchive', body, qs);
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'message') {
|
|
|
|
//https://api.slack.com/methods/chat.postMessage
|
|
|
|
if (['post', 'postEphemeral'].includes(operation)) {
|
|
|
|
const channel = this.getNodeParameter('channel', i) as string;
|
|
|
|
const { sendAsUser } = this.getNodeParameter('otherOptions', i) as IDataObject;
|
|
|
|
const text = this.getNodeParameter('text', i) as string;
|
|
|
|
const body: IDataObject = {
|
|
|
|
channel,
|
|
|
|
text,
|
|
|
|
};
|
2020-03-15 07:51:49 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let action = 'postMessage';
|
2021-01-06 23:26:17 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'postEphemeral') {
|
|
|
|
body.user = this.getNodeParameter('user', i) as string;
|
|
|
|
action = 'postEphemeral';
|
|
|
|
}
|
2021-01-06 23:26:17 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
|
2020-05-05 10:22:02 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (authentication === 'accessToken' && sendAsUser !== '') {
|
|
|
|
body.username = sendAsUser;
|
|
|
|
}
|
2020-03-15 07:51:49 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!jsonParameters) {
|
|
|
|
const attachments = this.getNodeParameter('attachments', i, []) as unknown as Attachment[];
|
|
|
|
const blocksUi = (this.getNodeParameter('blocksUi', i, []) as IDataObject).blocksValues as IDataObject[];
|
2020-05-05 10:22:02 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// The node does save the fields data differently than the API
|
|
|
|
// expects so fix the data befre we send the request
|
|
|
|
for (const attachment of attachments) {
|
|
|
|
if (attachment.fields !== undefined) {
|
|
|
|
if (attachment.fields.item !== undefined) {
|
|
|
|
// Move the field-content up
|
|
|
|
// @ts-ignore
|
|
|
|
attachment.fields = attachment.fields.item;
|
|
|
|
} else {
|
|
|
|
// If it does not have any items set remove it
|
2021-08-29 11:58:11 -07:00
|
|
|
// @ts-ignore
|
2021-07-19 23:58:54 -07:00
|
|
|
delete attachment.fields;
|
|
|
|
}
|
2020-05-05 10:22:02 -07:00
|
|
|
}
|
2019-07-14 09:43:58 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
body['attachments'] = attachments;
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (blocksUi) {
|
|
|
|
const blocks: Block[] = [];
|
|
|
|
for (const blockUi of blocksUi) {
|
|
|
|
const block: Block = {};
|
|
|
|
const elements: Element[] = [];
|
|
|
|
block.block_id = blockUi.blockId as string;
|
|
|
|
block.type = blockUi.type as string;
|
|
|
|
if (block.type === 'actions') {
|
|
|
|
const elementsUi = (blockUi.elementsUi as IDataObject).elementsValues as IDataObject[];
|
|
|
|
if (elementsUi) {
|
|
|
|
for (const elementUi of elementsUi) {
|
|
|
|
const element: Element = {};
|
|
|
|
if (elementUi.actionId === '') {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Action ID must be set');
|
2020-05-05 10:22:02 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (elementUi.text === '') {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Text must be set');
|
2020-05-05 10:22:02 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
element.action_id = elementUi.actionId as string;
|
|
|
|
element.type = elementUi.type as string;
|
|
|
|
element.text = {
|
|
|
|
text: elementUi.text as string,
|
|
|
|
type: 'plain_text',
|
|
|
|
emoji: elementUi.emoji as boolean,
|
|
|
|
};
|
|
|
|
if (elementUi.url) {
|
|
|
|
element.url = elementUi.url as string;
|
2020-05-05 10:22:02 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (elementUi.value) {
|
|
|
|
element.value = elementUi.value as string;
|
2020-05-05 10:22:02 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (elementUi.style !== 'default') {
|
|
|
|
element.style = elementUi.style as string;
|
2020-05-05 10:22:02 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
const confirmUi = (elementUi.confirmUi as IDataObject).confirmValue as IDataObject;
|
|
|
|
if (confirmUi) {
|
|
|
|
const confirm: Confirm = {};
|
|
|
|
const titleUi = (confirmUi.titleUi as IDataObject).titleValue as IDataObject;
|
|
|
|
const textUi = (confirmUi.textUi as IDataObject).textValue as IDataObject;
|
|
|
|
const confirmTextUi = (confirmUi.confirmTextUi as IDataObject).confirmValue as IDataObject;
|
|
|
|
const denyUi = (confirmUi.denyUi as IDataObject).denyValue as IDataObject;
|
|
|
|
const style = confirmUi.style as string;
|
|
|
|
if (titleUi) {
|
|
|
|
confirm.title = {
|
|
|
|
type: 'plain_text',
|
|
|
|
text: titleUi.text as string,
|
|
|
|
emoji: titleUi.emoji as boolean,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (textUi) {
|
|
|
|
confirm.text = {
|
|
|
|
type: 'plain_text',
|
|
|
|
text: textUi.text as string,
|
|
|
|
emoji: textUi.emoji as boolean,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (confirmTextUi) {
|
|
|
|
confirm.confirm = {
|
|
|
|
type: 'plain_text',
|
|
|
|
text: confirmTextUi.text as string,
|
|
|
|
emoji: confirmTextUi.emoji as boolean,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (denyUi) {
|
|
|
|
confirm.deny = {
|
|
|
|
type: 'plain_text',
|
|
|
|
text: denyUi.text as string,
|
|
|
|
emoji: denyUi.emoji as boolean,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (style !== 'default') {
|
|
|
|
confirm.style = style as string;
|
|
|
|
}
|
|
|
|
element.confirm = confirm;
|
|
|
|
}
|
|
|
|
elements.push(element);
|
2020-06-22 11:51:15 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
block.elements = elements;
|
2020-05-05 10:22:02 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (block.type === 'section') {
|
|
|
|
const textUi = (blockUi.textUi as IDataObject).textValue as IDataObject;
|
|
|
|
if (textUi) {
|
|
|
|
const text: Text = {};
|
|
|
|
if (textUi.type === 'plainText') {
|
|
|
|
text.type = 'plain_text';
|
|
|
|
text.emoji = textUi.emoji as boolean;
|
2020-05-05 10:22:02 -07:00
|
|
|
} else {
|
2021-07-19 23:58:54 -07:00
|
|
|
text.type = 'mrkdwn';
|
|
|
|
text.verbatim = textUi.verbatim as boolean;
|
2020-05-05 10:22:02 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
text.text = textUi.text as string;
|
|
|
|
block.text = text;
|
|
|
|
} else {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Property text must be defined');
|
2020-05-05 10:22:02 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
const fieldsUi = (blockUi.fieldsUi as IDataObject).fieldsValues as IDataObject[];
|
|
|
|
if (fieldsUi) {
|
|
|
|
const fields: Text[] = [];
|
|
|
|
for (const fieldUi of fieldsUi) {
|
|
|
|
const field: Text = {};
|
|
|
|
if (fieldUi.type === 'plainText') {
|
|
|
|
field.type = 'plain_text';
|
|
|
|
field.emoji = fieldUi.emoji as boolean;
|
|
|
|
} else {
|
|
|
|
field.type = 'mrkdwn';
|
|
|
|
field.verbatim = fieldUi.verbatim as boolean;
|
|
|
|
}
|
|
|
|
field.text = fieldUi.text as string;
|
|
|
|
fields.push(field);
|
2020-05-05 10:22:02 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
// If not fields were added then it's not needed to send the property
|
|
|
|
if (fields.length > 0) {
|
|
|
|
block.fields = fields;
|
2020-05-05 10:22:02 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
const accessoryUi = (blockUi.accessoryUi as IDataObject).accessoriesValues as IDataObject;
|
|
|
|
if (accessoryUi) {
|
|
|
|
const accessory: Element = {};
|
|
|
|
if (accessoryUi.type === 'button') {
|
|
|
|
accessory.type = 'button';
|
|
|
|
accessory.text = {
|
|
|
|
text: accessoryUi.text as string,
|
|
|
|
type: 'plain_text',
|
|
|
|
emoji: accessoryUi.emoji as boolean,
|
|
|
|
};
|
|
|
|
if (accessoryUi.url) {
|
|
|
|
accessory.url = accessoryUi.url as string;
|
2020-06-22 11:51:15 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (accessoryUi.value) {
|
|
|
|
accessory.value = accessoryUi.value as string;
|
2020-06-22 11:51:15 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (accessoryUi.style !== 'default') {
|
|
|
|
accessory.style = accessoryUi.style as string;
|
2020-06-22 11:51:15 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
const confirmUi = (accessoryUi.confirmUi as IDataObject).confirmValue as IDataObject;
|
|
|
|
if (confirmUi) {
|
|
|
|
const confirm: Confirm = {};
|
|
|
|
const titleUi = (confirmUi.titleUi as IDataObject).titleValue as IDataObject;
|
|
|
|
const textUi = (confirmUi.textUi as IDataObject).textValue as IDataObject;
|
|
|
|
const confirmTextUi = (confirmUi.confirmTextUi as IDataObject).confirmValue as IDataObject;
|
|
|
|
const denyUi = (confirmUi.denyUi as IDataObject).denyValue as IDataObject;
|
|
|
|
const style = confirmUi.style as string;
|
|
|
|
if (titleUi) {
|
|
|
|
confirm.title = {
|
|
|
|
type: 'plain_text',
|
|
|
|
text: titleUi.text as string,
|
|
|
|
emoji: titleUi.emoji as boolean,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (textUi) {
|
|
|
|
confirm.text = {
|
|
|
|
type: 'plain_text',
|
|
|
|
text: textUi.text as string,
|
|
|
|
emoji: textUi.emoji as boolean,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (confirmTextUi) {
|
|
|
|
confirm.confirm = {
|
|
|
|
type: 'plain_text',
|
|
|
|
text: confirmTextUi.text as string,
|
|
|
|
emoji: confirmTextUi.emoji as boolean,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (denyUi) {
|
|
|
|
confirm.deny = {
|
|
|
|
type: 'plain_text',
|
|
|
|
text: denyUi.text as string,
|
|
|
|
emoji: denyUi.emoji as boolean,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (style !== 'default') {
|
|
|
|
confirm.style = style as string;
|
|
|
|
}
|
|
|
|
accessory.confirm = confirm;
|
2020-06-22 11:51:15 -07:00
|
|
|
}
|
2020-05-05 10:22:02 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
block.accessory = accessory;
|
2020-05-05 10:22:02 -07:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
blocks.push(block);
|
2020-05-05 10:22:02 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
body.blocks = blocks;
|
2020-05-05 10:22:02 -07:00
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
const attachmentsJson = this.getNodeParameter('attachmentsJson', i, '') as string;
|
|
|
|
const blocksJson = this.getNodeParameter('blocksJson', i, []) as string;
|
|
|
|
if (attachmentsJson !== '' && validateJSON(attachmentsJson) === undefined) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Attachments it is not a valid json');
|
|
|
|
}
|
|
|
|
if (blocksJson !== '' && validateJSON(blocksJson) === undefined) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Blocks it is not a valid json');
|
|
|
|
}
|
|
|
|
if (attachmentsJson !== '') {
|
|
|
|
body.attachments = attachmentsJson;
|
|
|
|
}
|
|
|
|
if (blocksJson !== '') {
|
|
|
|
body.blocks = blocksJson;
|
|
|
|
}
|
2020-05-05 10:22:02 -07:00
|
|
|
}
|
2021-01-06 23:26:17 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// Add all the other options to the request
|
|
|
|
const otherOptions = this.getNodeParameter('otherOptions', i) as IDataObject;
|
|
|
|
Object.assign(body, otherOptions);
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', `/chat.${action}`, body, qs);
|
|
|
|
}
|
|
|
|
//https://api.slack.com/methods/chat.update
|
|
|
|
if (operation === 'update') {
|
|
|
|
const channel = this.getNodeParameter('channelId', i) as string;
|
|
|
|
const text = this.getNodeParameter('text', i) as string;
|
|
|
|
const ts = this.getNodeParameter('ts', i) as string;
|
|
|
|
const attachments = this.getNodeParameter('attachments', i, []) as unknown as IAttachment[];
|
|
|
|
const body: IDataObject = {
|
|
|
|
channel,
|
|
|
|
text,
|
|
|
|
ts,
|
|
|
|
};
|
2020-03-15 07:51:49 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// The node does save the fields data differently than the API
|
|
|
|
// expects so fix the data befre we send the request
|
|
|
|
for (const attachment of attachments) {
|
|
|
|
if (attachment.fields !== undefined) {
|
|
|
|
if (attachment.fields.item !== undefined) {
|
|
|
|
// Move the field-content up
|
|
|
|
// @ts-ignore
|
|
|
|
attachment.fields = attachment.fields.item;
|
|
|
|
} else {
|
|
|
|
// If it does not have any items set remove it
|
2021-08-29 11:58:11 -07:00
|
|
|
// @ts-ignore
|
2021-07-19 23:58:54 -07:00
|
|
|
delete attachment.fields;
|
|
|
|
}
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
body['attachments'] = attachments;
|
2020-03-08 15:22:33 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// Add all the other options to the request
|
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
|
|
|
Object.assign(body, updateFields);
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/chat.update', body, qs);
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/chat.delete
|
|
|
|
if (operation === 'delete') {
|
|
|
|
const channel = this.getNodeParameter('channelId', i) as string;
|
|
|
|
const timestamp = this.getNodeParameter('timestamp', i) as string;
|
|
|
|
const body: IDataObject = {
|
|
|
|
channel,
|
|
|
|
ts: timestamp,
|
|
|
|
};
|
|
|
|
// Add all the other options to the request
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/chat.delete', body, qs);
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/chat.getPermalink
|
|
|
|
if (operation === 'getPermalink') {
|
|
|
|
const channel = this.getNodeParameter('channelId', i) as string;
|
|
|
|
const timestamp = this.getNodeParameter('timestamp', i) as string;
|
|
|
|
const qs = {
|
|
|
|
channel,
|
|
|
|
message_ts: timestamp,
|
|
|
|
};
|
|
|
|
responseData = await slackApiRequest.call(this, 'GET', '/chat.getPermalink', {}, qs);
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'reaction') {
|
|
|
|
const channel = this.getNodeParameter('channelId', i) as string;
|
|
|
|
const timestamp = this.getNodeParameter('timestamp', i) as string;
|
|
|
|
//https://api.slack.com/methods/reactions.add
|
|
|
|
if (operation === 'add') {
|
|
|
|
const name = this.getNodeParameter('name', i) as string;
|
|
|
|
const body: IDataObject = {
|
|
|
|
channel,
|
|
|
|
name,
|
|
|
|
timestamp,
|
|
|
|
};
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/reactions.add', body, qs);
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/reactions.remove
|
|
|
|
if (operation === 'remove') {
|
|
|
|
const name = this.getNodeParameter('name', i) as string;
|
|
|
|
const body: IDataObject = {
|
|
|
|
channel,
|
|
|
|
name,
|
|
|
|
timestamp,
|
|
|
|
};
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/reactions.remove', body, qs);
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/reactions.get
|
|
|
|
if (operation === 'get') {
|
|
|
|
qs.channel = channel;
|
|
|
|
qs.timestamp = timestamp;
|
|
|
|
responseData = await slackApiRequest.call(this, 'GET', '/reactions.get', {}, qs);
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'star') {
|
|
|
|
//https://api.slack.com/methods/stars.add
|
|
|
|
if (operation === 'add') {
|
|
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
const body: IDataObject = {};
|
|
|
|
if (options.channelId) {
|
|
|
|
body.channel = options.channelId as string;
|
|
|
|
}
|
|
|
|
if (options.fileId) {
|
|
|
|
body.file = options.fileId as string;
|
|
|
|
}
|
|
|
|
if (options.fileComment) {
|
|
|
|
body.file_comment = options.fileComment as string;
|
|
|
|
}
|
|
|
|
if (options.timestamp) {
|
|
|
|
body.timestamp = options.timestamp as string;
|
|
|
|
}
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/stars.add', body, qs);
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/stars.remove
|
|
|
|
if (operation === 'delete') {
|
|
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
const body: IDataObject = {};
|
|
|
|
if (options.channelId) {
|
|
|
|
body.channel = options.channelId as string;
|
|
|
|
}
|
|
|
|
if (options.fileId) {
|
|
|
|
body.file = options.fileId as string;
|
|
|
|
}
|
|
|
|
if (options.fileComment) {
|
|
|
|
body.file_comment = options.fileComment as string;
|
|
|
|
}
|
|
|
|
if (options.timestamp) {
|
|
|
|
body.timestamp = options.timestamp as string;
|
|
|
|
}
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/stars.remove', body, qs);
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/stars.list
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
if (returnAll === true) {
|
|
|
|
responseData = await slackApiRequestAllItems.call(this, 'items', 'GET', '/stars.list', {}, qs);
|
|
|
|
} else {
|
|
|
|
qs.limit = this.getNodeParameter('limit', i) as number;
|
|
|
|
responseData = await slackApiRequest.call(this, 'GET', '/stars.list', {}, qs);
|
|
|
|
responseData = responseData.items;
|
|
|
|
}
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (resource === 'file') {
|
|
|
|
//https://api.slack.com/methods/files.upload
|
|
|
|
if (operation === 'upload') {
|
|
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
const binaryData = this.getNodeParameter('binaryData', i) as boolean;
|
|
|
|
const body: IDataObject = {};
|
|
|
|
if (options.channelIds) {
|
|
|
|
body.channels = (options.channelIds as string[]).join(',');
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (options.fileName) {
|
|
|
|
body.filename = options.fileName as string;
|
|
|
|
}
|
|
|
|
if (options.initialComment) {
|
|
|
|
body.initial_comment = options.initialComment as string;
|
|
|
|
}
|
|
|
|
if (options.threadTs) {
|
|
|
|
body.thread_ts = options.threadTs as string;
|
|
|
|
}
|
|
|
|
if (options.title) {
|
|
|
|
body.title = options.title as string;
|
|
|
|
}
|
|
|
|
if (binaryData) {
|
|
|
|
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i) as string;
|
|
|
|
if (items[i].binary === undefined
|
2020-03-15 07:51:49 -07:00
|
|
|
//@ts-ignore
|
2021-07-19 23:58:54 -07:00
|
|
|
|| items[i].binary[binaryPropertyName] === undefined) {
|
|
|
|
throw new NodeOperationError(this.getNode(), `No binary data property "${binaryPropertyName}" does not exists on item!`);
|
|
|
|
}
|
|
|
|
body.file = {
|
2020-03-08 15:22:33 -07:00
|
|
|
//@ts-ignore
|
2021-07-19 23:58:54 -07:00
|
|
|
value: Buffer.from(items[i].binary[binaryPropertyName].data, BINARY_ENCODING),
|
|
|
|
options: {
|
|
|
|
//@ts-ignore
|
|
|
|
filename: items[i].binary[binaryPropertyName].fileName,
|
|
|
|
//@ts-ignore
|
|
|
|
contentType: items[i].binary[binaryPropertyName].mimeType,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/files.upload', {}, qs, { 'Content-Type': 'multipart/form-data' }, { formData: body });
|
|
|
|
responseData = responseData.file;
|
|
|
|
} else {
|
|
|
|
const fileContent = this.getNodeParameter('fileContent', i) as string;
|
|
|
|
body.content = fileContent;
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/files.upload', body, qs, { 'Content-Type': 'application/x-www-form-urlencoded' }, { form: body });
|
|
|
|
responseData = responseData.file;
|
|
|
|
}
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/files.list
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
const filters = this.getNodeParameter('filters', i) as IDataObject;
|
|
|
|
if (filters.channelId) {
|
|
|
|
qs.channel = filters.channelId as string;
|
|
|
|
}
|
|
|
|
if (filters.showFilesHidden) {
|
|
|
|
qs.show_files_hidden_by_limit = filters.showFilesHidden as boolean;
|
|
|
|
}
|
|
|
|
if (filters.tsFrom) {
|
|
|
|
qs.ts_from = filters.tsFrom as string;
|
|
|
|
}
|
|
|
|
if (filters.tsTo) {
|
|
|
|
qs.ts_to = filters.tsTo as string;
|
|
|
|
}
|
|
|
|
if (filters.types) {
|
|
|
|
qs.types = (filters.types as string[]).join(',') as string;
|
|
|
|
}
|
|
|
|
if (filters.userId) {
|
|
|
|
qs.user = filters.userId as string;
|
|
|
|
}
|
|
|
|
if (returnAll === true) {
|
|
|
|
responseData = await slackApiRequestAllItems.call(this, 'files', 'GET', '/files.list', {}, qs);
|
|
|
|
} else {
|
|
|
|
qs.count = this.getNodeParameter('limit', i) as number;
|
|
|
|
responseData = await slackApiRequest.call(this, 'GET', '/files.list', {}, qs);
|
|
|
|
responseData = responseData.files;
|
|
|
|
}
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/files.info
|
|
|
|
if (operation === 'get') {
|
|
|
|
const fileId = this.getNodeParameter('fileId', i) as string;
|
|
|
|
qs.file = fileId;
|
|
|
|
responseData = await slackApiRequest.call(this, 'GET', '/files.info', {}, qs);
|
|
|
|
responseData = responseData.file;
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (resource === 'user') {
|
|
|
|
//https://api.slack.com/methods/users.info
|
|
|
|
if (operation === 'info') {
|
|
|
|
qs.user = this.getNodeParameter('user', i) as string;
|
|
|
|
responseData = await slackApiRequest.call(this, 'GET', '/users.info', {}, qs);
|
|
|
|
responseData = responseData.user;
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.slack.com/methods/users.getPresence
|
|
|
|
if (operation === 'getPresence') {
|
|
|
|
qs.user = this.getNodeParameter('user', i) as string;
|
|
|
|
responseData = await slackApiRequest.call(this, 'GET', '/users.getPresence', {}, qs);
|
2020-03-08 15:22:33 -07:00
|
|
|
}
|
|
|
|
}
|
2021-11-03 17:55:04 -07:00
|
|
|
if (resource === 'userGroup') {
|
|
|
|
//https://api.slack.com/methods/usergroups.create
|
|
|
|
if (operation === 'create') {
|
|
|
|
const name = this.getNodeParameter('name', i) as string;
|
|
|
|
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
|
|
|
|
const body: IDataObject = {
|
|
|
|
name,
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.assign(body, additionalFields);
|
|
|
|
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/usergroups.create', body, qs);
|
|
|
|
|
|
|
|
responseData = responseData.usergroup;
|
|
|
|
}
|
|
|
|
//https://api.slack.com/methods/usergroups.enable
|
|
|
|
if (operation === 'enable') {
|
|
|
|
const userGroupId = this.getNodeParameter('userGroupId', i) as string;
|
|
|
|
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
|
|
|
|
const body: IDataObject = {
|
|
|
|
usergroup: userGroupId,
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.assign(body, additionalFields);
|
|
|
|
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/usergroups.enable', body, qs);
|
|
|
|
|
|
|
|
responseData = responseData.usergroup;
|
|
|
|
}
|
|
|
|
//https://api.slack.com/methods/usergroups.disable
|
|
|
|
if (operation === 'disable') {
|
|
|
|
const userGroupId = this.getNodeParameter('userGroupId', i) as string;
|
|
|
|
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
|
|
|
|
const body: IDataObject = {
|
|
|
|
usergroup: userGroupId,
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.assign(body, additionalFields);
|
|
|
|
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/usergroups.disable', body, qs);
|
|
|
|
|
|
|
|
responseData = responseData.usergroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
//https://api.slack.com/methods/usergroups.list
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
|
|
|
|
const qs: IDataObject = {};
|
|
|
|
|
|
|
|
Object.assign(qs, additionalFields);
|
|
|
|
|
|
|
|
responseData = await slackApiRequest.call(this, 'GET', '/usergroups.list', {}, qs);
|
|
|
|
|
|
|
|
responseData = responseData.usergroups;
|
|
|
|
|
|
|
|
if (returnAll === false) {
|
|
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
|
|
|
|
|
|
responseData = responseData.slice(0, limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//https://api.slack.com/methods/usergroups.update
|
|
|
|
if (operation === 'update') {
|
|
|
|
const userGroupId = this.getNodeParameter('userGroupId', i) as string;
|
|
|
|
|
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
|
|
|
|
|
|
|
const body: IDataObject = {
|
|
|
|
usergroup: userGroupId,
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.assign(body, updateFields);
|
|
|
|
|
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/usergroups.update', body, qs);
|
|
|
|
|
|
|
|
responseData = responseData.usergroup;
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'userProfile') {
|
|
|
|
//https://api.slack.com/methods/users.profile.set
|
|
|
|
if (operation === 'update') {
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2020-10-01 22:31:41 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const timezone = this.getTimezone();
|
2020-10-01 22:31:41 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {};
|
2020-10-01 22:31:41 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(body, additionalFields);
|
2020-10-01 22:31:41 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (body.status_expiration === undefined) {
|
|
|
|
body.status_expiration = 0;
|
2020-10-01 22:31:41 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
body.status_expiration = moment.tz(body.status_expiration as string, timezone).unix();
|
2020-10-01 22:31:41 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (body.customFieldUi) {
|
|
|
|
const customFields = (body.customFieldUi as IDataObject).customFieldValues as IDataObject[];
|
2020-10-01 22:31:41 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body.fields = {};
|
2020-10-01 22:31:41 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
for (const customField of customFields) {
|
|
|
|
//@ts-ignore
|
|
|
|
body.fields[customField.id] = {
|
|
|
|
value: customField.value,
|
|
|
|
alt: customField.alt,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2020-10-01 22:31:41 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/users.profile.set', { profile: body }, qs);
|
2020-10-01 22:31:41 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.profile;
|
|
|
|
}
|
|
|
|
//https://api.slack.com/methods/users.profile.get
|
|
|
|
if (operation === 'get') {
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2020-10-01 22:31:41 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const qs: IDataObject = {};
|
2021-08-29 11:58:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, additionalFields);
|
2021-08-29 11:58:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await slackApiRequest.call(this, 'POST', '/users.profile.get', undefined, qs);
|
2021-08-29 11:58:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.profile;
|
|
|
|
}
|
2020-10-01 22:31:41 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (Array.isArray(responseData)) {
|
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
|
|
} else {
|
|
|
|
returnData.push(responseData as IDataObject);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|