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"}
WINS_CHANNEL = "C0123WINS"
LOSSES_CHANNEL = "C0123LOSSES"
DAYS_BACK = 90
def fetch_channel(channel_id):
oldest = str(int(time.time()) - DAYS_BACK * 86400)
msgs = []
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
msgs.extend(data.get("messages", []))
cursor = data.get("response_metadata", {}).get("next_cursor")
if not cursor:
break
time.sleep(1)
return [m for m in msgs if m.get("type") == "message"
and not m.get("bot_id") and len(m.get("text","")) > 20]
wins = fetch_channel(WINS_CHANNEL)
losses = fetch_channel(LOSSES_CHANNEL)
print(f"Wins: {len(wins)} messages | Losses: {len(losses)} messages")
# 2. Build corpora
win_text = "\n\n---\n\n".join(m.get("text","")[:500] for m in wins[-50:])
loss_text = "\n\n---\n\n".join(m.get("text","")[:500] for m in losses[-50:])
# 3. Create two brand voices
win_voice = requests.post(f"{MV_BASE}/brand-voices", headers=MV_H, json={
"name": "Win Voice: Sales Victories",
"extracted_content": win_text,
"description": f"Voice from {len(wins)} deal-win announcements. Captures celebration, proof language, competitive positioning, and momentum.",
}).json()
print(f"Win Voice: {win_voice.get('id','')} — {win_voice.get('traits', win_voice.get('voice_summary',''))}")
loss_voice = requests.post(f"{MV_BASE}/brand-voices", headers=MV_H, json={
"name": "Recovery Voice: Deal Losses",
"extracted_content": loss_text,
"description": f"Voice from {len(losses)} deal-loss notes. Captures empathy, learning, resilience, and honest reflection.",
}).json()
print(f"Loss Voice: {loss_voice.get('id','')} — {loss_voice.get('traits', loss_voice.get('voice_summary',''))}")
# 4. Test generation with each voice
for voice, label, prompt in [
(win_voice, "WIN", "Write a case study opening paragraph (100 words) celebrating a customer's success with our platform. Confident, proof-heavy, momentum-driven."),
(loss_voice, "RECOVERY", "Write a re-engagement email (100 words) to a churned customer. Empathetic, honest, learning-focused. Invite them back with a new feature."),
]:
gen = requests.post(f"{MV_BASE}/generations", headers=MV_H, json={
"brand_voice_id": voice["id"],
"prompt": prompt,
}).json()
print(f"\n[{label} VOICE TEST]\n{gen.get('output', gen.get('content',''))[:400]}")