import os, requests, time
GOOG = os.environ["GOOGLE_ACCESS_TOKEN"]
ACCT = os.environ["GOOGLE_ACCOUNT_ID"]
MV = os.environ["MAVERA_API_KEY"]
GB_BASE = "https://mybusiness.googleapis.com/v4"
MV_H = {"Authorization": f"Bearer {MV}", "Content-Type": "application/json"}
GB_H = {"Authorization": f"Bearer {GOOG}"}
STAR_MAP = {"ONE": 1, "TWO": 2, "THREE": 3, "FOUR": 4, "FIVE": 5}
# 1. Aggregate reviews from all locations
all_reviews = []
locations = requests.get(f"{GB_BASE}/{ACCT}/locations",
headers=GB_H, params={"pageSize": 50}).json().get("locations", [])
for loc in locations[:10]:
loc_id = loc.get("name", "")
loc_name = loc.get("locationName", "Unknown")
r = requests.get(f"{GB_BASE}/{loc_id}/reviews",
headers=GB_H, params={"pageSize": 30})
if r.status_code != 200:
continue
for rev in r.json().get("reviews", []):
stars = STAR_MAP.get(rev.get("starRating"), 3)
comment = rev.get("comment", "")
if comment:
all_reviews.append({"stars": stars, "text": comment[:300], "location": loc_name})
time.sleep(0.5)
# 2. Cluster into themes with Mave
review_block = "\n".join(
f"[{r['stars']}★ @ {r['location']}] {r['text'][:200]}"
for r in all_reviews[:40]
)
themes = requests.post("https://app.mavera.io/api/v1/mave/chat", headers=MV_H,
json={"message": f"""Cluster these {len(all_reviews)} Google Business reviews into theme categories.
{review_block}
For each theme:
- Category name
- Star range (which star levels this appears in)
- Frequency
- Representative quote
- Emotional tone of the reviewer
Return the top 8 themes."""}).json()
print("=== Review Theme Clusters ===")
print(themes.get("content", "")[:800])
# 3. Generate response templates per theme
gen = requests.post("https://app.mavera.io/api/v1/generations", headers=MV_H,
json={"prompt": f"""Generate review response templates based on these review themes:
{themes.get('content','')[:1200]}
For EACH theme, generate TWO templates:
1. One for 1-2 star reviews (empathetic, solution-oriented)
2. One for 4-5 star reviews (grateful, relationship-building)
Rules:
- Include [CUSTOMER_NAME] and [LOCATION] placeholders
- Under 80 words each
- Never defensive or corporate
- End with a specific next step or invitation
- Each template should feel warm and human"""}).json()
print("\n=== Response Templates ===")
print(gen.get("output", gen.get("content", gen.get("text", "")))[:2000])