import os, requests
DOMAIN = os.environ["PIPEDRIVE_DOMAIN"]
PD_TOKEN = os.environ["PIPEDRIVE_API_TOKEN"]
PD_BASE = f"https://{DOMAIN}.pipedrive.com"
MAVERA_KEY = os.environ["MAVERA_API_KEY"]
def pd_get(path, params=None):
params = params or {}
params["api_token"] = PD_TOKEN
r = requests.get(f"{PD_BASE}{path}", params=params)
r.raise_for_status()
return r.json()
# 1. Pull organizations
orgs = pd_get("/api/v2/organizations", {"limit": 20, "sort_by": "update_time", "sort_direction": "desc"}).get("data", []) or []
# 2. Research each with Mave Agent
for org in orgs[:10]:
name = org.get("name", "Unknown")
addr = org.get("address", "")
query = (
f"Research '{name}'{f' based in {addr}' if addr else ''}. "
f"Analyze: (1) business model, (2) recent news, (3) competitive landscape, "
f"(4) strategic initiatives, (5) pain points for B2B SaaS. Cite sources."
)
mave = requests.post(
"https://app.mavera.io/api/v1/mave/chat",
headers={"Authorization": f"Bearer {MAVERA_KEY}"},
json={"message": query},
)
mave.raise_for_status()
result = mave.json()
print(f"\n{'='*50}\nAccount: {name} | Sources: {len(result.get('sources', []))}")
print(result.get("content", "")[:500])
# 3. Write brief back as Pipedrive note
requests.post(
f"{PD_BASE}/api/v1/notes",
params={"api_token": PD_TOKEN},
json={
"content": f"<b>Account Intelligence Brief</b><br><br>{result.get('content', '')[:3000]}",
"org_id": org["id"],
"pinned_to_organization_flag": 1,
},
)