diff --git a/packages/backend/src/apps/slack/actions/send-a-message-to-channel/index.ts b/packages/backend/src/apps/slack/actions/send-a-message-to-channel/index.ts index 7983f4c7..0b793961 100644 --- a/packages/backend/src/apps/slack/actions/send-a-message-to-channel/index.ts +++ b/packages/backend/src/apps/slack/actions/send-a-message-to-channel/index.ts @@ -32,13 +32,46 @@ export default defineAction({ description: 'The content of your new message.', variables: true, }, + { + label: 'Send as a bot?', + key: 'sendAsBot', + type: 'dropdown' as const, + required: false, + value: false, + description: 'If you choose no, this message will appear to come from you. Direct messages are always sent by bots.', + variables: false, + options: [ + { + label: 'Yes', + value: true, + }, + { + label: 'No', + value: false, + } + ] + }, + { + label: 'Bot name', + key: 'botName', + type: 'string' as const, + required: true, + value: 'Automatisch', + description: 'Specify the bot name which appears as a bold username above the message inside Slack. Defaults to Automatisch.', + variables: true, + }, + { + label: 'Bot icon', + key: 'botIcon', + type: 'string' as const, + required: false, + description: 'Either an image url or an emoji available to your team (surrounded by :). For example, https://example.com/icon_256.png or :robot_face:', + variables: true, + }, ], async run($) { - const channelId = $.step.parameters.channel as string; - const text = $.step.parameters.message as string; - - const message = await postMessage($, channelId, text); + const message = await postMessage($); return message; }, diff --git a/packages/backend/src/apps/slack/actions/send-a-message-to-channel/post-message.ts b/packages/backend/src/apps/slack/actions/send-a-message-to-channel/post-message.ts index c19e61d0..fd1f35cc 100644 --- a/packages/backend/src/apps/slack/actions/send-a-message-to-channel/post-message.ts +++ b/packages/backend/src/apps/slack/actions/send-a-message-to-channel/post-message.ts @@ -1,23 +1,54 @@ import { IGlobalVariable } from '@automatisch/types'; +import { URL } from 'url'; -const postMessage = async ( - $: IGlobalVariable, - channelId: string, - text: string -) => { - const params = { +type TData = { + channel: string; + text: string; + username?: string; + icon_url?: string; + icon_emoji?: string; +} + +const postMessage = async ($: IGlobalVariable) => { + const { parameters } = $.step; + const channelId = parameters.channel as string; + const text = parameters.message as string; + const sendAsBot = parameters.sendAsBot as boolean; + const botName = parameters.botName as string; + const botIcon = parameters.botIcon as string; + + const data: TData = { channel: channelId, text, }; - const response = await $.http.post('/chat.postMessage', params); + if (sendAsBot) { + data.username = botName; + try { + // challenging the input to check if it is a URL! + new URL(botIcon); + data.icon_url = botIcon; + } catch { + data.icon_emoji = botIcon; + } + } + + const customConfig = { + sendAsBot, + }; + + const response = await $.http.post( + '/chat.postMessage', + data, + { additionalProperties: customConfig }, + ); if (response.data.ok === false) { throw new Error(JSON.stringify(response.data)); } const message = { - raw: response?.data?.message, + raw: response?.data, }; $.setActionItem(message);