feat: add find message action in Slack

This commit is contained in:
Ali BARIN
2022-09-09 23:53:17 +02:00
parent 75b536959e
commit 3593727d29
8 changed files with 160 additions and 1 deletions

View File

@@ -0,0 +1,50 @@
import SlackClient from '../index';
export default class FindMessages {
client: SlackClient;
constructor(client: SlackClient) {
this.client = client;
}
async run(
query: string,
sortBy: string,
sortDirection: string,
count = 1,
) {
const headers = {
Authorization: `Bearer ${this.client.connection.formattedData.accessToken}`,
};
const params = {
query,
sort: sortBy,
sort_dir: sortDirection,
count,
};
const response = await this.client.httpClient.get(
'/search.messages',
{ headers, params }
);
const data = response.data;
const messages = data.messages.matches;
const message = messages?.[0];
if (!data.ok) {
if (data.error === 'missing_scope') {
throw new Error(
`Error occured while finding messages; ${data.error}: ${data.needed}`
)
}
throw new Error(
`Error occured while finding messages; ${data.error}`
);
}
return message;
}
}