Update operation selection on Slack-Node

This commit is contained in:
Jan Oberhauser 2019-07-14 18:43:58 +02:00
parent d4a4088703
commit c6a55113e3

View file

@ -34,32 +34,85 @@ export class Slack implements INodeType {
], ],
properties: [ properties: [
{ {
displayName: 'Operation', displayName: 'Resource',
name: 'operation', name: 'resource',
type: 'options', type: 'options',
options: [ options: [
{ {
name: 'Create Channel', name: 'Channel',
value: 'channelsCreate', value: 'channel',
description: 'Creates a new channel',
}, },
{ {
name: 'Invite User', name: 'Message',
value: 'channelsInvite', value: 'message',
description: 'Invites a user to a channel',
},
{
name: 'Send Message',
value: 'chatPostMessage',
description: 'Posts a message into a channel',
}, },
], ],
default: 'chatPostMessage', default: 'message',
description: 'The resource to operate on.',
},
// ----------------------------------
// operations
// ----------------------------------
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'channel',
],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create a new channel',
},
{
name: 'Invite',
value: 'invite',
description: 'Invite a user to a channel',
},
],
default: 'create',
description: 'The operation to perform.', description: 'The operation to perform.',
}, },
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'message',
],
},
},
options: [
{
name: 'Post',
value: 'post',
description: 'Post a message into a channel',
},
],
default: 'post',
description: 'The operation to perform.',
},
// ---------------------------------- // ----------------------------------
// channelsCreate // channel
// ----------------------------------
// ----------------------------------
// channel:create
// ---------------------------------- // ----------------------------------
{ {
displayName: 'Name', displayName: 'Name',
@ -70,7 +123,10 @@ export class Slack implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: [
'channelsCreate' 'create'
],
resource: [
'channel',
], ],
}, },
}, },
@ -78,9 +134,8 @@ export class Slack implements INodeType {
description: 'The name of the channel to create.', description: 'The name of the channel to create.',
}, },
// ---------------------------------- // ----------------------------------
// channelsInvite // channel:invite
// ---------------------------------- // ----------------------------------
{ {
displayName: 'Channel ID', displayName: 'Channel ID',
@ -91,7 +146,10 @@ export class Slack implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: [
'channelsInvite' 'invite'
],
resource: [
'channel',
], ],
}, },
}, },
@ -107,7 +165,10 @@ export class Slack implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: [
'channelsInvite' 'invite'
],
resource: [
'channel',
], ],
}, },
}, },
@ -116,8 +177,13 @@ export class Slack implements INodeType {
}, },
// ---------------------------------- // ----------------------------------
// chatPostMessage // message
// ----------------------------------
// ----------------------------------
// message:post
// ---------------------------------- // ----------------------------------
{ {
displayName: 'Channel', displayName: 'Channel',
@ -128,7 +194,10 @@ export class Slack implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: [
'chatPostMessage' 'post'
],
resource: [
'message',
], ],
}, },
}, },
@ -146,7 +215,10 @@ export class Slack implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: [
'chatPostMessage' 'post'
],
resource: [
'message',
], ],
}, },
}, },
@ -160,7 +232,10 @@ export class Slack implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: [
'chatPostMessage' 'post'
],
resource: [
'message',
], ],
}, },
}, },
@ -173,12 +248,15 @@ export class Slack implements INodeType {
default: '', default: '',
displayOptions: { displayOptions: {
show: { show: {
operation: [
'chatPostMessage'
],
as_user: [ as_user: [
false false
], ],
operation: [
'post'
],
resource: [
'message',
],
}, },
}, },
description: 'Set the bot\'s user name.', description: 'Set the bot\'s user name.',
@ -194,7 +272,10 @@ export class Slack implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: [
'chatPostMessage' 'post'
],
resource: [
'message',
], ],
}, },
}, },
@ -382,7 +463,10 @@ export class Slack implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: [
'chatPostMessage' 'post'
],
resource: [
'message',
], ],
}, },
}, },
@ -399,6 +483,12 @@ export class Slack implements INodeType {
'/as_user': [ '/as_user': [
false false
], ],
'/operation': [
'post'
],
'/resource': [
'message',
],
}, },
}, },
default: '', default: '',
@ -413,6 +503,12 @@ export class Slack implements INodeType {
'/as_user': [ '/as_user': [
false false
], ],
'/operation': [
'post'
],
'/resource': [
'message',
],
}, },
}, },
default: '', default: '',
@ -478,8 +574,9 @@ export class Slack implements INodeType {
} }
const baseUrl = `https://slack.com/api/`; const baseUrl = `https://slack.com/api/`;
const operation = this.getNodeParameter('operation', 0) as string; let operation: string;
let requestMethod = 'GET'; let resource: string;
let requestMethod = 'POST';
// For Post // For Post
let body: IDataObject; let body: IDataObject;
@ -491,61 +588,70 @@ export class Slack implements INodeType {
body = {}; body = {};
qs = {}; qs = {};
if (operation === 'channelsCreate') { resource = this.getNodeParameter('resource', i) as string;
// ---------------------------------- operation = this.getNodeParameter('operation', i) as string;
// channelsCreate
// ----------------------------------
requestMethod = 'POST'; if (resource === 'channel') {
endpoint = 'channels.create'; if (operation === 'create') {
// ----------------------------------
// channel:create
// ----------------------------------
body.name = this.getNodeParameter('channel', i) as string; requestMethod = 'POST';
} else if (operation === 'chatPostMessage') { endpoint = 'channels.create';
// ----------------------------------
// chatPostMessage
// ----------------------------------
requestMethod = 'POST'; body.name = this.getNodeParameter('channel', i) as string;
endpoint = 'chat.postMessage'; } else if (operation === 'invite') {
// ----------------------------------
// channel:invite
// ----------------------------------
body.channel = this.getNodeParameter('channel', i) as string; requestMethod = 'POST';
body.text = this.getNodeParameter('text', i) as string; endpoint = 'channels.invite';
body.as_user = this.getNodeParameter('as_user', i) as boolean;
if (body.as_user === false) { body.channel = this.getNodeParameter('channel', i) as string;
body.username = this.getNodeParameter('username', i) as string; body.user = this.getNodeParameter('username', i) as string;
} }
} else if (resource === 'message') {
if (operation === 'post') {
// ----------------------------------
// message:post
// ----------------------------------
const attachments = this.getNodeParameter('attachments', i, []) as unknown as Attachment[]; requestMethod = 'POST';
endpoint = 'chat.postMessage';
// The node does save the fields data differently than the API body.channel = this.getNodeParameter('channel', i) as string;
// expects so fix the data befre we send the request body.text = this.getNodeParameter('text', i) as string;
for (const attachment of attachments) { body.as_user = this.getNodeParameter('as_user', i) as boolean;
if (attachment.fields !== undefined) { if (body.as_user === false) {
if (attachment.fields.item !== undefined) { body.username = this.getNodeParameter('username', i) as string;
// Move the field-content up }
// @ts-ignore
attachment.fields = attachment.fields.item; const attachments = this.getNodeParameter('attachments', i, []) as unknown as Attachment[];
} else {
// If it does not have any items set remove it // The node does save the fields data differently than the API
delete attachment.fields; // 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);
} }
body['attachments'] = attachments; } else {
throw new Error(`The resource "${resource}" is not known!`);
// 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 = { const options = {