Back to jobs
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.
/src/weeklyUserActivitySummary
Scheduled
Marketing
1import { TriggerClient, cronTrigger } from "@trigger.dev/sdk";2import { SendGrid } from "@trigger.dev/sendgrid";3import { Slack } from "@trigger.dev/slack";4import { weeklySummaryDb } from "./mocks/db";5import { weeklySummaryEmail } from "./mocks/emails";67const client = new TriggerClient({ id: "jobs-showcase" });89const sendgrid = new SendGrid({10id: "sendgrid",11apiKey: process.env.SENDGRID_API_KEY!,12});1314const slack = new Slack({ id: "slack" });1516// This Job sends a weekly summary email to users who have17// summariesEnabled = true, and then posts the total numbers to Slack.18client.defineJob({19id: "weekly-user-activity-summary",20name: "Weekly user activity summary",21version: "1.0.0",22integrations: { sendgrid, slack },23trigger: cronTrigger({24// Send every Friday at 4pm25cron: "0 16 * * 5",26}),27run: async (payload, io, ctx) => {28const users = await weeklySummaryDb.getUsers();2930let sentCount = 0;31let notSentCount = 0;3233for (const user of users) {34if (user.summariesEnabled) {35await io.sendgrid.sendEmail(`Weekly summary for ${user.id}`, {36to: user.email,37// The 'from' email must be a verified domain in your SendGrid account.3839subject: "Your weekly summary",40html: weeklySummaryEmail(user),41});42sentCount++;43} else {44notSentCount++;45}46}4748await io.slack.postMessage("Notify team", {49text: `Weekly summary sent to ${sentCount} users and not sent to ${notSentCount} users`,50// This has to be a channel ID, not a channel name51channel: "YOUR_CHANNEL_ID",52});53},54});5556// These lines can be removed if you don't want to use express57import { createExpressServer } from "@trigger.dev/express";58createExpressServer(client);