Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
key: "slack-add-emoji-reaction",
name: "Add Emoji Reaction",
description: "Add an emoji reaction to a message. [See the documentation](https://api.slack.com/methods/reactions.add)",
version: "0.0.16",
version: "0.0.17",
annotations: {
destructiveHint: false,
openWorldHint: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "slack-approve-workflow",
name: "Approve Workflow",
description: "Suspend the workflow until approved by a Slack message. [See the documentation](https://pipedream.com/docs/code/nodejs/rerun#flowsuspend)",
version: "0.0.5",
version: "0.0.6",
annotations: {
destructiveHint: false,
openWorldHint: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "slack-archive-channel",
name: "Archive Channel",
description: "Archive a channel. [See the documentation](https://api.slack.com/methods/conversations.archive)",
version: "0.0.24",
version: "0.0.25",
annotations: {
destructiveHint: true,
openWorldHint: true,
Expand Down
26 changes: 16 additions & 10 deletions components/slack/actions/common/send-message.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export default {
"as_user",
],
},
addToChannel: {
propDefinition: [
slack,
"addToChannel",
],
},
post_at: {
propDefinition: [
slack,
Expand Down Expand Up @@ -181,6 +187,14 @@ export default {
},
},
async run({ $ }) {
const channelId = await this.getChannelId();

if (this.addToChannel) {
await this.slack.maybeAddAppToChannels([
channelId,
]);
}

let blocks = this.blocks;

if (!blocks) {
Expand Down Expand Up @@ -216,7 +230,7 @@ export default {

const obj = {
text: this.text,
channel: await this.getChannelId(),
channel: channelId,
attachments: this.attachments,
unfurl_links: this.unfurl_links,
unfurl_media: this.unfurl_media,
Expand All @@ -241,15 +255,7 @@ export default {
const { channel } = await this.slack.conversationsInfo({
channel: resp.channel,
});
let channelName = `#${channel?.name}`;
if (channel.is_im) {
const { profile } = await this.slack.getUserProfile({
user: channel.user,
});
channelName = `@${profile.real_name}`;
} else if (channel.is_mpim) {
channelName = `@${channel.purpose.value}`;
}
const channelName = await this.slack.getChannelDisplayName(channel);
$.export("$summary", `Successfully sent a message to ${channelName}`);
return resp;
},
Expand Down
2 changes: 1 addition & 1 deletion components/slack/actions/create-channel/create-channel.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
key: "slack-create-channel",
name: "Create a Channel",
description: "Create a new channel. [See the documentation](https://api.slack.com/methods/conversations.create)",
version: "0.0.25",
version: "0.0.26",
annotations: {
destructiveHint: false,
openWorldHint: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
key: "slack-create-reminder",
name: "Create Reminder",
description: "Create a reminder. [See the documentation](https://api.slack.com/methods/reminders.add)",
version: "0.0.25",
version: "0.0.26",
annotations: {
destructiveHint: false,
openWorldHint: true,
Expand Down
2 changes: 1 addition & 1 deletion components/slack/actions/delete-file/delete-file.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
key: "slack-delete-file",
name: "Delete File",
description: "Delete a file. [See the documentation](https://api.slack.com/methods/files.delete)",
version: "0.0.24",
version: "0.0.25",
annotations: {
destructiveHint: true,
openWorldHint: true,
Expand Down
2 changes: 1 addition & 1 deletion components/slack/actions/delete-message/delete-message.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
key: "slack-delete-message",
name: "Delete Message",
description: "Delete a message. [See the documentation](https://api.slack.com/methods/chat.delete)",
version: "0.0.24",
version: "0.1.0",
annotations: {
destructiveHint: true,
openWorldHint: true,
Expand Down
177 changes: 151 additions & 26 deletions components/slack/actions/find-message/find-message.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import slack from "../../slack.app.mjs";
export default {
key: "slack-find-message",
name: "Find Message",
description: "Find a Slack message. [See the documentation](https://api.slack.com/methods/search.messages)",
version: "0.0.26",
description: "Find a Slack message. [See the documentation](https://api.slack.com/methods/assistant.search.context)",
version: "0.1.0",
annotations: {
destructiveHint: false,
openWorldHint: true,
Expand All @@ -19,18 +19,11 @@ export default {
"query",
],
},
teamId: {
propDefinition: [
slack,
"team",
],
optional: true,
},
maxResults: {
type: "integer",
label: "Max Results",
description: "The maximum number of messages to return",
default: 100,
default: 20,
optional: true,
},
sort: {
Expand All @@ -54,29 +47,161 @@ export default {
optional: true,
},
},
methods: {
normalizeAssistantMatch(match) {
if (!match || typeof match !== "object") {
return match;
}
const {
author_user_id: authorUserId,
team_id: teamId,
channel_id: channelId,
message_ts: messageTs,
content,
permalink,
is_author_bot: isAuthorBot,
message,
channel,
...rest
} = match;
const baseMessage = typeof message === "object"
? message
: {};
const channelInfo = channel && typeof channel === "object"
? {
...channel,
id: channel.id || channelId,
}
: channelId
? {
id: channelId,
}
: undefined;
const normalized = {
type: "message",
user: authorUserId,
team: teamId,
ts: messageTs,
text: content,
permalink,
channel: channelInfo,
...baseMessage,
...rest,
};
if (isAuthorBot !== undefined && normalized.is_author_bot === undefined) {
normalized.is_author_bot = isAuthorBot;
}
if (normalized.text == null) {
normalized.text = baseMessage.text || content;
}
if (normalized.ts == null) {
normalized.ts = baseMessage.ts || messageTs;
}
if (!normalized.channel && baseMessage.channel) {
normalized.channel = baseMessage.channel;
} else if (normalized.channel && baseMessage.channel && typeof baseMessage.channel === "object") {
normalized.channel = {
...normalized.channel,
...baseMessage.channel,
};
}
return normalized;
},
async searchWithAssistant(baseParams, maxResults) {
const matches = [];
let cursor;

do {
const response = await this.slack.assistantSearch({
...baseParams,
channel_types: "public_channel,private_channel",
cursor,
});
const messages = (response.results?.messages || [])
.map((item) => this.normalizeAssistantMatch(item));
matches.push(...messages);
cursor = response.response_metadata?.next_cursor;
} while (cursor && matches.length < maxResults);

return matches.slice(0, maxResults);
},
async searchWithSearchMessages(baseParams, maxResults) {
const matches = [];
let page = 1;
const count = Math.min(Math.max(maxResults, 1), 100);

while (matches.length < maxResults) {
const response = await this.slack.searchMessages({
...baseParams,
count,
page,
});
const pageMatches = response.messages?.matches || [];
matches.push(...pageMatches);

if (matches.length >= maxResults) {
break;
}

const pagination = response.messages?.pagination;
const paging = response.messages?.paging;
const hasMore = pagination
? pagination.page < pagination.page_count
: paging
? paging.page < paging.pages
: false;

if (!hasMore) {
break;
}

page += 1;
}

return matches.slice(0, maxResults);
},
shouldFallbackToSearchMessages(error) {
const errorCode = typeof error === "string"
? error
: error?.data?.error || error?.message;

if (!errorCode.includes("missing_scope")) {
return false;
}

const providedSources = [
error?.data?.provided,
error?.provided,
error?.original?.data?.provided,
].filter(Boolean);

const providedScopes = providedSources
.flatMap((value) => Array.isArray(value)
? value
: String(value).split(","))
.map((scope) => scope.trim())
.filter(Boolean);

return providedScopes.includes("search:read");
},
},
async run({ $ }) {
const matches = [];
const params = {
const maxResults = Math.max(this.maxResults ?? 20, 1);
const baseParams = {
query: this.query,
team_id: this.teamId,
sort: this.sort,
sort_dir: this.sortDirection,
page: 1,
};
let hasMore;
let matches;

do {
const { messages } = await this.slack.searchMessages(params);
matches.push(...messages.matches);
if (matches.length >= this.maxResults) {
break;
try {
matches = await this.searchWithAssistant(baseParams, maxResults);
} catch (error) {
if (this.shouldFallbackToSearchMessages(error)) {
matches = await this.searchWithSearchMessages(baseParams, maxResults);
} else {
throw error;
}
hasMore = messages.matches?.length;
params.page++;
} while (hasMore);

if (matches.length > this.maxResults) {
matches.length = this.maxResults;
}

$.export("$summary", `Found ${matches.length} matching message${matches.length === 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
key: "slack-find-user-by-email",
name: "Find User by Email",
description: "Find a user by matching against their email. [See the documentation](https://api.slack.com/methods/users.lookupByEmail)",
version: "0.0.24",
version: "0.0.25",
annotations: {
destructiveHint: false,
openWorldHint: true,
Expand Down
22 changes: 21 additions & 1 deletion components/slack/actions/get-file/get-file.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import constants from "../../common/constants.mjs";
import slack from "../../slack.app.mjs";

export default {
key: "slack-get-file",
name: "Get File",
description: "Return information about a file. [See the documentation](https://api.slack.com/methods/files.info)",
version: "0.0.24",
version: "0.1.0",
annotations: {
destructiveHint: false,
openWorldHint: true,
Expand All @@ -17,6 +18,19 @@ export default {
propDefinition: [
slack,
"conversation",
() => ({
types: [
constants.CHANNEL_TYPE.PUBLIC,
constants.CHANNEL_TYPE.PRIVATE,
],
}),
],
description: "Select a public or private channel",
},
addToChannel: {
propDefinition: [
slack,
"addToChannel",
],
},
file: {
Expand All @@ -30,6 +44,12 @@ export default {
},
},
async run({ $ }) {
if (this.addToChannel) {
await this.slack.maybeAddAppToChannels([
this.conversation,
]);
}

const response = await this.slack.getFileInfo({
file: this.file,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
key: "slack-invite-user-to-channel",
name: "Invite User to Channel",
description: "Invite a user to an existing channel. [See the documentation](https://api.slack.com/methods/conversations.invite)",
version: "0.0.24",
version: "0.0.25",
annotations: {
destructiveHint: false,
openWorldHint: true,
Expand Down
2 changes: 1 addition & 1 deletion components/slack/actions/kick-user/kick-user.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "slack-kick-user",
name: "Kick User",
description: "Remove a user from a conversation. [See the documentation](https://api.slack.com/methods/conversations.kick)",
version: "0.0.24",
version: "0.0.25",
annotations: {
destructiveHint: true,
openWorldHint: true,
Expand Down
Loading
Loading