Send Slack events to Amazon EventBridge.
Create a new Slack app in your workspace, install it and save its signing secret in a AWS Secrets Manager secret.
This can be done with the AWS CLI:
aws secretsmanager create-secret --name my-slack-app --secret-string <signing secret>
Define a SlackEvents
in your Stack
and deploy it:
import { Stack, StackProps } from 'aws-cdk-lib';
import * as cloudstructs from 'cloudstructs';
import { Construct } from 'constructs';
export class MyStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
new cloudstructs.SlackEvents(this, 'SlackEvents', {
signingSecret: cdk.SecretValue.secretsManager('my-slack-app'),
});
}
}
Look for the API endoint in your stack outputs and use it to enable event subscriptions in your Slack app. At this point you can also finish configuring the scopes of your Slack app and add it to the channels where you want to listen for events.
You can now intercept Slack events using a events.Rule
:
const fileSharedRule = new events.Rule(this, 'SlackEventsRule', {
eventPattern: {
detail: {
event: {
type: ['file_shared'],
},
},
resources: ['<your app id>'],
source: ['slack'],
},
});
fileSharedRule.addTarget(new targets.LambdaFunction(myLambda, {
event: events.RuleTargetInput.fromEventPath('$.detail.event'),
}));
By default events are sent to your default event bus, which receives events emitted by AWS services.
Set the customEventBus
to true
to create and send events to a
custom event bus
const slackEvents = new cloudstructs.SlackEvents(this, 'SlackEvents', {
signingSecret: cdk.SecretValue.secretsManager('my-slack-app'),
customEventBus: true,
});
const fileSharedRule = new events.Rule(this, 'SlackEventsRule', {
eventBus: slackEvents.eventBus,
eventPattern: {
detail: {
event: {
type: ['file_shared'],
},
},
resources: ['<your app id>'],
source: ['slack'],
},
});