2022-03-10 06:59:48 +00:00
|
|
|
import { QueryError, QueryResult, QueryService } from '@tooljet-plugins/common';
|
|
|
|
|
import got, { Headers } from 'got';
|
|
|
|
|
import { SourceOptions, QueryOptions } from './types';
|
2021-07-17 17:54:14 +00:00
|
|
|
|
|
|
|
|
export default class SlackQueryService implements QueryService {
|
|
|
|
|
authUrl(): string {
|
|
|
|
|
const clientId = process.env.SLACK_CLIENT_ID;
|
2023-11-01 09:39:41 +00:00
|
|
|
const host = process.env.TOOLJET_HOST;
|
|
|
|
|
const subpath = process.env.SUB_PATH;
|
|
|
|
|
const fullUrl = `${host}${subpath ? subpath : '/'}`;
|
|
|
|
|
return `https://slack.com/oauth/v2/authorize?response_type=code&client_id=${clientId}&redirect_uri=${fullUrl}oauth2/authorize`;
|
2021-07-17 17:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-24 09:17:38 +00:00
|
|
|
async accessDetailsFrom(authCode: string): Promise<object> {
|
2021-09-21 13:48:28 +00:00
|
|
|
const accessTokenUrl = 'https://slack.com/api/oauth.v2.access';
|
2021-07-24 09:17:38 +00:00
|
|
|
const clientId = process.env.SLACK_CLIENT_ID;
|
|
|
|
|
const clientSecret = process.env.SLACK_CLIENT_SECRET;
|
2023-11-01 09:39:41 +00:00
|
|
|
const host = process.env.TOOLJET_HOST;
|
|
|
|
|
const subpath = process.env.SUB_PATH;
|
|
|
|
|
const fullUrl = `${host}${subpath ? subpath : '/'}`;
|
|
|
|
|
const redirectUri = `${fullUrl}oauth2/authorize`;
|
2021-07-24 09:17:38 +00:00
|
|
|
|
|
|
|
|
const body = `code=${authCode}&client_id=${clientId}&client_secret=${clientSecret}&redirect_uri=${redirectUri}`;
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
const response = await got(accessTokenUrl, {
|
2021-07-25 17:20:19 +00:00
|
|
|
method: 'post',
|
2021-07-24 09:17:38 +00:00
|
|
|
body,
|
2021-09-21 13:48:28 +00:00
|
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
2021-07-24 09:17:38 +00:00
|
|
|
});
|
2021-09-21 13:48:28 +00:00
|
|
|
|
2021-07-24 09:17:38 +00:00
|
|
|
const result = JSON.parse(response.body);
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
if (response.statusCode !== 200) {
|
2021-07-24 09:17:38 +00:00
|
|
|
throw Error('could not connect to Slack');
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
const authDetails = [];
|
2021-07-24 09:17:38 +00:00
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
if (result['access_token']) {
|
2021-07-24 09:17:38 +00:00
|
|
|
authDetails.push(['access_token', result['access_token']]);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
if (result['refresh_token']) {
|
2021-07-24 09:17:38 +00:00
|
|
|
authDetails.push(['refresh_token', result['refresh_token']]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return authDetails;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-17 07:08:17 +00:00
|
|
|
authHeader(token: string): Headers {
|
2021-09-21 13:48:28 +00:00
|
|
|
return { Authorization: `Bearer ${token}` };
|
2021-07-24 09:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-24 13:59:21 +00:00
|
|
|
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions, dataSourceId: string): Promise<QueryResult> {
|
2021-09-21 13:48:28 +00:00
|
|
|
let result = {};
|
2021-07-24 09:17:38 +00:00
|
|
|
let response = null;
|
|
|
|
|
const operation = queryOptions.operation;
|
2022-01-24 13:59:21 +00:00
|
|
|
const accessToken = sourceOptions.access_token;
|
2021-07-24 09:17:38 +00:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
switch (operation) {
|
|
|
|
|
case 'list_users':
|
2021-09-21 13:48:28 +00:00
|
|
|
response = await got('https://slack.com/api/users.list', {
|
|
|
|
|
method: 'get',
|
|
|
|
|
headers: this.authHeader(accessToken),
|
2021-07-24 09:17:38 +00:00
|
|
|
});
|
2021-09-21 13:48:28 +00:00
|
|
|
|
2021-07-24 09:17:38 +00:00
|
|
|
result = JSON.parse(response.body);
|
|
|
|
|
break;
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
case 'send_message': {
|
2023-02-07 06:59:37 +00:00
|
|
|
if (sourceOptions.access_type === 'chat:write') {
|
|
|
|
|
const body = {
|
|
|
|
|
channel: queryOptions.channel,
|
|
|
|
|
text: queryOptions.message,
|
|
|
|
|
as_user: queryOptions.sendAsUser,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
response = await got('https://slack.com/api/chat.postMessage', {
|
|
|
|
|
method: 'post',
|
|
|
|
|
json: body,
|
|
|
|
|
headers: this.authHeader(accessToken),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
result = JSON.parse(response.body);
|
|
|
|
|
} else {
|
|
|
|
|
result = {
|
|
|
|
|
ok: false,
|
|
|
|
|
error: 'You do not have the required permissions to perform this operation',
|
|
|
|
|
};
|
|
|
|
|
}
|
2021-09-21 13:48:28 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2023-01-27 10:56:31 +00:00
|
|
|
|
|
|
|
|
case 'list_messages':
|
|
|
|
|
response = await got('https://slack.com/api/conversations.history', {
|
|
|
|
|
method: 'post',
|
|
|
|
|
form: {
|
|
|
|
|
channel: queryOptions.channel,
|
|
|
|
|
limit: queryOptions.limit || 100,
|
|
|
|
|
cursor: queryOptions.cursor || '',
|
|
|
|
|
},
|
|
|
|
|
headers: this.authHeader(accessToken),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
result = JSON.parse(response.body);
|
|
|
|
|
break;
|
2021-07-24 09:17:38 +00:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw new QueryError('Query could not be completed', error.message, {});
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-17 17:54:14 +00:00
|
|
|
return {
|
|
|
|
|
status: 'ok',
|
2021-09-21 13:48:28 +00:00
|
|
|
data: result,
|
|
|
|
|
};
|
2021-07-17 17:54:14 +00:00
|
|
|
}
|
|
|
|
|
}
|