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 { Github } from "@trigger.dev/github";
3
import { OpenAI } from "@trigger.dev/openai";
4
import { Slack } from "@trigger.dev/slack";
5
6
const github = new Github({
7
id: "github",
8
token: process.env.GITHUB_TOKEN!,
9
});
10
11
const openai = new OpenAI({
12
id: "openai",
13
apiKey: process.env.OPENAI_API_KEY!,
14
});
15
16
const slack = new Slack({ id: "slack" });
17
18
// This Job runs at 7am every day using a cron schedule expression.
19
client.defineJob({
20
id: "summarize-yesterday-commits-on-slack",
21
name: "Summarize yesterdays GitHub commits on Slack",
22
version: "1.0.0",
23
trigger: cronTrigger({
24
// Note that the time is in UTC.
25
// Converted to cron schedule expression using https://crontab.guru/
26
cron: "0 7 * * *",
27
}),
28
integrations: {
29
github,
30
openai,
31
slack,
32
},
33
run: async (payload, io, ctx) => {
34
// 1. Get the 'since' and 'until' timestamps
35
const since = payload.lastTimestamp?.toISOString();
36
const until = payload.ts?.toISOString();
37
38
// 2. Get yesterday's commits from GitHub
39
const owner = "<your-org-name>";
40
const repo = "<your-repo-name>";
41
42
const { data } = await io.github.runTask(
43
"get-yesterdays-commits",
44
async (client) => {
45
return client.rest.repos.listCommits({
46
owner,
47
repo,
48
since,
49
until,
50
});
51
},
52
{ name: "Get Yesterday's Commits" }
53
);
54
55
// 3. Turn the commit data into a shorter format for OpenAI:
56
const formattedCommits = await io.runTask("format-commits", async () => {
57
return data.map((commit) => {
58
return {
59
author: commit.commit.author?.name,
60
message: commit.commit.message,
61
time: commit.commit.author?.date,
62
link: commit.html_url,
63
};
64
});
65
});
66
67
// 4. Summarize the commits with OpenAI
68
const chatCompletion = await io.openai.createChatCompletion(
69
"chat-completion",
70
{
71
model: "gpt-3.5-turbo",
72
messages: [
73
{
74
role: "system",
75
content: `You are a expert programmer experienced in GitHub.
76
You are to concisely summarize the GitHub commits in one message.
77
Reply with a heading message -- 'GitHub Commits for ${repo} yesterday'
78
-- followed by a summary of the commits. Use bullet points in your summary
79
list and use appropriate spacing for maximum readability.`,
80
},
81
{
82
role: "user",
83
content: `Here are all of the GitHub commits from yesterday
84
(delimited by triple quotes below). Please summarize them like
85
"{user} {message} ({url})".\n\n"""${JSON.stringify(
86
formattedCommits,
87
null,
88
2
89
)}"""`,
90
},
91
],
92
}
93
);
94
95
// 5. Post to Slack
96
const response = await io.slack.postMessage("post message", {
97
channel: process.env.SLACK_CHANNEL_ID!,
98
text: chatCompletion.choices[0]?.message?.content || "No summary found",
99
});
100
},
101
});

Full-stack Slack example projects