diff --git a/packages/backend/src/apps/slack/actions.ts b/packages/backend/src/apps/slack/actions.ts index 4b650816..7e44ebc4 100644 --- a/packages/backend/src/apps/slack/actions.ts +++ b/packages/backend/src/apps/slack/actions.ts @@ -1,13 +1,12 @@ import SendMessageToChannel from './actions/send-message-to-channel'; -import { IJSONObject } from '@automatisch/types'; +import SlackClient from './client'; export default class Actions { + client: SlackClient; sendMessageToChannel: SendMessageToChannel; - constructor(connectionData: IJSONObject, parameters: IJSONObject) { - this.sendMessageToChannel = new SendMessageToChannel( - connectionData, - parameters - ); + constructor(client: SlackClient) { + this.client = client; + this.sendMessageToChannel = new SendMessageToChannel(client); } } diff --git a/packages/backend/src/apps/slack/actions/send-message-to-channel.ts b/packages/backend/src/apps/slack/actions/send-message-to-channel.ts index 8a9ef17d..e5f290fe 100644 --- a/packages/backend/src/apps/slack/actions/send-message-to-channel.ts +++ b/packages/backend/src/apps/slack/actions/send-message-to-channel.ts @@ -1,21 +1,18 @@ -import { WebClient } from '@slack/web-api'; -import { IJSONObject } from '@automatisch/types'; +import SlackClient from '../client'; export default class SendMessageToChannel { - client: WebClient; - parameters: IJSONObject; + client: SlackClient; - constructor(connectionData: IJSONObject, parameters: IJSONObject) { - this.client = new WebClient(connectionData.accessToken as string); - this.parameters = parameters; + constructor(client: SlackClient) { + this.client = client; } async run() { - const result = await this.client.chat.postMessage({ - channel: this.parameters.channel as string, - text: this.parameters.message as string, - }); + const channelId = this.client.step.parameters.channel as string; + const text = this.client.step.parameters.message as string; - return result; + const message = await this.client.postMessageToChannel.run(channelId, text); + + return message; } } diff --git a/packages/backend/src/apps/slack/client/endpoints/post-message-to-channel.ts b/packages/backend/src/apps/slack/client/endpoints/post-message-to-channel.ts new file mode 100644 index 00000000..a714c209 --- /dev/null +++ b/packages/backend/src/apps/slack/client/endpoints/post-message-to-channel.ts @@ -0,0 +1,34 @@ +import SlackClient from '../index'; + +export default class PostMessageToChannel { + client: SlackClient; + + constructor(client: SlackClient) { + this.client = client; + } + + async run(channelId: string, text: string) { + const headers = { + Authorization: `Bearer ${this.client.connection.formattedData.accessToken}`, + }; + + const params = { + channel: channelId, + text, + }; + + const response = await this.client.httpClient.post( + '/chat.postMessage', + params, + { headers } + ); + + if (response.data.ok === 'false') { + throw new Error( + `Error occured while posting a message to channel: ${response.data.error}` + ); + } + + return response.data.message; + } +} diff --git a/packages/backend/src/apps/slack/client/index.ts b/packages/backend/src/apps/slack/client/index.ts index c5d741ca..b3e9e17d 100644 --- a/packages/backend/src/apps/slack/client/index.ts +++ b/packages/backend/src/apps/slack/client/index.ts @@ -1,6 +1,7 @@ import { IFlow, IStep, IConnection } from '@automatisch/types'; import HttpClient from '../../../helpers/http-client'; import VerifyAccessToken from './endpoints/verify-access-token'; +import PostMessageToChannel from './endpoints/post-message-to-channel'; export default class SlackClient { flow: IFlow; @@ -9,6 +10,7 @@ export default class SlackClient { httpClient: HttpClient; verifyAccessToken: VerifyAccessToken; + postMessageToChannel: PostMessageToChannel; static baseUrl = 'https://slack.com/api'; @@ -19,5 +21,6 @@ export default class SlackClient { this.httpClient = new HttpClient({ baseURL: SlackClient.baseUrl }); this.verifyAccessToken = new VerifyAccessToken(this); + this.postMessageToChannel = new PostMessageToChannel(this); } } diff --git a/packages/backend/src/apps/slack/index.ts b/packages/backend/src/apps/slack/index.ts index 21289148..6736e09a 100644 --- a/packages/backend/src/apps/slack/index.ts +++ b/packages/backend/src/apps/slack/index.ts @@ -23,9 +23,8 @@ export default class Slack implements IService { this.client = new SlackClient(connection, flow, step); this.authenticationClient = new Authentication(this.client); - // this.triggers = new Triggers(this.client); - // this.actions = new Actions(this.client); + this.actions = new Actions(this.client); this.data = new Data(this.client); } }