Skip to content

Commit

Permalink
Merge pull request #4727 from kishanprmr/slack-message-ts
Browse files Browse the repository at this point in the history
feat(slack): add timestamp URL option
  • Loading branch information
abuaboud committed May 20, 2024
2 parents 48b0192 + 1d24d6b commit 9622222
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 43 deletions.
2 changes: 1 addition & 1 deletion packages/pieces/community/slack/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "@activepieces/piece-slack",
"version": "0.5.9"
"version": "0.5.10"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,47 @@ import { createAction, Property } from '@activepieces/pieces-framework';
import { slackChannel, slackInfo } from '../common/props';

import { WebClient } from '@slack/web-api';
import { processMessageTimestamp } from '../common/utils';

export const addRectionToMessageAction = createAction({
auth: slackAuth,
name: 'slack-add-reaction-to-message',
displayName: 'Add Reaction to Message',
description: 'Add an emoji reaction to a message.',

props: {
info: slackInfo,
channel: slackChannel,
ts: Property.ShortText({
displayName: 'Message ts',
description:
'Provide the ts (timestamp) value of the message to update, e.g. `1710304378.475129`.',
required: true,
}),
reaction: Property.ShortText({
displayName: 'Reaction (emoji) name',
required: true,
description: 'e.g.`thumbsup`',
}),
},

async run(context) {
const { channel, ts, reaction } = context.propsValue;

const slack = new WebClient(context.auth.access_token);

const response = await slack.reactions.add({
channel,
timestamp: ts,
name: reaction,
});

return response;
},
auth: slackAuth,
name: 'slack-add-reaction-to-message',
displayName: 'Add Reaction to Message',
description: 'Add an emoji reaction to a message.',

props: {
info: slackInfo,
channel: slackChannel,
ts: Property.ShortText({
displayName: 'Message Timestamp',
description:
'Please provide the timestamp of the message you wish to react, such as `1710304378.475129`. Alternatively, you can easily obtain the message link by clicking on the three dots next to the message and selecting the `Copy link` option.',
required: true,
}),
reaction: Property.ShortText({
displayName: 'Reaction (emoji) name',
required: true,
description: 'e.g.`thumbsup`',
}),
},

async run(context) {
const { channel, ts, reaction } = context.propsValue;

const slack = new WebClient(context.auth.access_token);

const messageTimestamp = processMessageTimestamp(ts);

if (messageTimestamp) {
const response = await slack.reactions.add({
channel,
timestamp: messageTimestamp,
name: reaction,
});

return response;
} else {
throw new Error('Invalid Timestamp Value.');
}
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@activepieces/pieces-common';
import { slackAuth } from '../..';
import { blocks, slackChannel, slackInfo } from '../common/props';
import { processMessageTimestamp } from '../common/utils';

export const updateMessage = createAction({
// auth: check https://www.activepieces.com/docs/developers/piece-reference/authentication,
Expand All @@ -18,9 +19,9 @@ export const updateMessage = createAction({
info: slackInfo,
channel: slackChannel,
ts: Property.ShortText({
displayName: 'Message ts',
displayName: 'Message Timestamp',
description:
'Provide the ts (timestamp) value of the message to update, e.g. `1710304378.475129`.',
'Please provide the timestamp of the message you wish to update, such as `1710304378.475129`. Alternatively, you can easily obtain the message link by clicking on the three dots next to the message and selecting the `Copy link` option.',
required: true,
}),
text: Property.LongText({
Expand All @@ -31,12 +32,17 @@ export const updateMessage = createAction({
blocks,
},
async run({ auth, propsValue }) {
const messageTimestamp = processMessageTimestamp(propsValue.ts);
if (!messageTimestamp) {
throw new Error('Invalid Timestamp Value.');
}

const request: HttpRequest = {
method: HttpMethod.POST,
url: 'https://slack.com/api/chat.update',
body: {
channel: propsValue.channel,
ts: propsValue.ts,
ts: messageTimestamp,
text: propsValue.text,
blocks: propsValue.blocks,
},
Expand Down
32 changes: 27 additions & 5 deletions packages/pieces/community/slack/src/lib/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ export const slackSendMessage = async ({
if (!response.body.ok) {
switch (response.body.error) {
case 'not_in_channel':
throw new Error(JSON.stringify({
message: 'The bot is not in the channel',
code: 'not_in_channel',
action: 'Invite the bot from the channel settings'
}));
throw new Error(
JSON.stringify({
message: 'The bot is not in the channel',
code: 'not_in_channel',
action: 'Invite the bot from the channel settings',
})
);
default: {
throw new Error(response.body.error);
}
Expand All @@ -95,3 +97,23 @@ type SlackSendMessageParams = {
file?: ApFile;
threadTs?: string;
};

export function processMessageTimestamp(input: string) {
// Regular expression to match a URL containing the timestamp
const urlRegex = /\/p(\d+)(\d{6})$/;
// Check if the input is a URL
const urlMatch = input.match(urlRegex);
if (urlMatch) {
const timestamp = `${urlMatch[1]}.${urlMatch[2]}`;
return timestamp;
}

// Check if the input is already in the desired format
const timestampRegex = /^(\d+)\.(\d{6})$/;
const timestampMatch = input.match(timestampRegex);
if (timestampMatch) {
return input;
}

return null;
}

0 comments on commit 9622222

Please sign in to comment.