diff --git a/packages/pieces/community/slack/package.json b/packages/pieces/community/slack/package.json index d0081e7c7b8..f49536488f9 100644 --- a/packages/pieces/community/slack/package.json +++ b/packages/pieces/community/slack/package.json @@ -1,6 +1,6 @@ { "name": "@activepieces/piece-slack", - "version": "0.11.4", + "version": "0.11.5", "dependencies": { "@slack/web-api": "7.9.0", "slackify-markdown": "4.4.0" diff --git a/packages/pieces/community/slack/src/lib/triggers/new-message.ts b/packages/pieces/community/slack/src/lib/triggers/new-message.ts index 5b92ba83dd8..32b2868c5d0 100644 --- a/packages/pieces/community/slack/src/lib/triggers/new-message.ts +++ b/packages/pieces/community/slack/src/lib/triggers/new-message.ts @@ -9,10 +9,28 @@ export const newMessageTrigger = createTrigger({ props: { ignoreBots: Property.Checkbox({ displayName: 'Ignore Bot Messages ?', - required: true, + required: false, + defaultValue: false, + }), + keyword: Property.ShortText({ + displayName: 'Keyword', + description: 'The keyword to search for in messages. Leave empty to trigger on all messages.', + required: false, + }), + includeThreads: Property.Checkbox({ + displayName: 'Include Thread Messages', + description: 'If enabled, also triggers on matches in thread replies', + required: false, + defaultValue: true, + }), + matchWholeWord: Property.Checkbox({ + displayName: 'Match Whole Word Only', + description: 'If enabled, only matches the keyword as a complete word (e.g. "test" won\'t match "testing").', + required: false, defaultValue: false, }), }, + type: TriggerStrategy.APP_WEBHOOK, sampleData: undefined, onEnable: async (context) => { @@ -29,24 +47,58 @@ export const newMessageTrigger = createTrigger({ run: async (context) => { const payloadBody = context.payload.body as PayloadBody; + const {ignoreBots, keyword, includeThreads, matchWholeWord} = context.propsValue; - // check if it's channel message - if (!['channel','group'].includes(payloadBody.event.channel_type)) { + // ignore system messages (joins, leaves, file shares, etc.) + if (payloadBody.event.subtype) { return []; } - - // check for bot messages - if (context.propsValue.ignoreBots && payloadBody.event.bot_id) { + + if (ignoreBots && payloadBody.event.bot_id) { return []; } + // check if it's channel message + if (!['channel','group'].includes(payloadBody.event.channel_type)) { + if (!includeThreads || !payloadBody.event.thread_ts) { + return []; + } + } + + // if keyword is provided, check if the message contains the keyword + if (keyword && keyword.trim()) { + if (!payloadBody.event.text) { + return []; + } + + const messageText = payloadBody.event.text.toLowerCase(); + const searchKeyword = keyword.toLowerCase(); + let keywordFound = false; + + if (matchWholeWord) { + const wordRegex = new RegExp(`\\b${searchKeyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`, 'i'); + keywordFound = wordRegex.test(payloadBody.event.text); + } else { + keywordFound = messageText.includes(searchKeyword); + } + + if (!keywordFound) { + return []; + } + } + return [payloadBody.event]; + }, }); type PayloadBody = { event: { + text?: string; channel: string; + user?: string; + channel_type:string; bot_id?: string; - channel_type:string + thread_ts?: string; + subtype?: string; }; };