# Upwork Job Monitoring System with MS Teams & AI Cover Letters

## Overview

This system automatically:
1. Monitors Upwork for new job postings matching your search criteria
2. Sends notifications to MS Teams when new jobs are found
3. Generates AI-powered cover letters using OpenAI GPT-4

## System Components

### 1. `setup.php`
Initial authorization script to get OAuth2 tokens from Upwork.
Run this ONCE to authorize the application.

### 2. `myapp.php`
Main monitoring script that:
- Loads OAuth2 tokens from `tokens.json`
- Queries Upwork GraphQL API for new jobs
- Automatically refreshes expired tokens
- Sends Teams notifications with job details
- Tracks processed jobs to avoid duplicates

### 3. `generate_draft.php`
Web interface for generating cover letters:
- Fetches job details from Upwork API
- Uses OpenAI GPT-4 to generate personalized proposals
- Beautiful UI with copy-to-clipboard functionality
- Direct link to apply on Upwork

### 4. `tokens.json`
Stores OAuth2 tokens (auto-managed):
- access_token
- refresh_token
- expires_in (unix timestamp)

## Setup Instructions

### Initial Setup (One Time)

```bash
cd /var/www/html/upwork/php-upwork-oauth2
php setup.php
```

Follow the prompts to authorize the application.

### Running the Monitor

```bash
php myapp.php
```

### Automated Monitoring (Cron)

Add to crontab for automatic execution:

```bash
crontab -e
```

Add this line to run every 30 minutes:
```
*/30 * * * * cd /var/www/html/upwork/php-upwork-oauth2 && /usr/bin/php myapp.php >> /var/log/upwork-jobs.log 2>&1
```

Or every hour:
```
0 * * * * cd /var/www/html/upwork/php-upwork-oauth2 && /usr/bin/php myapp.php >> /var/log/upwork-jobs.log 2>&1
```

## How It Works

### Workflow

1. **Monitor runs** → Queries Upwork API for jobs matching "zoho"
2. **New jobs found** → Compares with previously stored job IDs
3. **Teams notification sent** → Rich card with:
   - Job title
   - Skills required
   - Description preview
   - "View on Upwork" button
   - "Generate Cover Letter" button
4. **Click "Generate Cover Letter"** → Opens web page
5. **AI generates proposal** → Uses your custom prompt template
6. **Copy & apply** → One-click copy to clipboard

### Token Management

- Tokens expire every 24 hours
- System automatically refreshes tokens when needed
- Refreshed tokens saved back to `tokens.json`
- No manual intervention required

## Configuration

### Search Criteria

Edit `myapp.php` line 108-113 to change search terms:

```php
$variables = [
    'marketPlaceJobFilter' => [
        'searchExpression_eq' => 'zoho', // Change this
    ],
];
```

### Cover Letter Prompt

Edit `generate_draft.php` lines 10-43 to customize your proposal style.

### Teams Webhook

Change webhook URL in `myapp.php` line 27.

### OpenAI API Key

Change API key in `myapp.php` line 28 and `generate_draft.php` line 10.

## Features

### MS Teams Integration
- Rich adaptive cards with formatted job details (contract type, budget, client spend/rating, payment verification, location, average bid stats)
- Clickable action buttons
- Instant notifications
- Works on desktop and mobile

### AI Cover Letter Generation
- Uses GPT-4o-mini for cost-effective generation
- Custom prompt template matching your writing style
- Natural, human-sounding proposals
- Mentions your real experience, clients, and industries
- 150-200 word optimized length

### Token Auto-Refresh
- Automatic OAuth2 token refresh
- Persistent storage
- No downtime or manual intervention
- Works indefinitely

### Job Tracking
- Stores processed job IDs in `last_jobs.json`
- Prevents duplicate notifications
- Only notifies about truly new jobs

## Costs

### Upwork API
- Free

### MS Teams Webhook
- Free

### OpenAI API
- GPT-4o-mini: ~$0.002-0.005 per cover letter
- Estimated: $0.10-0.50/month for typical usage

## Troubleshooting

### No notifications received
1. Check Teams webhook URL is correct
2. Verify `php myapp.php` runs without errors
3. Check if new jobs actually exist

### Token errors
1. Delete `tokens.json`
2. Run `php setup.php` again
3. Re-authorize the application

### Cover letter generation fails
1. Check OpenAI API key is valid
2. Verify you have API credits
3. Check error message in browser

### Jobs not found
1. Verify your Upwork API credentials
2. Check search criteria in `myapp.php`
3. Ensure tokens are valid

## File Structure

```
/var/www/html/upwork/php-upwork-oauth2/
├── setup.php                   # Initial authorization
├── myapp.php                   # Main monitoring script
├── generate_draft.php          # Cover letter generator
├── tokens.json                 # OAuth tokens (auto-managed)
├── last_jobs.json             # Processed job IDs
├── composer.json              # Dependencies
└── vendor/                    # PHP packages
```

## Security Notes

- Keep `tokens.json` secure (contains access tokens)
- Keep OpenAI API key secure
- Keep Teams webhook URL private
- Consider using environment variables for production

## Support

For issues or questions, check:
- Upwork API Documentation: https://developers.upwork.com/
- OpenAI API Documentation: https://platform.openai.com/docs
- MS Teams Webhooks: https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/

