2021-07-17 17:54:14 +00:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2021-07-24 09:17:38 +00:00
|
|
|
import { QueryError } from 'src/modules/data_sources/query.error';
|
2021-07-17 17:54:14 +00:00
|
|
|
import { QueryResult } from 'src/modules/data_sources/query_result.type';
|
|
|
|
|
import { QueryService } from 'src/modules/data_sources/query_service.interface';
|
|
|
|
|
const got = require('got');
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export default class SlackQueryService implements QueryService {
|
|
|
|
|
authUrl(): string {
|
|
|
|
|
const clientId = process.env.SLACK_CLIENT_ID;
|
|
|
|
|
const tooljetHost = process.env.TOOLJET_HOST;
|
|
|
|
|
return `https://slack.com/oauth/v2/authorize?response_type=code&client_id=${clientId}&redirect_uri=${tooljetHost}/oauth2/authorize`;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
const tooljetHost = process.env.TOOLJET_HOST;
|
|
|
|
|
const redirectUri = `${tooljetHost}/oauth2/authorize`;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
authHeader(token: string): object {
|
2021-09-21 13:48:28 +00:00
|
|
|
return { Authorization: `Bearer ${token}` };
|
2021-07-24 09:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-17 17:54:14 +00:00
|
|
|
async run(sourceOptions: any, queryOptions: any, 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;
|
|
|
|
|
const accessToken = sourceOptions['access_token'];
|
|
|
|
|
|
|
|
|
|
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': {
|
2021-07-24 09:17:38 +00:00
|
|
|
const body = {
|
|
|
|
|
channel: queryOptions['channel'],
|
|
|
|
|
text: queryOptions['message'],
|
2021-09-21 13:48:28 +00:00
|
|
|
as_user: queryOptions['sendAsUser'],
|
|
|
|
|
};
|
2021-07-24 09:17:38 +00:00
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
response = await got('https://slack.com/api/chat.postMessage', {
|
|
|
|
|
method: 'post',
|
2021-07-24 09:17:38 +00:00
|
|
|
json: body,
|
2021-09-21 13:48:28 +00:00
|
|
|
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);
|
2021-09-21 13:48:28 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|