Skip to content

Commit a1acbfb

Browse files
committed
Add addToChannel prop to message-based triggers
and remove historical messages for new-keyword-mention source since the search:read scope is no longer requested.
1 parent 9a61fa5 commit a1acbfb

File tree

5 files changed

+62
-33
lines changed

5 files changed

+62
-33
lines changed

components/slack_v2_test/slack_v2_test.app.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,13 @@ export default {
465465
default: 1,
466466
optional: true,
467467
},
468+
addToChannel: {
469+
type: "boolean",
470+
label: "Add app to channel automatically?",
471+
description: "If `true`, the app will be added to the specified channel(s) automatically. If `false`, you must add the app to the channel manually. Defaults to `true`.",
472+
default: true,
473+
optional: false,
474+
},
468475
},
469476
methods: {
470477
getChannelLabel(resource) {

components/slack_v2_test/sources/common/base.mjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,28 @@ export default {
160160
},
161161
);
162162
},
163+
async getBotInfo() {
164+
return await this.slack.authTest({
165+
as_bot: true,
166+
});
167+
},
168+
async addAppToChannels(channelIds = []) {
169+
const { user_id: botUserId } = await this.getBotInfo();
170+
// XXX: Don't try to add the app to DM or group DM channels,
171+
// it will result in an error: method_not_supported_for_channel_type
172+
for (const channel of channelIds) {
173+
try {
174+
await this.slack.inviteToConversation({
175+
channel,
176+
users: botUserId,
177+
});
178+
} catch (error) {
179+
if (!error.includes("already_in_channel")) {
180+
throw error;
181+
}
182+
}
183+
}
184+
},
163185
processEvent(event) {
164186
return event;
165187
},

components/slack_v2_test/sources/new-keyword-mention/new-keyword-mention.mjs

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ export default {
3838
"keyword",
3939
],
4040
},
41+
addToChannel: {
42+
propDefinition: [
43+
common.props.slack,
44+
"addToChannel",
45+
],
46+
},
4147
ignoreBot: {
4248
propDefinition: [
4349
common.props.slack,
@@ -47,43 +53,15 @@ export default {
4753
},
4854
hooks: {
4955
...common.hooks,
50-
async deploy() {
51-
// emit historical events
52-
const messages = await this.getMatches({
53-
query: this.keyword,
54-
sort: "timestamp",
55-
});
56-
const filteredMessages = this.conversations?.length > 0
57-
? messages.filter((message) => this.conversations.includes(message.channel.id))
58-
: messages;
59-
await this.emitHistoricalEvents(filteredMessages.slice(-25).reverse());
56+
async activate() {
57+
if (this.addToChannel && this.conversations?.length) {
58+
if (this.conversations[0] === "message") return;
59+
await this.addAppToChannels(this.conversations);
60+
}
6061
},
6162
},
6263
methods: {
6364
...common.methods,
64-
async getMatches(params) {
65-
return (await this.slack.searchMessages(params)).messages.matches || [];
66-
},
67-
async emitHistoricalEvents(messages) {
68-
for (const message of messages) {
69-
const event = await this.processEvent({
70-
...message,
71-
subtype: message.subtype || constants.SUBTYPE.PD_HISTORY_MESSAGE,
72-
});
73-
if (event) {
74-
if (!event.client_msg_id) {
75-
event.pipedream_msg_id = `pd_${Date.now()}_${Math.random().toString(36)
76-
.substr(2, 10)}`;
77-
}
78-
79-
this.$emit(event, {
80-
id: event.client_msg_id || event.pipedream_msg_id,
81-
summary: this.getSummary(event),
82-
ts: event.event_ts || Date.now(),
83-
});
84-
}
85-
}
86-
},
8765
getSummary() {
8866
return "New keyword mention received";
8967
},

components/slack_v2_test/sources/new-message-in-channels/new-message-in-channels.mjs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ export default {
3232
];
3333
},
3434
},
35+
addToChannel: {
36+
propDefinition: [
37+
common.props.slack,
38+
"addToChannel",
39+
],
40+
},
3541
resolveNames: {
3642
propDefinition: [
3743
common.props.slack,
@@ -51,6 +57,14 @@ export default {
5157
optional: true,
5258
},
5359
},
60+
hooks: {
61+
async activate() {
62+
if (this.addToChannel && this.conversations?.length) {
63+
if (this.conversations[0] === "message") return;
64+
await this.addAppToChannels(this.conversations);
65+
}
66+
},
67+
},
5468
methods: {
5569
...common.methods,
5670
getSummary() {

components/slack_v2_test/sources/new-user-mention/new-user-mention.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ export default {
5252
],
5353
},
5454
},
55+
hooks: {
56+
async activate() {
57+
if (this.addToChannel && this.conversations?.length) {
58+
if (this.conversations[0] === "message") return;
59+
await this.addAppToChannels(this.conversations);
60+
}
61+
},
62+
},
5563
methods: {
5664
...common.methods,
5765
getSummary() {

0 commit comments

Comments
 (0)