feat(slack): send direct message

This commit is contained in:
Ali BARIN
2023-05-13 20:02:19 +00:00
parent 183b9b0d88
commit d23d5d2da0
7 changed files with 204 additions and 3 deletions

View File

@@ -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;