Merge pull request #330 from automatisch/feature/slack-new-message-to-channel-trigger
feat: Implement new message to a channel for slack w/o webhook
This commit is contained in:
@@ -5,11 +5,13 @@ import {
|
|||||||
IJSONObject,
|
IJSONObject,
|
||||||
} from '@automatisch/types';
|
} from '@automatisch/types';
|
||||||
import Authentication from './authentication';
|
import Authentication from './authentication';
|
||||||
|
import Triggers from './triggers';
|
||||||
import Actions from './actions';
|
import Actions from './actions';
|
||||||
import Data from './data';
|
import Data from './data';
|
||||||
|
|
||||||
export default class Slack implements IService {
|
export default class Slack implements IService {
|
||||||
authenticationClient: IAuthentication;
|
authenticationClient: IAuthentication;
|
||||||
|
triggers: Triggers;
|
||||||
actions: Actions;
|
actions: Actions;
|
||||||
data: Data;
|
data: Data;
|
||||||
|
|
||||||
@@ -20,6 +22,7 @@ export default class Slack implements IService {
|
|||||||
) {
|
) {
|
||||||
this.authenticationClient = new Authentication(appData, connectionData);
|
this.authenticationClient = new Authentication(appData, connectionData);
|
||||||
this.data = new Data(connectionData);
|
this.data = new Data(connectionData);
|
||||||
|
this.triggers = new Triggers(connectionData, parameters);
|
||||||
this.actions = new Actions(connectionData, parameters);
|
this.actions = new Actions(connectionData, parameters);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -97,6 +97,65 @@
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"triggers": [
|
||||||
|
{
|
||||||
|
"name": "New message posted to a channel",
|
||||||
|
"key": "newMessageToChannel",
|
||||||
|
"description": "Triggers when a new message is posted to a channel",
|
||||||
|
"substeps": [
|
||||||
|
{
|
||||||
|
"key": "chooseAccount",
|
||||||
|
"name": "Choose account"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"key": "chooseTrigger",
|
||||||
|
"name": "Set up a trigger",
|
||||||
|
"arguments": [
|
||||||
|
{
|
||||||
|
"label": "Channel",
|
||||||
|
"key": "channel",
|
||||||
|
"type": "dropdown",
|
||||||
|
"required": true,
|
||||||
|
"variables": false,
|
||||||
|
"source": {
|
||||||
|
"type": "query",
|
||||||
|
"name": "getData",
|
||||||
|
"arguments": [
|
||||||
|
{
|
||||||
|
"name": "key",
|
||||||
|
"value": "listChannels"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "Trigger for Bot Messages?",
|
||||||
|
"key": "triggerForBotMessages",
|
||||||
|
"type": "dropdown",
|
||||||
|
"description": "Should this flow trigger for bot messages?",
|
||||||
|
"required": true,
|
||||||
|
"value": true,
|
||||||
|
"variables": false,
|
||||||
|
"options": [
|
||||||
|
{
|
||||||
|
"label": "Yes",
|
||||||
|
"value": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "No",
|
||||||
|
"value": false
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"key": "testStep",
|
||||||
|
"name": "Test trigger"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"actions": [
|
"actions": [
|
||||||
{
|
{
|
||||||
"name": "Send a message to channel",
|
"name": "Send a message to channel",
|
||||||
|
13
packages/backend/src/apps/slack/triggers.ts
Normal file
13
packages/backend/src/apps/slack/triggers.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { IJSONObject } from '@automatisch/types';
|
||||||
|
import NewMessageToChannel from './triggers/new-message-to-channel';
|
||||||
|
|
||||||
|
export default class Triggers {
|
||||||
|
newMessageToChannel: NewMessageToChannel;
|
||||||
|
|
||||||
|
constructor(connectionData: IJSONObject, parameters: IJSONObject) {
|
||||||
|
this.newMessageToChannel = new NewMessageToChannel(
|
||||||
|
connectionData,
|
||||||
|
parameters
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,47 @@
|
|||||||
|
import { IJSONObject } from '@automatisch/types';
|
||||||
|
import axios, { AxiosInstance } from 'axios';
|
||||||
|
|
||||||
|
export default class NewMessageToChannel {
|
||||||
|
httpClient: AxiosInstance;
|
||||||
|
parameters: IJSONObject;
|
||||||
|
connectionData: IJSONObject;
|
||||||
|
BASE_URL = 'https://slack.com/api';
|
||||||
|
|
||||||
|
constructor(connectionData: IJSONObject, parameters: IJSONObject) {
|
||||||
|
this.httpClient = axios.create({ baseURL: this.BASE_URL });
|
||||||
|
this.connectionData = connectionData;
|
||||||
|
this.parameters = parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
async run() {
|
||||||
|
// TODO: Fix after webhook implementation.
|
||||||
|
}
|
||||||
|
|
||||||
|
async testRun() {
|
||||||
|
const headers = {
|
||||||
|
Authorization: `Bearer ${this.connectionData.accessToken}`,
|
||||||
|
};
|
||||||
|
|
||||||
|
const params = {
|
||||||
|
channel: this.parameters.channel,
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await this.httpClient.get('/conversations.history', {
|
||||||
|
headers,
|
||||||
|
params,
|
||||||
|
});
|
||||||
|
|
||||||
|
let lastMessage;
|
||||||
|
|
||||||
|
if (this.parameters.triggerForBotMessages) {
|
||||||
|
lastMessage = response.data.messages[0];
|
||||||
|
} else {
|
||||||
|
lastMessage = response.data.messages.find(
|
||||||
|
(message: IJSONObject) =>
|
||||||
|
!Object.prototype.hasOwnProperty.call(message, 'bot_id')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return [lastMessage];
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user