|
| 1 | +### Project Setup |
| 2 | + |
| 3 | +This example demonstrates automated web monitoring using Trigger.dev's job scheduling and Anchor Browser's AI-powered browser automation tools. The project runs daily at 5pm ET to find the cheapest Broadway tickets available for same-day shows. |
| 4 | + |
| 5 | +##### How it works: |
| 6 | + |
| 7 | +1. Trigger.dev schedules and executes the monitoring job |
| 8 | +2. Anchor Browser spins up a remote browser session with an AI agent |
| 9 | +3. The AI agent uses computer vision and natural language processing to analyze the [TDF website](https://www.tdf.org/discount-ticket-programs/tkts-by-tdf/tkts-live/) |
| 10 | +4. AI agent returns the lowest-priced show with specific details: name, price, and showtime |
| 11 | + |
| 12 | + |
| 13 | +#### Prerequisites |
| 14 | + |
| 15 | +Before we dive into the code, you'll need: |
| 16 | +- Node.js (version 16 or higher) |
| 17 | +- A Trigger.dev account - Sign up at https://trigger.dev for their free tier |
| 18 | +- Anchor Browser API access - Get your API key from the [Anchor Browser dashboard](https://anchorbrowser.io) |
| 19 | + |
| 20 | +#### Dependencies installation |
| 21 | + |
| 22 | +Initialize a new Node.js project and install the required packages from Anchor Browser and Trigger.dev: |
| 23 | + |
| 24 | +``` |
| 25 | +npm init -y |
| 26 | +npm install anchorbrowser |
| 27 | +npx trigger.dev@latest init -p <your-project-id> |
| 28 | +``` |
| 29 | + |
| 30 | +#### Environment configuration |
| 31 | + |
| 32 | +Create a .env file in your project root with the following variables: |
| 33 | + |
| 34 | +``` |
| 35 | +# Trigger.dev Configuration |
| 36 | +TRIGGER_API_KEY=tr_dev_your_trigger_api_key_here |
| 37 | +
|
| 38 | +# Anchor Browser Configuration |
| 39 | +ANCHOR_BROWSER_API_KEY=sk-your_anchor_browser_api_key_here |
| 40 | +``` |
| 41 | + |
| 42 | +Make sure to add .env to your .gitignore file to keep your API keys secure: |
| 43 | + |
| 44 | +``` |
| 45 | +echo ".env" >> .gitignore |
| 46 | +``` |
| 47 | + |
| 48 | +Since Anchor Browser uses browser automation libraries under the hood, we need to configure Trigger.dev to handle these dependencies properly by excluding them from the build bundle. |
| 49 | + |
| 50 | +Configure Trigger.dev's trigger.config.ts for browser automation dependencies: |
| 51 | + |
| 52 | +``` |
| 53 | +import { defineConfig } from "@trigger.dev/sdk/v3"; |
| 54 | +
|
| 55 | +export default defineConfig({ |
| 56 | + project: "proj_your_project_id_here", // Get from Trigger.dev dashboard |
| 57 | + maxDuration: 3600, // 1 hour - plenty of time for web automation |
| 58 | + dirs: ["./src/trigger"], |
| 59 | + build: { |
| 60 | + external: [ |
| 61 | + "playwright-core", |
| 62 | + "playwright", |
| 63 | + "chromium-bidi" |
| 64 | + ] |
| 65 | + } |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +### Core Monitoring Job |
| 70 | + |
| 71 | +Create a new file within your project subfolder for trigger.dev functions, [src/trigger/broadway-monitor.ts](src/trigger/broadway-monitor.ts). Below is our Broadway ticket monitor task that runs daily at 5pm ET: |
| 72 | + |
| 73 | + |
| 74 | +``` |
| 75 | +import { schedules } from "@trigger.dev/sdk"; |
| 76 | +import Anchorbrowser from 'anchorbrowser'; |
| 77 | +
|
| 78 | +export const broadwayMonitor = schedules.task({ |
| 79 | + id: "broadway-ticket-monitor", |
| 80 | + cron: "0 21 * * *", |
| 81 | + run: async (payload, { ctx }) => { |
| 82 | + const client = new Anchorbrowser({ |
| 83 | + apiKey: process.env.ANCHOR_BROWSER_API_KEY!, |
| 84 | + }); |
| 85 | +
|
| 86 | + let session; |
| 87 | + try { |
| 88 | + // Create explicit session to get live view URL |
| 89 | + session = await client.sessions.create(); |
| 90 | + console.log(`Session ID: ${session.data.id}`); |
| 91 | + console.log(`Live View URL: https://live.anchorbrowser.io?sessionId=${session.data.id}`); |
| 92 | +
|
| 93 | + const response = await client.tools.performWebTask({ |
| 94 | + sessionId: session.data.id, |
| 95 | + url: "https://www.tdf.org/discount-ticket-programs/tkts-by-tdf/tkts-live/", |
| 96 | + prompt: `Look for the "Broadway Shows" section on this page. Find the show with the absolute lowest starting price available right now and return the show name, current lowest price, and show time. Be very specific about the current price you see. Format as: Show: [name], Price: [exact current price], Time: [time]` |
| 97 | + }); |
| 98 | +
|
| 99 | + console.log("Raw response:", response); |
| 100 | + |
| 101 | + const result = response.data.result?.result || response.data.result || response.data; |
| 102 | + |
| 103 | + if (result && typeof result === 'string' && result.includes('Show:')) { |
| 104 | + console.log(`🎭 Best Broadway Deal Found!`); |
| 105 | + console.log(result); |
| 106 | +
|
| 107 | + return { |
| 108 | + success: true, |
| 109 | + bestDeal: result, |
| 110 | + liveViewUrl: `https://live.anchorbrowser.io?sessionId=${session.data.id}` |
| 111 | + }; |
| 112 | + } else { |
| 113 | + console.log("No Broadway deals found today"); |
| 114 | + return { success: true, message: "No deals found" }; |
| 115 | + } |
| 116 | +
|
| 117 | + } finally { |
| 118 | + if (session?.data?.id) { |
| 119 | + try { |
| 120 | + await client.sessions.delete(session.data.id); |
| 121 | + } catch (cleanupError) { |
| 122 | + console.warn("Failed to cleanup session:", cleanupError); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + }, |
| 127 | +}); |
| 128 | +``` |
| 129 | + |
| 130 | +Run the Trigger.dev development server from your project root directory to register your new task: |
| 131 | + |
| 132 | +``` |
| 133 | +npx trigger.dev@latest dev |
| 134 | +``` |
| 135 | + |
| 136 | +### Learn more |
| 137 | +- [Trigger.dev docs](https://trigger.dev/docs) - learn about Trigger.dev |
| 138 | +- [Anchor Browser docs](https://docs.anchorbrowser.io/introduction) - learn about Anchor Browser |
0 commit comments