Back to jobs

Weekly user activity summary email

Trigger: cronTrigger

Sends a weekly summary email to users who have summariesEnabled = true, at 4pm every Friday, and then posts the total numbers to Slack.

Integrations:

/src/weeklyUserActivitySummary

Scheduled

Marketing

1
import { TriggerClient, cronTrigger } from "@trigger.dev/sdk";
2
import { SendGrid } from "@trigger.dev/sendgrid";
3
import { Slack } from "@trigger.dev/slack";
4
import { weeklySummaryDb } from "./mocks/db";
5
import { weeklySummaryEmail } from "./mocks/emails";
6
7
const client = new TriggerClient({ id: "jobs-showcase" });
8
9
const sendgrid = new SendGrid({
10
id: "sendgrid",
11
apiKey: process.env.SENDGRID_API_KEY!,
12
});
13
14
const slack = new Slack({ id: "slack" });
15
16
// This Job sends a weekly summary email to users who have
17
// summariesEnabled = true, and then posts the total numbers to Slack.
18
client.defineJob({
19
id: "weekly-user-activity-summary",
20
name: "Weekly user activity summary",
21
version: "1.0.0",
22
integrations: { sendgrid, slack },
23
trigger: cronTrigger({
24
// Send every Friday at 4pm
25
cron: "0 16 * * 5",
26
}),
27
run: async (payload, io, ctx) => {
28
const users = await weeklySummaryDb.getUsers();
29
30
let sentCount = 0;
31
let notSentCount = 0;
32
33
for (const user of users) {
34
if (user.summariesEnabled) {
35
await io.sendgrid.sendEmail(`Weekly summary for ${user.id}`, {
36
to: user.email,
37
// The 'from' email must be a verified domain in your SendGrid account.
38
39
subject: "Your weekly summary",
40
html: weeklySummaryEmail(user),
41
});
42
sentCount++;
43
} else {
44
notSentCount++;
45
}
46
}
47
48
await io.slack.postMessage("Notify team", {
49
text: `Weekly summary sent to ${sentCount} users and not sent to ${notSentCount} users`,
50
// This has to be a channel ID, not a channel name
51
channel: "YOUR_CHANNEL_ID",
52
});
53
},
54
});
55
56
// These lines can be removed if you don't want to use express
57
import { createExpressServer } from "@trigger.dev/express";
58
createExpressServer(client);