import os, requests
from requests.auth import HTTPBasicAuth
from openai import OpenAI
CLOSE_KEY = os.environ["CLOSE_API_KEY"]
CLOSE_AUTH = HTTPBasicAuth(CLOSE_KEY, "")
MAVERA_KEY = os.environ["MAVERA_API_KEY"]
def close_get(path, params=None):
r = requests.get(f"https://api.close.com/api/v1{path}", auth=CLOSE_AUTH, params=params or {})
r.raise_for_status()
return r.json()
# 1. Pull calls with recordings
calls = [c for c in close_get("/activity/call/", {"_limit": 50}).get("data", []) if c.get("recording_url")]
if not calls:
print("No calls with recordings found.")
exit()
call = calls[0]
# 2. Download and transcribe via Whisper
audio = requests.get(call["recording_url"], auth=CLOSE_AUTH)
audio.raise_for_status()
audio_path = "/tmp/close_call.mp3"
with open(audio_path, "wb") as f:
f.write(audio.content)
whisper = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", ""))
with open(audio_path, "rb") as af:
transcription = whisper.audio.transcriptions.create(model="whisper-1", file=af, response_format="text")
transcript = transcription if isinstance(transcription, str) else transcription.text
# 3. Feed to Mave Agent
mave = requests.post(
"https://app.mavera.io/api/v1/mave/chat",
headers={"Authorization": f"Bearer {MAVERA_KEY}"},
json={"message": (
f"Analyze this sales call transcript. Extract:\n"
f"1. Key decisions made\n2. Objections raised\n"
f"3. Next steps and commitments\n4. Coaching insights for the rep\n"
f"5. Call quality score (1-10)\n\n"
f"Duration: {call.get('duration', 'unknown')}s | Direction: {call.get('direction', 'unknown')}\n\n"
f"Transcript:\n{transcript[:8000]}"
)},
)
mave.raise_for_status()
print(f"Call Analysis for {call.get('phone', 'unknown')}")
print(f"Duration: {call.get('duration', 0)}s\n")
print(mave.json().get("content", "")[:1000])