Feature/slack node extended (#1239)

* Added reaction method to message

* Increased limit for channels on getChannels, removed unused fields in the code

* Using own operation for reactions

* Registering reaction fields and operations

* Added Operation "Reaction" to Slack.node.ts

* Fixing variable name for emoji

* now removing reaction on "remove" instead of "add"

* Using GET for reactions.get and passing arguments as query

* Added members operation

* Fixed typo in timestamp

* Added user.info and user.getPresence

* Fixed: wrong operation name

*  Improvements to #1238

*  Add field resolve data when retrieving channel members

*  Minor improvements to Slack-Node

Co-authored-by: Andreas Scherren <ascherren@brightfuture.de>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
Ricardo Espinoza 2020-12-13 04:47:52 -05:00 committed by GitHub
parent 5e56dcb037
commit 532503b69f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 509 additions and 126 deletions

View file

@ -8,13 +8,15 @@ const userScopes = [
'chat:write',
'files:read',
'files:write',
'groups:read',
'im:read',
'mpim:read',
'reactions:read',
'reactions:write',
'stars:read',
'stars:write',
'users.profile:read',
'users.profile:write',
'groups:read',
'im:read',
'mpim:read',
];
export class SlackOAuth2Api implements ICredentialType {

View file

@ -1,4 +1,6 @@
import { INodeProperties } from 'n8n-workflow';
import {
INodeProperties,
} from 'n8n-workflow';
export const channelOperations = [
{
@ -63,6 +65,11 @@ export const channelOperations = [
value: 'leave',
description: 'Leaves a conversation.',
},
{
name: 'Member',
value: 'member',
description: 'List members of a conversation.',
},
{
name: 'Open',
value: 'open',
@ -125,6 +132,7 @@ export const channelFields = [
required: true,
description: 'The name of the channel to archive.',
},
/* -------------------------------------------------------------------------- */
/* channel:close */
/* -------------------------------------------------------------------------- */
@ -149,6 +157,7 @@ export const channelFields = [
required: true,
description: 'The name of the channel to close.',
},
/* -------------------------------------------------------------------------- */
/* channel:create */
/* -------------------------------------------------------------------------- */
@ -197,6 +206,7 @@ export const channelFields = [
},
],
},
/* -------------------------------------------------------------------------- */
/* channel:invite */
/* -------------------------------------------------------------------------- */
@ -242,6 +252,7 @@ export const channelFields = [
required: true,
description: 'The ID of the user to invite into channel.',
},
/* -------------------------------------------------------------------------- */
/* channel:get */
/* -------------------------------------------------------------------------- */
@ -288,6 +299,7 @@ export const channelFields = [
},
],
},
/* -------------------------------------------------------------------------- */
/* channel:kick */
/* -------------------------------------------------------------------------- */
@ -332,6 +344,7 @@ export const channelFields = [
},
default: '',
},
/* -------------------------------------------------------------------------- */
/* channel:join */
/* -------------------------------------------------------------------------- */
@ -356,6 +369,7 @@ export const channelFields = [
},
required: true,
},
/* -------------------------------------------------------------------------- */
/* channel:getAll */
/* -------------------------------------------------------------------------- */
@ -451,6 +465,7 @@ export const channelFields = [
},
],
},
/* -------------------------------------------------------------------------- */
/* channel:history */
/* -------------------------------------------------------------------------- */
@ -557,6 +572,7 @@ export const channelFields = [
},
],
},
/* -------------------------------------------------------------------------- */
/* channel:leave */
/* -------------------------------------------------------------------------- */
@ -581,6 +597,86 @@ export const channelFields = [
required: true,
description: 'The name of the channel to leave.',
},
/* -------------------------------------------------------------------------- */
/* channel:member */
/* -------------------------------------------------------------------------- */
{
displayName: 'Channel',
name: 'channelId',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getChannels',
},
default: '',
displayOptions: {
show: {
operation: [
'member',
],
resource: [
'channel',
],
},
},
required: true,
},
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
resource: [
'channel',
],
operation: [
'member',
],
},
},
default: false,
description: 'If all results should be returned or only up to a given limit.',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
default: 100,
placeholder: 'Limit',
displayOptions: {
show: {
operation: [
'member',
],
resource: [
'channel',
],
returnAll: [
false,
],
},
},
required: false,
},
{
displayName: 'Resolve Data',
name: 'resolveData',
type: 'boolean',
default: false,
displayOptions: {
show: {
resource: [
'channel',
],
operation: [
'member',
],
},
},
description: 'By default the response only contain the ID to resource. If this<br />option gets activated it will resolve the data automatically.',
},
/* -------------------------------------------------------------------------- */
/* channel:open */
/* -------------------------------------------------------------------------- */
@ -627,6 +723,7 @@ export const channelFields = [
},
],
},
/* -------------------------------------------------------------------------- */
/* channel:rename */
/* -------------------------------------------------------------------------- */
@ -669,6 +766,7 @@ export const channelFields = [
required: true,
description: 'New name for conversation.',
},
/* -------------------------------------------------------------------------- */
/* channel:replies */
/* -------------------------------------------------------------------------- */
@ -793,6 +891,7 @@ export const channelFields = [
},
],
},
/* -------------------------------------------------------------------------- */
/* channel:setPurpose */
/* -------------------------------------------------------------------------- */
@ -835,6 +934,7 @@ export const channelFields = [
required: true,
description: 'A new, specialer purpose',
},
/* -------------------------------------------------------------------------- */
/* channel:setTopic */
/* -------------------------------------------------------------------------- */
@ -877,6 +977,7 @@ export const channelFields = [
required: true,
description: 'The new topic string. Does not support formatting or linkification.',
},
/* -------------------------------------------------------------------------- */
/* channel:unarchive */
/* -------------------------------------------------------------------------- */

View file

@ -159,6 +159,7 @@ export const fileFields = [
},
],
},
/* ----------------------------------------------------------------------- */
/* file:getAll */
/* ----------------------------------------------------------------------- */
@ -261,29 +262,29 @@ export const fileFields = [
value: 'all',
},
{
name: 'Spaces',
value: 'spaces',
},
{
name: 'Snippets',
value: 'snippets',
name: 'Google Docs',
value: 'gdocs',
},
{
name: 'Images',
value: 'images',
},
{
name: 'Google Docs',
value: 'gdocs',
name: 'Snippets',
value: 'snippets',
},
{
name: 'Zips',
value: 'zips',
name: 'Spaces',
value: 'spaces',
},
{
name: 'pdfs',
value: 'pdfs',
},
{
name: 'Zips',
value: 'zips',
},
],
default: ['all'],
description: 'Filter files by type',
@ -300,6 +301,7 @@ export const fileFields = [
},
],
},
/* ----------------------------------------------------------------------- */
/* file:get */
/* ----------------------------------------------------------------------- */

View file

@ -382,6 +382,13 @@ export const messageFields = [
default: '',
description: 'URL to an image to use as the icon for this message.',
},
{
displayName: 'Link Names',
name: 'link_names',
type: 'boolean',
default: false,
description: 'Find and link channel names and usernames.',
},
{
displayName: 'Make Reply',
name: 'thread_ts',
@ -389,20 +396,6 @@ export const messageFields = [
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',
@ -418,14 +411,22 @@ export const messageFields = [
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',
displayName: 'Unfurl Links',
name: 'unfurl_links',
type: 'boolean',
default: false,
description: 'Find and link channel names and usernames.',
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.',
},
],
},
/* ----------------------------------------------------------------------- */
/* message:update */
/* ----------------------------------------------------------------------- */

View file

@ -4,4 +4,3 @@ export interface IAttachment {
item?: object[];
};
}

View file

@ -0,0 +1,101 @@
import { INodeProperties } from 'n8n-workflow';
export const reactionOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'reaction',
],
},
},
options: [
{
name: 'Add',
value: 'add',
description: 'Adds a reaction to a message',
},
{
name: 'Get',
value: 'get',
description: 'Get the reactions of a message',
},
{
name: 'Remove',
value: 'remove',
description: 'Remove a reaction of a message',
},
],
default: 'add',
description: 'The operation to perform.',
},
] as INodeProperties[];
export const reactionFields = [
{
displayName: 'Channel',
name: 'channelId',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getChannels',
},
required: true,
default: '',
displayOptions: {
show: {
resource: [
'reaction',
],
operation: [
'add',
'get',
'remove',
],
},
},
description: 'Channel containing the message.',
},
{
displayName: 'Emoji',
name: 'name',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'reaction',
],
operation: [
'add',
'remove',
],
},
},
description: 'Name of emoji.',
placeholder: '+1',
},
{
displayName: 'Timestamp',
name: 'timestamp',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'reaction',
],
operation: [
'add',
'get',
'remove',
],
},
},
description: `Timestamp of the message.`,
},
] as INodeProperties[];

View file

@ -32,6 +32,16 @@ import {
fileOperations,
} from './FileDescription';
import {
reactionFields,
reactionOperations,
} from './ReactionDescription';
import {
userFields,
userOperations,
} from './UserDescription';
import {
userProfileFields,
userProfileOperations,
@ -46,6 +56,7 @@ import {
import {
IAttachment,
} from './MessageInterface';
import moment = require('moment');
interface Attachment {
@ -163,10 +174,18 @@ export class Slack implements INodeType {
name: 'Message',
value: 'message',
},
{
name: 'Reaction',
value: 'reaction',
},
{
name: 'Star',
value: 'star',
},
{
name: 'User',
value: 'user',
},
{
name: 'User Profile',
value: 'userProfile',
@ -184,6 +203,10 @@ export class Slack implements INodeType {
...starFields,
...fileOperations,
...fileFields,
...reactionOperations,
...reactionFields,
...userOperations,
...userFields,
...userProfileOperations,
...userProfileFields,
],
@ -217,7 +240,7 @@ export class Slack implements INodeType {
// select them easily
async getChannels(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const qs = { types: 'public_channel,private_channel' };
const qs = { types: 'public_channel,private_channel', limit: 1000 };
const channels = await slackApiRequestAllItems.call(this, 'channels', 'GET', '/conversations.list', {}, qs);
for (const channel of channels) {
const channelName = channel.name;
@ -383,6 +406,29 @@ export class Slack implements INodeType {
};
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 }));
}
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;
}
}
//https://api.slack.com/methods/conversations.open
if (operation === 'open') {
const options = this.getNodeParameter('options', i) as IDataObject;
@ -745,6 +791,36 @@ export class Slack implements INodeType {
responseData = await slackApiRequest.call(this, 'POST', '/chat.update', body, qs);
}
}
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);
}
//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);
}
//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);
}
}
if (resource === 'star') {
//https://api.slack.com/methods/stars.add
if (operation === 'add') {
@ -879,6 +955,19 @@ export class Slack implements INodeType {
responseData = responseData.file;
}
}
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;
}
//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);
}
}
if (resource === 'userProfile') {
//https://api.slack.com/methods/users.profile.set
if (operation === 'update') {

View file

@ -67,13 +67,6 @@ export const starFields = [
default: '',
description: 'Channel to add star to, or channel where the message to add star to was posted (used with timestamp).',
},
{
displayName: 'File ID',
name: 'fileId',
type: 'string',
default: '',
description: 'File to add star to.',
},
{
displayName: 'File Comment',
name: 'fileComment',
@ -81,6 +74,13 @@ export const starFields = [
default: '',
description: 'File comment to add star to.',
},
{
displayName: 'File ID',
name: 'fileId',
type: 'string',
default: '',
description: 'File to add star to.',
},
{
displayName: 'Timestamp',
name: 'timestamp',
@ -90,6 +90,7 @@ export const starFields = [
},
],
},
/* ----------------------------------------------------------------------- */
/* star:delete */
/* ----------------------------------------------------------------------- */
@ -144,6 +145,7 @@ export const starFields = [
},
],
},
/* -------------------------------------------------------------------------- */
/* star:getAll */
/* -------------------------------------------------------------------------- */

View file

@ -0,0 +1,85 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const userOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'user',
],
},
},
options: [
{
name: 'Info',
value: 'info',
description: `Get information about a user`,
},
{
name: 'Get Presence',
value: 'getPresence',
description: `Get online status of a user`,
},
],
default: 'info',
description: 'The operation to perform.',
},
] as INodeProperties[];
export const userFields = [
/* -------------------------------------------------------------------------- */
/* user:info */
/* -------------------------------------------------------------------------- */
{
displayName: 'User ID',
name: 'user',
type: 'string',
typeOptions: {
loadOptionsMethod: 'getUsers',
},
default: '',
displayOptions: {
show: {
operation: [
'info',
],
resource: [
'user',
],
},
},
required: true,
description: 'The ID of the user to get information about.',
},
/* -------------------------------------------------------------------------- */
/* user:getPresence */
/* -------------------------------------------------------------------------- */
{
displayName: 'User ID',
name: 'user',
type: 'string',
typeOptions: {
loadOptionsMethod: 'getUsers',
},
default: '',
displayOptions: {
show: {
operation: [
'getPresence',
],
resource: [
'user',
],
},
},
required: true,
description: 'The ID of the user to get the online status of.',
},
] as INodeProperties[];

View file

@ -144,6 +144,7 @@ export const userProfileFields = [
},
],
},
/* -------------------------------------------------------------------------- */
/* userProfile:get */
/* -------------------------------------------------------------------------- */