n8n/packages/nodes-base/nodes/Slack/Slack.node.ts

575 lines
13 KiB
TypeScript
Raw Normal View History

2019-06-23 03:35:23 -07:00
import { IExecuteFunctions } from 'n8n-core';
import {
IDataObject,
INodeTypeDescription,
INodeExecutionData,
INodeType,
} from 'n8n-workflow';
interface Attachment {
fields: {
item?: object[];
};
}
export class Slack implements INodeType {
description: INodeTypeDescription = {
displayName: 'Slack',
name: 'slack',
icon: 'file:slack.png',
group: ['output'],
version: 1,
description: 'Sends data to Slack',
defaults: {
name: 'Slack',
color: '#BB2244',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'slackApi',
required: true,
}
],
properties: [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
options: [
{
name: 'Create Channel',
value: 'channelsCreate',
description: 'Creates a new channel',
},
{
name: 'Invite User',
value: 'channelsInvite',
description: 'Invites a user to a channel',
},
{
name: 'Send Message',
value: 'chatPostMessage',
description: 'Posts a message into a channel',
},
],
default: 'chatPostMessage',
description: 'The operation to perform.',
},
// ----------------------------------
// channelsCreate
// ----------------------------------
{
displayName: 'Name',
name: 'channel',
type: 'string',
default: '',
placeholder: 'Channel name',
displayOptions: {
show: {
operation: [
'channelsCreate'
],
},
},
required: true,
description: 'The name of the channel to create.',
},
// ----------------------------------
// channelsInvite
// ----------------------------------
{
displayName: 'Channel ID',
name: 'channel',
type: 'string',
default: '',
placeholder: 'myChannel',
displayOptions: {
show: {
operation: [
'channelsInvite'
],
},
},
required: true,
description: 'The ID of the channel to invite user to.',
},
{
displayName: 'User ID',
name: 'username',
type: 'string',
default: '',
placeholder: 'frank',
displayOptions: {
show: {
operation: [
'channelsInvite'
],
},
},
required: true,
description: 'The ID of the user to invite into channel.',
},
// ----------------------------------
// chatPostMessage
// ----------------------------------
{
displayName: 'Channel',
name: 'channel',
type: 'string',
default: '',
placeholder: 'Channel name',
displayOptions: {
show: {
operation: [
'chatPostMessage'
],
},
},
required: true,
description: 'The channel to send the message to.',
},
{
displayName: 'Text',
name: 'text',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
displayOptions: {
show: {
operation: [
'chatPostMessage'
],
},
},
description: 'The text to send.',
},
{
displayName: 'As User',
name: 'as_user',
type: 'boolean',
default: false,
displayOptions: {
show: {
operation: [
'chatPostMessage'
],
},
},
description: 'Post the message as authenticated user instead of bot.',
},
{
displayName: 'User Name',
name: 'username',
type: 'string',
default: '',
displayOptions: {
show: {
operation: [
'chatPostMessage'
],
as_user: [
false
],
},
},
description: 'Set the bot\'s user name.',
},
{
displayName: 'Attachments',
name: 'attachments',
type: 'collection',
typeOptions: {
multipleValues: true,
multipleValueButtonText: 'Add attachment',
},
displayOptions: {
show: {
operation: [
'chatPostMessage'
],
},
},
default: {}, // TODO: Remove comment: has to make default array for the main property, check where that happens in UI
description: 'The attachment to add',
placeholder: 'Add attachment item',
options: [
{
displayName: 'Fallback Text',
name: 'fallback',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
description: 'Required plain-text summary of the attachment.',
},
{
displayName: 'Text',
name: 'text',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
description: 'Text to send.',
},
{
displayName: 'Title',
name: 'title',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
description: 'Title of the message.',
},
{
displayName: 'Title Link',
name: 'title_link',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
description: 'Link of the title.',
},
{
displayName: 'Color',
name: 'color',
type: 'color',
default: '#ff0000',
description: 'Color of the line left of text.',
},
{
displayName: 'Pretext',
name: 'pretext',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
description: 'Text which appears before the message block.',
},
{
displayName: 'Author Name',
name: 'author_name',
type: 'string',
default: '',
description: 'Name that should appear.',
},
{
displayName: 'Author Link',
name: 'author_link',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
description: 'Link for the author.',
},
{
displayName: 'Author Icon',
name: 'author_icon',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
description: 'Icon which should appear for the user.',
},
{
displayName: 'Image URL',
name: 'image_url',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
description: 'URL of image.',
},
{
displayName: 'Thumbnail URL',
name: 'thumb_url',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
description: 'URL of thumbnail.',
},
{
displayName: 'Footer',
name: 'footer',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
description: 'Text of footer to add.',
},
{
displayName: 'Footer Icon',
name: 'footer_icon',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
description: 'Icon which should appear next to footer.',
},
{
displayName: 'Timestamp',
name: 'ts',
type: 'dateTime',
default: '',
description: 'Time message relates to.',
},
{
displayName: 'Fields',
name: 'fields',
placeholder: 'Add Fields',
description: 'Fields to add to message.',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
default: {},
options: [
{
name: 'item',
displayName: 'Item',
values: [
{
displayName: 'Title',
name: 'title',
type: 'string',
default: '',
description: 'Title of the item.',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
description: 'Value of the item.',
},
{
displayName: 'Short',
name: 'short',
type: 'boolean',
default: true,
description: 'If items can be displayed next to each other.',
},
]
},
],
}
],
},
{
displayName: 'Other Options',
name: 'otherOptions',
type: 'collection',
displayOptions: {
show: {
operation: [
'chatPostMessage'
],
},
},
default: {},
description: 'Other options to set',
placeholder: 'Add options',
options: [
{
displayName: 'Icon Emoji',
name: 'icon_emoji',
type: 'string',
displayOptions: {
show: {
'/as_user': [
false
],
},
},
default: '',
description: 'Emoji to use as the icon for this message. Overrides icon_url.',
},
{
displayName: 'Icon URL',
name: 'icon_url',
type: 'string',
displayOptions: {
show: {
'/as_user': [
false
],
},
},
default: '',
description: 'URL to an image to use as the icon for this message.',
},
{
displayName: 'Make Reply',
name: 'thread_ts',
type: 'string',
default: '',
description: 'Provide another message\'s ts value to make this message a reply.',
},
{
displayName: 'Unfurl Links',
name: 'unfurl_links',
type: 'boolean',
default: false,
description: 'Pass true to enable unfurling of primarily text-based content.',
},
{
displayName: 'Unfurl Media',
name: 'unfurl_media',
type: 'boolean',
default: true,
description: 'Pass false to disable unfurling of media content.',
},
{
displayName: 'Markdown',
name: 'mrkdwn',
type: 'boolean',
default: true,
description: 'Use Slack Markdown parsing.',
},
{
displayName: 'Reply Broadcast',
name: 'reply_broadcast',
type: 'boolean',
default: false,
description: 'Used in conjunction with thread_ts and indicates whether reply should be made visible to everyone in the channel or conversation.',
},
{
displayName: 'Link Names',
name: 'link_names',
type: 'boolean',
default: false,
description: 'Find and link channel names and usernames.',
},
],
},
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const credentials = this.getCredentials('slackApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const baseUrl = `https://slack.com/api/`;
const operation = this.getNodeParameter('operation', 0) as string;
let requestMethod = 'GET';
// For Post
let body: IDataObject;
// For Query string
let qs: IDataObject;
for (let i = 0; i < items.length; i++) {
let endpoint = '';
body = {};
qs = {};
if (operation === 'channelsCreate') {
// ----------------------------------
// channelsCreate
// ----------------------------------
requestMethod = 'POST';
endpoint = 'channels.create';
body.name = this.getNodeParameter('channel', i) as string;
} else if (operation === 'chatPostMessage') {
// ----------------------------------
// chatPostMessage
// ----------------------------------
requestMethod = 'POST';
endpoint = 'chat.postMessage';
body.channel = this.getNodeParameter('channel', i) as string;
body.text = this.getNodeParameter('text', i) as string;
body.as_user = this.getNodeParameter('as_user', i) as boolean;
if (body.as_user === false) {
body.username = this.getNodeParameter('username', i) as string;
}
const attachments = this.getNodeParameter('attachments', i, []) as unknown as Attachment[];
// 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
delete attachment.fields;
}
}
}
body['attachments'] = attachments;
// Add all the other options to the request
const otherOptions = this.getNodeParameter('otherOptions', i) as IDataObject;
Object.assign(body, otherOptions);
} else if (operation === 'channelsInvite') {
// ----------------------------------
// channelsInvite
// ----------------------------------
requestMethod = 'POST';
endpoint = 'channels.invite';
body.channel = this.getNodeParameter('channel', i) as string;
body.user = this.getNodeParameter('username', i) as string;
}
const options = {
method: requestMethod,
body,
qs,
uri: `${baseUrl}/${endpoint}`,
headers: {
Authorization: `Bearer ${credentials.accessToken }`,
'content-type': 'application/json; charset=utf-8'
},
json: true
};
const responseData = await this.helpers.request(options);
if (!responseData.ok) {
throw new Error(`Request to Slack did fail with error: "${responseData.error}"`);
}
returnData.push(responseData as IDataObject);
}
return [this.helpers.returnJsonArray(returnData)];
}
}