Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions plugins/slack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## Usage

````typescript
const slackPlugin = new SlackPlugin({
webhookUrl: 'https://hooks.slack.com/services/SCRIBBLESCRIBBLESCRIBBLE',
})
const cdcPlugin = new ChangeDataCapturePlugin({
stub,
broadcastAllEvents: true,
events: [],
})

cdcPlugin.onEvent(({ action, schema, table, data }) => {
ctx.waitUntil(
slackPlugin.sendMessage({
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `${action} detected on ${table}`,
},
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: 'The following data was associated with this action:',
},
},
{
type: 'section',
block_id: 'section_1',
text: {
type: 'mrkdwn',
text: '```' + `${JSON.stringify(data)}` + '```',
},
},
],
})
)
})
````
43 changes: 43 additions & 0 deletions plugins/slack/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { StarbasePlugin } from '../../src/plugin'

export class SlackPlugin extends StarbasePlugin {
// Prefix route
prefix: string = '/slack'
// Webhook URL to call to Slack
webhookUrl?: string

constructor(opts?: { webhookUrl: string }) {
super('starbasedb:slack', {
requiresAuth: true,
})

this.webhookUrl = opts?.webhookUrl
}

public async sendMessage(message: { text?: string; blocks?: any[] }) {
if (!this.webhookUrl) {
throw new Error(`Slack webhook URL was not provided.`)
}

try {
const response = await fetch(this.webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(message),
})

if (!response.ok) {
throw new Error(
`Failed to send Slack message: ${response.statusText}`
)
}

return response
} catch (error) {
console.error('Error sending Slack message:', error)
throw error
}
}
}