ToolJet/server/plugins/datasources/slack/index.ts

97 lines
2.9 KiB
TypeScript
Raw Normal View History

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> {
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}`;
const response = await got(accessTokenUrl, {
method: 'post',
2021-07-24 09:17:38 +00:00
body,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
2021-07-24 09:17:38 +00:00
});
2021-07-24 09:17:38 +00:00
const result = JSON.parse(response.body);
if (response.statusCode !== 200) {
2021-07-24 09:17:38 +00:00
throw Error('could not connect to Slack');
}
const authDetails = [];
2021-07-24 09:17:38 +00:00
if (result['access_token']) {
2021-07-24 09:17:38 +00:00
authDetails.push(['access_token', result['access_token']]);
}
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 {
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> {
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':
response = await got('https://slack.com/api/users.list', {
method: 'get',
headers: this.authHeader(accessToken),
2021-07-24 09:17:38 +00:00
});
2021-07-24 09:17:38 +00:00
result = JSON.parse(response.body);
break;
case 'send_message': {
2021-07-24 09:17:38 +00:00
const body = {
channel: queryOptions['channel'],
text: queryOptions['message'],
as_user: queryOptions['sendAsUser'],
};
2021-07-24 09:17:38 +00:00
response = await got('https://slack.com/api/chat.postMessage', {
method: 'post',
2021-07-24 09:17:38 +00:00
json: body,
headers: this.authHeader(accessToken),
2021-07-24 09:17:38 +00:00
});
2021-07-24 09:17:38 +00:00
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',
data: result,
};
2021-07-17 17:54:14 +00:00
}
}