import os, requests, time, re
DC_TOKEN = os.environ["DISCORD_BOT_TOKEN"]
DC_BASE = "https://discord.com/api/v10"
DC_H = {"Authorization": f"Bot {DC_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"}
HELP_CHANNELS = ["CHANNEL_ID_1", "CHANNEL_ID_2"]
QUESTION_PATTERNS = re.compile(r"\?|how do|how can|is there|can i|does anyone|why does|where is|what is", re.I)
# 1. Collect questions from channels
all_questions = []
for channel_id in HELP_CHANNELS:
after = "0"
while True:
r = requests.get(f"{DC_BASE}/channels/{channel_id}/messages", headers=DC_H,
params={"limit": 100, "after": after})
if r.status_code == 429:
time.sleep(r.json().get("retry_after", 1))
continue
if not r.ok:
break
batch = r.json()
if not batch:
break
for m in batch:
content = m.get("content", "")
if (not m.get("author",{}).get("bot",False)
and QUESTION_PATTERNS.search(content)
and 15 < len(content) < 500):
all_questions.append(content)
after = batch[0]["id"]
if len(all_questions) >= 200:
break
time.sleep(0.5)
print(f"Collected {len(all_questions)} questions")
# 2. Cluster via Mave
clustering = requests.post(f"{MV_BASE}/mave/chat", headers=MV_H, json={
"message": f"FAQ analyst. Cluster these {len(all_questions)} community questions into 10-15 FAQ topics.\n\n"
+ "\n".join(f"- {q[:200]}" for q in all_questions[:100])
+ "\n\nFor each cluster:\n- Topic name (clear, concise)\n- Frequency (how many questions)\n- Representative question (best phrased example)\n- Key details needed in the answer\n\nRank by frequency."
}).json()
clusters_text = clustering.get("content", "")
print(f"\nClusters:\n{clusters_text[:1500]}")
# 3. Generate FAQ entries
faq = requests.post(f"{MV_BASE}/generations", headers=MV_H, json={
"prompt": f"Generate a complete FAQ page from these clustered community questions.\n\n"
f"CLUSTERS:\n{clusters_text[:4000]}\n\n"
"For each FAQ entry:\n"
"- Question (phrased clearly, search-friendly)\n"
"- Answer (2-4 sentences, helpful, specific)\n"
"- Related: link suggestions to other FAQ entries\n\n"
"Tone: friendly, direct, knowledgeable. Use 'you' not 'the user'.\n"
"Format as markdown with ## headers per question.",
}).json()
print(f"\n{'='*60}\nGENERATED FAQ ({len(all_questions)} questions → clustered)\n{'='*60}")
print(faq.get("output", faq.get("content", ""))[:2500])