import os, requests
SF = os.environ["SALESFORCE_INSTANCE"]
SF_T = os.environ["SALESFORCE_ACCESS_TOKEN"]
MV_K = os.environ["MAVERA_API_KEY"]
SF_H = {"Authorization": f"Bearer {SF_T}"}
def sf_query(soql):
r = requests.get(f"https://{SF}/services/data/v66.0/query", headers=SF_H, params={"q": soql})
r.raise_for_status()
return r.json()["records"]
aid = "0015e000001XyZa"
acct = sf_query(f"SELECT Name, Industry, AnnualRevenue, NumberOfEmployees, Description FROM Account WHERE Id = '{aid}'")[0]
contacts = sf_query(f"SELECT Name, Title FROM Contact WHERE AccountId = '{aid}' ORDER BY CreatedDate DESC LIMIT 10")
tasks = sf_query(f"SELECT Subject, Description, ActivityDate FROM Task WHERE AccountId = '{aid}' ORDER BY ActivityDate DESC LIMIT 10")
contact_block = "\n".join(f"- {c['Name']} ({c.get('Title', 'N/A')})" for c in contacts)
task_block = "\n".join(f"- [{t.get('ActivityDate', 'N/A')}] {t.get('Subject', '')}: {(t.get('Description') or '')[:200]}" for t in tasks)
prompt = f"""Research this account and produce an AE-ready intelligence brief.
ACCOUNT: {acct['Name']} | {acct.get('Industry', 'N/A')} | ${acct.get('AnnualRevenue', 'N/A')} | {acct.get('NumberOfEmployees', 'N/A')} employees
Description: {acct.get('Description', 'N/A')}
KEY CONTACTS
{contact_block}
RECENT ACTIVITY
{task_block}
Produce: 1) Company overview & news 2) Industry trends 3) Competitive landscape 4) Messaging angles 5) Discovery questions"""
brief = requests.post(
"https://app.mavera.io/api/v1/mave/chat",
headers={"Authorization": f"Bearer {MV_K}", "Content-Type": "application/json"},
json={"message": prompt},
).json()
print(brief.get("content", ""))
print(f"Sources: {len(brief.get('sources', []))}")