Back to APIs

Post messages to Slack when events occur.

Using our official Slack integration

Create any tasks possible with the Slack API.

Use io.runTask() and the official SDK or fetch.

Slack integration docs

Example code using Slack

Below are some working code examples of how you can use Slack with Trigger.dev. These samples are open source and maintained by the community, you can copy and paste them into your own projects.

1
import { TriggerClient, cronTrigger } from "@trigger.dev/sdk";
2
import { Linear } from "@trigger.dev/linear";
3
import { Slack } from "@trigger.dev/slack";
4
import { get } from "http";
5
6
const linear = new Linear({
7
id: "linear",
8
apiKey: process.env.LINEAR_API_KEY!,
9
});
10
11
const slack = new Slack({ id: "slack" });
12
13
client.defineJob({
14
id: "linear-issues-daily-slack-alert",
15
name: "Daily Slack alert for Linear issues",
16
version: "1.0.0",
17
integrations: {
18
linear,
19
slack,
20
},
21
trigger: cronTrigger({
22
// Note that the time is in UTC.
23
// Converted to cron schedule expression using https://crontab.guru/
24
cron: "0 9 * * 1,2,3,4,5",
25
}),
26
27
run: async (payload, io, ctx) => {
28
const inProgressIssues = await io.linear.issues("get-in-progress-issues", {
29
// Only get the first 20 issues
30
first: 20,
31
filter: {
32
team: {
33
id: {
34
// To get your Team id from within Linear, hit CMD+K and "Copy model UUID"
35
eq: "<your-team-uuid>",
36
},
37
},
38
assignee: {
39
email: {
40
eq: "<assignee-email-address>",
41
},
42
},
43
state: {
44
name: {
45
eq: "In Progress",
46
},
47
},
48
},
49
});
50
51
await io.slack.postMessage("post message", {
52
channel: process.env.SLACK_CHANNEL_ID!,
53
// Include text for notifications and blocks to get a rich Slack message in the channel
54
text: `You have ${inProgressIssues.nodes.length} 'In Progress' issues in Linear!`,
55
// Create rich Slack messages with the Block Kit builder https://app.slack.com/block-kit-builder/
56
blocks: inProgressIssues.nodes.flatMap((issue) => [
57
{
58
type: "section",
59
text: {
60
type: "mrkdwn",
61
text: `⏳ *${issue.title}*`,
62
},
63
accessory: {
64
type: "button",
65
text: {
66
type: "plain_text",
67
text: "View issue",
68
emoji: true,
69
},
70
value: "click_me_123",
71
url: issue.url,
72
action_id: "button-action",
73
},
74
},
75
{
76
type: "divider",
77
},
78
]),
79
});
80
},
81
});

Full-stack Slack example projects