#!/bin/bash
# Real-time LinkedIn Lead Discovery and Messaging Test Script
# NOTE: This requires a Chrome browser with CDP running locally
# Vercel serverless functions may not maintain browser connections between requests
BASE_URL="https://linkedin-mcp-mocha.vercel.app"
# For local testing: BASE_URL="http://localhost:3001"
# Set these environment variables before running:
# export API_KEY="your-api-key"
# export CDP_URL="http://localhost:9222" # Chrome with --remote-debugging-port=9222
# export LI_AT_COOKIE="your-li-at-cookie"
if [ -z "$API_KEY" ]; then
echo "❌ ERROR: API_KEY not set"
echo "Generate one: curl -X POST \"$BASE_URL/api/generate-key\" -H \"Content-Type: application/json\" -d '{\"tier\": \"starter\"}'"
exit 1
fi
if [ -z "$CDP_URL" ]; then
echo "❌ ERROR: CDP_URL not set"
echo "Start Chrome with: chrome --remote-debugging-port=9222"
exit 1
fi
if [ -z "$LI_AT_COOKIE" ]; then
echo "❌ ERROR: LI_AT_COOKIE not set"
echo "Get it from Chrome DevTools → Application → Cookies → li_at"
exit 1
fi
echo "======================================"
echo "🚀 LinkedIn Lead Discovery Test"
echo "======================================"
echo "API: $BASE_URL"
echo "CDP: $CDP_URL"
echo ""
# Step 1: Connect Browser
echo "📡 Step 1: Connecting to Chrome browser..."
CONNECT_RESULT=$(curl -s -X POST "$BASE_URL/api/browser/connect" \
-H "Content-Type: application/json" \
-d "{\"cdp_url\": \"$CDP_URL\"}")
echo "$CONNECT_RESULT" | python -m json.tool 2>/dev/null || echo "$CONNECT_RESULT"
echo ""
if echo "$CONNECT_RESULT" | grep -q '"success":false'; then
echo "❌ Browser connection failed. Is Chrome running with CDP?"
exit 1
fi
# Step 2: Setup LinkedIn Session
echo "🔐 Step 2: Setting up LinkedIn session..."
SESSION_RESULT=$(curl -s -X POST "$BASE_URL/api/session/setup" \
-H "Content-Type: application/json" \
-d "{\"api_key\": \"$API_KEY\", \"li_at_cookie\": \"$LI_AT_COOKIE\"}")
echo "$SESSION_RESULT" | python -m json.tool 2>/dev/null || echo "$SESSION_RESULT"
echo ""
if echo "$SESSION_RESULT" | grep -q '"success":false'; then
echo "❌ Session setup failed. Check your li_at cookie."
exit 1
fi
# Step 3: Search for Leads
echo "🔍 Step 3: Searching for leads..."
SEARCH_RESULT=$(curl -s -X POST "$BASE_URL/api/leads/search" \
-H "Content-Type: application/json" \
-d "{
\"api_key\": \"$API_KEY\",
\"keywords\": \"CEO OR Founder OR CTO\",
\"location\": \"United States\",
\"limit\": 5
}")
echo "$SEARCH_RESULT" | python -m json.tool 2>/dev/null || echo "$SEARCH_RESULT"
echo ""
# Extract first profile URL
FIRST_PROFILE=$(echo "$SEARCH_RESULT" | python -c "import sys, json; data = json.load(sys.stdin); print(data.get('data', [{}])[0].get('profile_url', ''))" 2>/dev/null)
if [ -z "$FIRST_PROFILE" ] || [ "$FIRST_PROFILE" == "None" ]; then
echo "⚠️ No profiles found in search results"
echo "Trying manual profile analysis..."
FIRST_PROFILE="https://www.linkedin.com/in/test-profile"
else
echo "✅ Found profile: $FIRST_PROFILE"
fi
echo ""
# Step 4: Analyze Profile
echo "📊 Step 4: Analyzing profile..."
ANALYZE_RESULT=$(curl -s -X POST "$BASE_URL/api/leads/analyze" \
-H "Content-Type: application/json" \
-d "{
\"api_key\": \"$API_KEY\",
\"profile_url\": \"$FIRST_PROFILE\"
}")
echo "$ANALYZE_RESULT" | python -m json.tool 2>/dev/null || echo "$ANALYZE_RESULT"
echo ""
# Step 5: Score Lead (AI)
echo "🤖 Step 5: Scoring lead with AI (Vertex AI)..."
SCORE_RESULT=$(curl -s -X POST "$BASE_URL/api/leads/score" \
-H "Content-Type: application/json" \
-d "{
\"api_key\": \"$API_KEY\",
\"profile_url\": \"$FIRST_PROFILE\"
}")
echo "$SCORE_RESULT" | python -m json.tool 2>/dev/null || echo "$SCORE_RESULT"
echo ""
# Step 6: Generate Personalized Message
echo "✍️ Step 6: Generating personalized message with AI..."
MESSAGE_RESULT=$(curl -s -X POST "$BASE_URL/api/messages/generate" \
-H "Content-Type: application/json" \
-d "{
\"api_key\": \"$API_KEY\",
\"profile_url\": \"$FIRST_PROFILE\",
\"value_proposition\": \"We help tech companies scale their sales teams with AI-powered automation and lead generation solutions\",
\"message_type\": \"connection\"
}")
echo "$MESSAGE_RESULT" | python -m json.tool 2>/dev/null || echo "$MESSAGE_RESULT"
echo ""
# Extract message
MESSAGE_TEXT=$(echo "$MESSAGE_RESULT" | python -c "import sys, json; data = json.load(sys.stdin); print(data.get('message', ''))" 2>/dev/null)
if [ -n "$MESSAGE_TEXT" ] && [ "$MESSAGE_TEXT" != "None" ]; then
echo "✅ Generated message:"
echo "$MESSAGE_TEXT"
echo ""
echo "📤 Step 7: Ready to send message..."
echo "⚠️ To actually send, run:"
echo ""
echo "curl -X POST \"$BASE_URL/api/messages/send\" \\"
echo " -H \"Content-Type: application/json\" \\"
echo " -d '{"
echo " \"api_key\": \"$API_KEY\","
echo " \"profile_url\": \"$FIRST_PROFILE\","
echo " \"message\": \"$MESSAGE_TEXT\","
echo " \"is_connection_request\": true"
echo " }'"
echo ""
else
echo "⚠️ No message generated"
fi
echo "======================================"
echo "✅ Test Complete!"
echo "======================================"