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

@@ -1,5 +1,6 @@
import findMessage from './find-message'; import findMessage from './find-message';
import findUserByEmail from './find-user-by-email'; import findUserByEmail from './find-user-by-email';
import sendMessageToChannel from './send-a-message-to-channel'; 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];

View File

@@ -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;
},
});

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;

View File

@@ -1,3 +1,4 @@
import listChannels from './list-channels'; import listChannels from './list-channels';
import listUsers from './list-users';
export default [listChannels]; export default [listChannels, listUsers];

View File

@@ -36,7 +36,7 @@ export default {
do { do {
const response: TResponse = await $.http.get('/conversations.list', { const response: TResponse = await $.http.get('/conversations.list', {
params: { params: {
types: 'public_channel,private_channel,im', types: 'public_channel,private_channel',
cursor: nextCursor, cursor: nextCursor,
limit: 1000, limit: 1000,
} }

View File

@@ -0,0 +1,66 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
type TMember = {
id: string;
profile: {
real_name_normalized: string;
};
}
type TUserListResponseData = {
members: TMember[],
response_metadata?: {
next_cursor: string
};
needed?: string;
error?: string;
ok: boolean;
}
type TResponse = {
data: TUserListResponseData;
}
export default {
name: 'List users',
key: 'listUsers',
async run($: IGlobalVariable) {
const users: {
data: IJSONObject[];
error: IJSONObject | null;
} = {
data: [],
error: null,
};
let nextCursor;
do {
const response: TResponse = await $.http.get('/users.list', {
params: {
cursor: nextCursor,
limit: 1000,
}
});
nextCursor = response.data.response_metadata?.next_cursor;
if (response.data.error === 'missing_scope') {
throw new Error(`Missing "${response.data.needed}" scope while authorizing. Please, reconnect your connection!`);
}
if (response.data.ok === false) {
throw new Error(JSON.stringify(response.data, null, 2));
}
for (const member of response.data.members) {
users.data.push({
value: member.id as string,
name: member.profile.real_name_normalized as string,
});
}
} while (nextCursor);
return users;
},
};

View File

@@ -7,6 +7,8 @@ items:
desc: Finds a user by email. desc: Finds a user by email.
- name: Send a message to channel - name: Send a message to channel
desc: Sends a message to a channel you specify. desc: Sends a message to a channel you specify.
- name: Send a direct message
desc: Sends a direct message to a user or yourself from the Slackbot.
--- ---
<script setup> <script setup>