Skip to content

Instantly share code, notes, and snippets.

@urbanisierung
Created December 10, 2020 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urbanisierung/c0dad4df33d9554d036e84b20896f04f to your computer and use it in GitHub Desktop.
Save urbanisierung/c0dad4df33d9554d036e84b20896f04f to your computer and use it in GitHub Desktop.
import { IncomingWebhook } from '@slack/webhook'
import { ZeebeController } from '../zeebe.controller'
const SLACK_WEBHOOK_BASE = 'https://hooks.slack.com/services'
export class SlackWorkerController {
private webhook: IncomingWebhook | null = null
constructor(private zeebeController: ZeebeController) {}
public createWorker(taskType: string) {
this.zeebeController.getZeebeClient().createWorker({
taskType,
taskHandler: async (job: any, complete: any, worker: any) => {
const webhookid = job.customHeaders.webhookid
const message = job.customHeaders.message
const webhookurl = `${SLACK_WEBHOOK_BASE}/${webhookid}`
this.webhook = new IncomingWebhook(webhookurl)
try {
await this.send(message)
complete.success()
} catch (error) {
complete.failure('Failed to send slack message')
}
},
})
}
private async send(message: string) {
const slackMessage = {
text: `🚀 ${message} 🚀`,
mrkdwn: true,
attachments: [
{
title: `Greetings from Rest Zeebe!`,
},
],
}
if (this.webhook) {
await this.webhook.send(slackMessage)
} else {
throw new Error(`Failed to initialize Slack Webhook`)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment