import os, requests, time
NOTION = os.environ["NOTION_API_KEY"]
MV = os.environ["MAVERA_API_KEY"]
NB = "https://api.notion.com/v1"
MB = "https://app.mavera.io/api/v1"
NH = {
"Authorization": f"Bearer {NOTION}",
"Notion-Version": "2022-06-28",
"Content-Type": "application/json",
}
MH = {"Authorization": f"Bearer {MV}", "Content-Type": "application/json"}
COMPETITIVE_DB_ID = "your-competitive-wiki-database-id"
# 1. Query competitive wiki pages
competitors = requests.post(f"{NB}/databases/{COMPETITIVE_DB_ID}/query", headers=NH, json={
"sorts": [{"property": "Last edited time", "direction": "descending"}],
"page_size": 10,
}).json().get("results", [])
print(f"Found {len(competitors)} competitor pages")
# 2. Extract text from each page
def get_blocks_text(block_id):
texts = []
cursor = None
while True:
params = {"page_size": 100}
if cursor:
params["start_cursor"] = cursor
r = requests.get(f"{NB}/blocks/{block_id}/children", headers=NH, params=params)
if r.status_code == 429:
time.sleep(1); continue
data = r.json()
for block in data.get("results", []):
btype = block.get("type", "")
rt = block.get(btype, {}).get("rich_text", [])
text = "".join(t.get("plain_text", "") for t in rt)
if text.strip():
texts.append(text)
cursor = data.get("next_cursor")
if not cursor:
break
time.sleep(0.4)
return "\n".join(texts)
battle_cards = []
for page in competitors:
props = page.get("properties", {})
title_parts = props.get("Name", props.get("Title", props.get("Competitor", {}))).get("title", [])
comp_name = "".join(t.get("plain_text", "") for t in title_parts) or "Unknown Competitor"
category = (props.get("Category", {}).get("select", {}) or {}).get("name", "")
website = (props.get("Website", {}).get("url", None)) or ""
internal_intel = get_blocks_text(page["id"])
print(f" Extracting: {comp_name} ({len(internal_intel)} chars)")
# 3. Enrich with Mave Agent (public research)
enrichment = requests.post(f"{MB}/mave/chat", headers=MH, json={
"message": (
f"Research the company '{comp_name}'"
f"{' (' + website + ')' if website else ''}. "
f"Focus on: recent product launches, pricing changes, funding rounds, "
f"key hires, market positioning, and notable customer wins or losses. "
f"Provide cited facts with dates where possible."
),
}).json()
mave_research = enrichment.get("content", "")
sources = enrichment.get("sources", [])
# 4. Generate battle card combining internal intel + Mave research
gen = requests.post(f"{MB}/generations", headers=MH, json={
"prompt": (
f"Create a sales battle card for competing against '{comp_name}'.\n\n"
f"INTERNAL INTELLIGENCE (from our wiki):\n{internal_intel[:4000]}\n\n"
f"MARKET RESEARCH:\n{mave_research[:4000]}\n\n"
"Structure the battle card with these sections:\n"
"1. **Overview** — Company summary, funding, employee count\n"
"2. **Their Positioning** — How they describe themselves\n"
"3. **Where They Win** — Their genuine strengths\n"
"4. **Where We Win** — Our advantages backed by evidence\n"
"5. **Common Objections** — What prospects say, with rebuttals\n"
"6. **Killer Questions** — Questions reps should ask that expose weaknesses\n"
"7. **Landmine Questions** — Questions to watch for that favor them\n"
"8. **Quick Pricing Comparison** — If pricing data is available\n"
"9. **Recent Customer Movements** — Wins/losses from either side\n\n"
"Be specific and actionable. This goes directly to sales reps."
),
}).json()
card_content = gen.get("output", gen.get("content", ""))
battle_cards.append({
"competitor": comp_name,
"card": card_content,
"sources": len(sources),
})
# 5. Optionally write back to Notion as a child page or block
blocks = []
for paragraph in card_content.split("\n\n")[:50]:
p = paragraph.strip()
if not p:
continue
if p.startswith("## ") or p.startswith("**") and p.endswith("**"):
blocks.append({"object": "block", "type": "heading_2",
"heading_2": {"rich_text": [{"type": "text", "text": {"content": p.strip("#* ")}}]}})
else:
blocks.append({"object": "block", "type": "paragraph",
"paragraph": {"rich_text": [{"type": "text", "text": {"content": p[:2000]}}]}})
if blocks:
bc_page = requests.post(f"{NB}/pages", headers=NH, json={
"parent": {"database_id": COMPETITIVE_DB_ID},
"properties": {
"Name": {"title": [{"text": {"content": f"Battle Card: {comp_name}"}}]},
},
"children": blocks[:100],
})
if bc_page.ok:
print(f" → Battle card page created in Notion")
print(f" ✓ {comp_name} — {len(card_content)} chars, {len(sources)} sources")
time.sleep(1)
print(f"\nGenerated {len(battle_cards)} battle cards")