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 { 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);
}
}

View File

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

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

View File

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