import { defineDurable } from "@zapier/zapier-durable"; import { createZapierSdk } from "@zapier/zapier-sdk"; import { z } from "zod"; import { AI_AUTHENTICATION_ID, AI_MODEL_ID, BOT_NAME, CAREERS_INSTRUCTIONS, CAREERS_URL, COMPANY_NAME, JUDGE_INSTRUCTIONS, LOOKBACK_DAYS, SEARCH_INSTRUCTIONS, SLACK_CHANNEL, } from "./config.ts"; const sdk = createZapierSdk(); // The daily schedule trigger sends nothing the zap reads; the schema only checks that the input parses. const InputSchema = z.object({}).loose(); // A real open role on the careers page. const RoleSchema = z.object({ title: z.string(), team: z.string().default(""), location: z.string().default(""), }); // A job post the search found, before it's judged. const FoundSchema = z.object({ title: z.string(), site: z.string(), url: z.string(), posted: z.string().default("unknown"), details: z.string().default(""), }); // A judged post. const PostSchema = z.object({ title: z.string(), site: z.string(), url: z.string(), verdict: z.string(), reason: z.string(), }); type Post = z.infer; const TOOLS = { web_search: { type: "builtin", id: "tool_web_search", builtin_tool_name: "web_search", }, web_scrape: { type: "builtin", id: "tool_web_scrape", builtin_tool_name: "web_scrape", }, fail: { type: "builtin", id: "tool_fail", builtin_tool_name: "fail" }, }; const field = (name: string, isRequired = true) => ({ name, type: "text", isRequired, }); // Runs one AI by Zapier step that returns a list, and checks each item against the schema. async function askForList( schema: S, instructions: string, inputFields: Record, fields: string[], tools: (keyof typeof TOOLS)[], optional: string[] = [], ): Promise[]> { const result = await sdk.runAction({ appKey: "ai", actionType: "write", actionKey: "get_completion", // Searching the web and opening pages can run past the SDK's 3-minute default. timeoutSeconds: 600, inputs: { model_id: AI_MODEL_ID, authentication_id: AI_AUTHENTICATION_ID, instructions, inputFields, includeWorkflowData: false, isOutputArray: true, outputFields: fields.map((name) => field(name, !optional.includes(name))), ...(tools.length ? { tools: tools.map((t) => TOOLS[t]) } : {}), }, }); return z .object({ result: z.object({ items: z.array(schema).default([]) }) }) .parse(result.data[0]).result.items; } function alertText(posts: Post[]): string { const lines = posts.map( (p) => `• <${p.url}|${p.title.replace(/[<>|]/g, "")}> on ${p.site}: ${p.reason}`, ); const headline = posts.length === 1 ? `1 job post uses ${COMPANY_NAME}'s name and doesn't match an open role` : `${posts.length} job posts use ${COMPANY_NAME}'s name and don't match an open role`; return [ `:rotating_light: *${headline}*`, ...lines, `Check each one against <${CAREERS_URL}|the careers page>, then report it to the site it's on.`, ].join("\n"); } const workflow = defineDurable( "alert-slack-to-fake-job-posts-using-your-company-name", async (ctx, rawInput) => { InputSchema.parse( typeof rawInput === "string" ? JSON.parse(rawInput) : rawInput, ); const today = await ctx.step("set-date", async () => new Date().toDateString(), ); const roles = await ctx.step("read-careers-page", async () => askForList( RoleSchema, CAREERS_INSTRUCTIONS, { company: COMPANY_NAME, careers_page: CAREERS_URL }, ["title", "team", "location"], ["web_scrape", "fail"], ["team", "location"], ), ); const found = await ctx.step("search-for-job-posts", async () => askForList( FoundSchema, SEARCH_INSTRUCTIONS, { company: COMPANY_NAME, careers_page: CAREERS_URL, lookback: `the last ${LOOKBACK_DAYS} days, up to ${today}`, }, ["title", "site", "url", "posted", "details"], ["web_search", "web_scrape"], ), ); if (found.length === 0) { return { roles: roles.length, checked: 0, flagged: 0, found: [] }; } const judged = await ctx.step("judge-posts", async () => askForList( PostSchema, JUDGE_INSTRUCTIONS, { company: COMPANY_NAME, lookback: `the last ${LOOKBACK_DAYS} days, up to ${today}`, open_roles: JSON.stringify(roles), posts: JSON.stringify(found), }, ["title", "site", "url", "verdict", "reason"], [], ), ); const posts = judged.filter( (p) => p.verdict.trim().toLowerCase() === "suspicious", ); if (posts.length === 0) { return { roles: roles.length, checked: judged.length, flagged: 0, found: judged, }; } const posted = await ctx.step("post-alert", async () => sdk.runAction({ appKey: "slack", actionType: "write", actionKey: "channel_message", connection: "slack", inputs: { channel: SLACK_CHANNEL, text: alertText(posts), as_bot: "yes", username: BOT_NAME, unfurl: "no", add_edit_link: "no", }, }), ); return { roles: roles.length, checked: judged.length, flagged: posts.length, found: judged, posted: posted.data[0] ?? null, }; }, ); export default workflow;