Merge pull request #477 from automatisch/feature/slack-send-a-message-to-channel

refactor: Adjust send a message to channel to use slack client
This commit is contained in:
Ömer Faruk Aydın
2022-09-01 23:24:15 +03:00
committed by GitHub
5 changed files with 52 additions and 20 deletions

View File

@@ -1,13 +1,12 @@
import SendMessageToChannel from './actions/send-message-to-channel'; import SendMessageToChannel from './actions/send-message-to-channel';
import { IJSONObject } from '@automatisch/types'; import SlackClient from './client';
export default class Actions { export default class Actions {
client: SlackClient;
sendMessageToChannel: SendMessageToChannel; sendMessageToChannel: SendMessageToChannel;
constructor(connectionData: IJSONObject, parameters: IJSONObject) { constructor(client: SlackClient) {
this.sendMessageToChannel = new SendMessageToChannel( this.client = client;
connectionData, this.sendMessageToChannel = new SendMessageToChannel(client);
parameters
);
} }
} }

View File

@@ -1,21 +1,18 @@
import { WebClient } from '@slack/web-api'; import SlackClient from '../client';
import { IJSONObject } from '@automatisch/types';
export default class SendMessageToChannel { export default class SendMessageToChannel {
client: WebClient; client: SlackClient;
parameters: IJSONObject;
constructor(connectionData: IJSONObject, parameters: IJSONObject) { constructor(client: SlackClient) {
this.client = new WebClient(connectionData.accessToken as string); this.client = client;
this.parameters = parameters;
} }
async run() { async run() {
const result = await this.client.chat.postMessage({ const channelId = this.client.step.parameters.channel as string;
channel: this.parameters.channel as string, const text = this.client.step.parameters.message as string;
text: this.parameters.message as string,
});
return result; const message = await this.client.postMessageToChannel.run(channelId, text);
return message;
} }
} }

View File

@@ -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;
}
}

View File

@@ -1,6 +1,7 @@
import { IFlow, IStep, IConnection } from '@automatisch/types'; import { IFlow, IStep, IConnection } from '@automatisch/types';
import HttpClient from '../../../helpers/http-client'; import HttpClient from '../../../helpers/http-client';
import VerifyAccessToken from './endpoints/verify-access-token'; import VerifyAccessToken from './endpoints/verify-access-token';
import PostMessageToChannel from './endpoints/post-message-to-channel';
export default class SlackClient { export default class SlackClient {
flow: IFlow; flow: IFlow;
@@ -9,6 +10,7 @@ export default class SlackClient {
httpClient: HttpClient; httpClient: HttpClient;
verifyAccessToken: VerifyAccessToken; verifyAccessToken: VerifyAccessToken;
postMessageToChannel: PostMessageToChannel;
static baseUrl = 'https://slack.com/api'; static baseUrl = 'https://slack.com/api';
@@ -19,5 +21,6 @@ export default class SlackClient {
this.httpClient = new HttpClient({ baseURL: SlackClient.baseUrl }); this.httpClient = new HttpClient({ baseURL: SlackClient.baseUrl });
this.verifyAccessToken = new VerifyAccessToken(this); this.verifyAccessToken = new VerifyAccessToken(this);
this.postMessageToChannel = new PostMessageToChannel(this);
} }
} }

View File

@@ -23,9 +23,8 @@ export default class Slack implements IService {
this.client = new SlackClient(connection, flow, step); this.client = new SlackClient(connection, flow, step);
this.authenticationClient = new Authentication(this.client); this.authenticationClient = new Authentication(this.client);
// this.triggers = new Triggers(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); this.data = new Data(this.client);
} }
} }