Skip to content

Commit

Permalink
Merge pull request #236 from madhav-relish/unsubscribe-automation
Browse files Browse the repository at this point in the history
#230 : Unsubscribe automation
  • Loading branch information
elie222 authored Oct 18, 2024
2 parents 7503462 + 9523adf commit d33198f
Show file tree
Hide file tree
Showing 9 changed files with 1,280 additions and 33 deletions.
4 changes: 4 additions & 0 deletions apps/unsubscriber/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
GOOGLE_GENERATIVE_AI_API_KEY=
# If required, set CORS_ORIGIN to allow requests from your frontend
CORS_ORIGIN="http://localhost:3000"
NODE_ENV="development"
95 changes: 95 additions & 0 deletions apps/unsubscriber/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Unsubscribe Automation Service

This service provides an automated solution for unsubscribing from email newsletters. It exposes an API that can be used to handle unsubscribe requests programmatically. The service uses AI to analyze unsubscribe pages and performs automated actions to complete the unsubscribe process.

## Features

- Exposes a RESTful API for handling unsubscribe requests
- AI-powered analysis of unsubscribe pages
- Automated web interactions using Playwright
- Support for both OpenAI and Google AI models
- CORS support for easy integration with frontend applications

## Prerequisites

- Node.js (v14 or later)
- pnpm package manager
- A Google AI API key

## Installation

1. Navigate to the project directory:

```
cd apps/unsubscribe-automation
```

2. Install dependencies:

```
pnpm install
```

3. Set up environment variables:
Create a `.env` file in the root directory with the following content:

```
GOOGLE_GENERATIVE_AI_API_KEY=your_google_ai_api_key_here
CORS_ORIGIN=http://localhost:3000
```

Replace the API keys with your actual keys, and adjust the CORS_ORIGIN if needed.
You can get a Google AI API Key here: https://aistudio.google.com/app/apikey

4. Install Playwright and its dependencies:

```bash
pnpm exec playwright install
```

This command will install Playwright and its necessary browser binaries.

## Running the Service

1. Start the server:
```
pnpm start
```
The server will start on http://localhost:5000 by default.

## Usage

To use the unsubscribe service, send a POST request to the `/unsubscribe` endpoint:

```bash
curl -X POST http://localhost:5000/unsubscribe \
-H "Content-Type: application/json" \
-d '{
"url": "https://example.com/unsubscribe"
}'
```

- Replace `https://example.com/unsubscribe` with the actual unsubscribe URL.

## API Endpoints

- `GET /`: Health check endpoint
- `POST /unsubscribe`: Trigger the unsubscribe process

## Development

- To run the service in development mode with hot reloading:

```
pnpm run dev
```

- To build the TypeScript files:
```
pnpm run build
```

## Troubleshooting

- If you encounter any issues with Playwright, ensure that you have the necessary system dependencies installed. Refer to the [Playwright installation guide](https://playwright.dev/docs/intro#installation) for more information.
- Check the console output for any error messages or logs that might indicate the cause of any issues.
32 changes: 32 additions & 0 deletions apps/unsubscriber/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "inbox-zero-unsubscriber",
"version": "1.0.0",
"private": true,
"type": "module",
"main": "index.js",
"scripts": {
"start": "tsx --tsconfig tsconfig.json src/server.ts",
"dev": "tsx watch --tsconfig tsconfig.json src/server.ts",
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"@types/dotenv": "^8.2.0",
"@types/node": "22.5.4",
"playwright": "^1.47.2",
"tsx": "^4.19.1",
"typescript": "^5.6.2"
},
"dependencies": {
"@ai-sdk/amazon-bedrock": "^0.0.29",
"@ai-sdk/anthropic": "^0.0.51",
"@ai-sdk/google": "^0.0.51",
"@ai-sdk/openai": "^0.0.59",
"@fastify/cors": "^10.0.1",
"@t3-oss/env-core": "^0.11.1",
"ai": "^3.3.36",
"dotenv": "^16.4.5",
"fastify": "^5.0.0",
"zod": "^3.23.8"
}
}
33 changes: 33 additions & 0 deletions apps/unsubscriber/src/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createEnv } from "@t3-oss/env-core";
import { z } from "zod";
import "dotenv/config";

export const env = createEnv({
server: {
NODE_ENV: z.enum(["development", "production", "test"]),
PORT: z.number().default(5000),
GOOGLE_GENERATIVE_AI_API_KEY: z.string().min(1),
CORS_ORIGIN: z.string().optional(),
},

/**
* What object holds the environment variables at runtime. This is usually
* `process.env` or `import.meta.env`.
*/
runtimeEnv: process.env,

/**
* By default, this library will feed the environment variables directly to
* the Zod validator.
*
* This means that if you have an empty string for a value that is supposed
* to be a number (e.g. `PORT=` in a ".env" file), Zod will incorrectly flag
* it as a type mismatch violation. Additionally, if you have an empty string
* for a value that is supposed to be a string with a default value (e.g.
* `DOMAIN=` in an ".env" file), the default value will never be applied.
*
* In order to solve these issues, we recommend that all new projects
* explicitly specify this option as true.
*/
emptyStringAsUndefined: true,
});
19 changes: 19 additions & 0 deletions apps/unsubscriber/src/llm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { google } from "@ai-sdk/google";
import { openai } from "@ai-sdk/openai";
import { anthropic } from "@ai-sdk/anthropic";
import { bedrock } from "@ai-sdk/amazon-bedrock";

type LLMProvider = "google" | "openai" | "anthropic" | "bedrock";

export function getModel(provider: LLMProvider) {
switch (provider) {
case "google":
return google("gemini-1.5-flash");
case "openai":
return openai("gpt-4o-mini");
case "anthropic":
return anthropic("claude-3-5-sonnet-20240620");
case "bedrock":
return bedrock("claude-3-5-sonnet-20240620");
}
}
Loading

1 comment on commit d33198f

@vercel
Copy link

@vercel vercel bot commented on d33198f Oct 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.