feat(slack): send direct message
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import findMessage from './find-message';
|
||||
import findUserByEmail from './find-user-by-email';
|
||||
import sendMessageToChannel from './send-a-message-to-channel';
|
||||
import sendDirectMessage from './send-a-direct-message';
|
||||
|
||||
export default [findMessage, findUserByEmail, sendMessageToChannel];
|
||||
export default [findMessage, findUserByEmail, sendMessageToChannel, sendDirectMessage];
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
import defineAction from '../../../../helpers/define-action';
|
||||
import postMessage from './post-message';
|
||||
|
||||
export default defineAction({
|
||||
name: 'Send a direct message',
|
||||
key: 'sendDirectMessage',
|
||||
description: 'Sends a direct message to a user or yourself from the Slackbot.',
|
||||
arguments: [
|
||||
{
|
||||
label: 'To username',
|
||||
key: 'toUsername',
|
||||
type: 'dropdown' as const,
|
||||
required: true,
|
||||
description: 'Pick a user to send the message to.',
|
||||
variables: false,
|
||||
source: {
|
||||
type: 'query',
|
||||
name: 'getDynamicData',
|
||||
arguments: [
|
||||
{
|
||||
name: 'key',
|
||||
value: 'listUsers',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
label: 'Message text',
|
||||
key: 'message',
|
||||
type: 'string' as const,
|
||||
required: true,
|
||||
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,
|
||||
},
|
||||
],
|
||||
additionalFields: {
|
||||
type: 'query',
|
||||
name: 'getDynamicFields',
|
||||
arguments: [
|
||||
{
|
||||
name: 'key',
|
||||
value: 'listFieldsAfterSendAsBot',
|
||||
},
|
||||
{
|
||||
name: 'parameters.sendAsBot',
|
||||
value: '{parameters.sendAsBot}',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
async run($) {
|
||||
const message = await postMessage($);
|
||||
|
||||
return message;
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
import { IGlobalVariable } from '@automatisch/types';
|
||||
import { URL } from 'url';
|
||||
|
||||
type TData = {
|
||||
channel: string;
|
||||
text: string;
|
||||
username?: string;
|
||||
icon_url?: string;
|
||||
icon_emoji?: string;
|
||||
};
|
||||
|
||||
const postMessage = async ($: IGlobalVariable) => {
|
||||
const { parameters } = $.step;
|
||||
const toUsername = parameters.toUsername 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: toUsername,
|
||||
text,
|
||||
};
|
||||
|
||||
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,
|
||||
};
|
||||
|
||||
$.setActionItem(message);
|
||||
};
|
||||
|
||||
export default postMessage;
|
||||
Reference in New Issue
Block a user