import os, requests, time
SL_TOKEN = os.environ["SLACK_BOT_TOKEN"]
SL_BASE = "https://slack.com/api"
SL_H = {"Authorization": f"Bearer {SL_TOKEN}"}
MV = os.environ["MAVERA_API_KEY"]
MV_BASE = "https://app.mavera.io/api/v1"
MV_H = {"Authorization": f"Bearer {MV}", "Content-Type": "application/json"}
CHANNEL_ID = "C0123INTEL"
DAYS_BACK = 30
# 1. Fetch intel channel
oldest = str(int(time.time()) - DAYS_BACK * 86400)
messages = []
cursor = None
while True:
params = {"channel": CHANNEL_ID, "limit": 200, "oldest": oldest}
if cursor:
params["cursor"] = cursor
r = requests.get(f"{SL_BASE}/conversations.history", headers=SL_H, params=params)
data = r.json()
if not data.get("ok"):
break
messages.extend(data.get("messages", []))
cursor = data.get("response_metadata", {}).get("next_cursor")
if not cursor:
break
time.sleep(1)
intel = [m for m in messages if m.get("type") == "message" and len(m.get("text","")) > 20]
print(f"Intel input: {len(intel)} (past {DAYS_BACK} days)")
# 2. Build enriched corpus (include thread replies for context)
enriched = []
for m in sorted(intel, key=lambda x: float(x.get("ts",0)))[-50:]:
entry = f"[{time.strftime('%Y-%m-%d', time.localtime(float(m.get('ts',0))))}] {m.get('text','')[:500]}"
if m.get("reply_count", 0) > 0:
replies = requests.get(f"{SL_BASE}/conversations.replies", headers=SL_H,
params={"channel": CHANNEL_ID, "ts": m["ts"], "limit": 5}).json()
if replies.get("ok"):
for rep in replies.get("messages", [])[1:]:
entry += f"\n → Reply: {rep.get('text','')[:200]}"
time.sleep(1)
enriched.append(entry)
corpus = "\n\n".join(enriched)
# 3. Mave research brief
brief = requests.post(f"{MV_BASE}/mave/chat", headers=MV_H, json={
"message": f"Competitive intelligence analyst. Synthesize {len(intel)} internal observations from our #competitive-intel channel.\n\n"
f"RAW INTEL:\n{corpus[:8000]}\n\n"
"Produce a COMPETITIVE RESEARCH BRIEF with:\n\n"
"1. **Executive Summary** (3 sentences)\n"
"2. **Competitor Activity Matrix** — What each competitor did, our assessment\n"
"3. **Pricing Intelligence** — Any pricing changes detected\n"
"4. **Feature Gap Analysis** — What competitors have that we don't\n"
"5. **Customer Movement** — Any switching signals (theirs to us, ours to them)\n"
"6. **Recommended Actions** for Product, Marketing, and Sales\n"
"7. **Watch List** — Emerging threats to monitor next month\n\n"
"Format as a brief that can be shared directly with leadership."
}).json()
print(f"\n{'='*60}\nCOMPETITIVE RESEARCH BRIEF — {DAYS_BACK}-day window\n{'='*60}")
print(brief.get("content", "")[:3000])