2026-02-09 18:07:14 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
"""
|
2026-02-09 19:32:25 +01:00
|
|
|
Discord bot for #remora channel - analyzes links in real-time with Haiku
|
|
|
|
|
Fetches content, sends to gateway for AI analysis, adds to Tududi inbox
|
2026-02-09 18:07:14 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import discord
|
|
|
|
|
import os
|
|
|
|
|
import json
|
|
|
|
|
import re
|
|
|
|
|
import requests
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from dotenv import load_dotenv
|
2026-02-09 18:46:45 +01:00
|
|
|
import logging
|
|
|
|
|
from urllib.parse import urlparse
|
2026-02-09 18:07:14 +01:00
|
|
|
|
|
|
|
|
# Load .env file
|
|
|
|
|
load_dotenv()
|
|
|
|
|
|
2026-02-09 18:46:45 +01:00
|
|
|
# Setup logging
|
|
|
|
|
log_file = Path(__file__).parent / "bot.log"
|
|
|
|
|
logging.basicConfig(
|
|
|
|
|
level=logging.DEBUG,
|
|
|
|
|
format='[%(asctime)s] [%(levelname)-8s] %(message)s',
|
|
|
|
|
handlers=[
|
|
|
|
|
logging.FileHandler(log_file),
|
|
|
|
|
logging.StreamHandler()
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2026-02-09 18:07:14 +01:00
|
|
|
# Config
|
|
|
|
|
CHANNEL_ID = 1467557082583535729
|
|
|
|
|
TRACKER_FILE = Path(__file__).parent / "tracker.json"
|
|
|
|
|
TUDUDI_API_URL = os.getenv("TUDUDI_API_URL", "https://todo.dilain.com/api/v1")
|
|
|
|
|
TUDUDI_API_KEY = os.getenv("TUDUDI_API_KEY")
|
|
|
|
|
GATEWAY_URL = os.getenv("OPENCLAW_GATEWAY", "http://127.0.0.1:18789")
|
2026-02-09 18:46:45 +01:00
|
|
|
GATEWAY_TOKEN = os.getenv("OPENCLAW_GATEWAY_TOKEN", "")
|
|
|
|
|
|
|
|
|
|
logger.info("=" * 60)
|
|
|
|
|
logger.info("Bot startup")
|
|
|
|
|
logger.info(f" Channel ID: {CHANNEL_ID}")
|
|
|
|
|
logger.info(f" Tududi API: {TUDUDI_API_URL}")
|
|
|
|
|
logger.info(f" Gateway: {GATEWAY_URL}")
|
|
|
|
|
logger.info("=" * 60)
|
2026-02-09 18:07:14 +01:00
|
|
|
|
|
|
|
|
# Load or init tracker
|
|
|
|
|
def load_tracker():
|
|
|
|
|
if TRACKER_FILE.exists():
|
|
|
|
|
with open(TRACKER_FILE) as f:
|
|
|
|
|
return json.load(f)
|
|
|
|
|
return {
|
|
|
|
|
"channel_id": CHANNEL_ID,
|
|
|
|
|
"processed_message_ids": [],
|
|
|
|
|
"links": []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def save_tracker(data):
|
|
|
|
|
with open(TRACKER_FILE, "w") as f:
|
|
|
|
|
json.dump(data, f, indent=2)
|
|
|
|
|
|
|
|
|
|
# Detect links in text
|
|
|
|
|
def extract_urls(text):
|
|
|
|
|
url_pattern = r'https?://[^\s<>"{}|\\^`\[\]]+'
|
|
|
|
|
return re.findall(url_pattern, text)
|
|
|
|
|
|
2026-02-09 18:46:45 +01:00
|
|
|
# Detect link type
|
|
|
|
|
def detect_link_type(url):
|
|
|
|
|
domain = urlparse(url).netloc.lower()
|
|
|
|
|
|
|
|
|
|
if "github.com" in domain:
|
|
|
|
|
return "GitHub"
|
|
|
|
|
elif "reddit.com" in domain:
|
|
|
|
|
return "Reddit"
|
|
|
|
|
elif "youtube.com" in domain or "youtu.be" in domain:
|
|
|
|
|
return "YouTube"
|
|
|
|
|
elif "tiktok.com" in domain:
|
|
|
|
|
return "TikTok"
|
|
|
|
|
elif "twitter.com" in domain or "x.com" in domain:
|
|
|
|
|
return "Twitter/X"
|
|
|
|
|
elif "medium.com" in domain:
|
|
|
|
|
return "Medium"
|
|
|
|
|
elif "dev.to" in domain:
|
|
|
|
|
return "Dev.to"
|
|
|
|
|
elif "arxiv.org" in domain:
|
|
|
|
|
return "arXiv"
|
|
|
|
|
else:
|
|
|
|
|
return "Article"
|
|
|
|
|
|
2026-02-09 19:32:25 +01:00
|
|
|
# Fetch URL content
|
2026-02-09 18:46:45 +01:00
|
|
|
def fetch_url_content(url):
|
2026-02-09 19:32:25 +01:00
|
|
|
"""Fetch URL and return content"""
|
2026-02-09 18:46:45 +01:00
|
|
|
logger.debug(f" 📥 Fetching: {url}")
|
|
|
|
|
|
2026-02-09 18:07:14 +01:00
|
|
|
try:
|
2026-02-09 18:46:45 +01:00
|
|
|
response = requests.get(
|
|
|
|
|
url,
|
2026-02-09 18:50:14 +01:00
|
|
|
timeout=8,
|
|
|
|
|
headers={
|
|
|
|
|
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36',
|
|
|
|
|
'Accept': 'text/html,application/xhtml+xml'
|
|
|
|
|
},
|
2026-02-09 18:46:45 +01:00
|
|
|
allow_redirects=True
|
|
|
|
|
)
|
|
|
|
|
response.raise_for_status()
|
2026-02-09 19:32:25 +01:00
|
|
|
content = response.text[:5000] # First 5k chars
|
2026-02-09 18:50:14 +01:00
|
|
|
|
2026-02-09 19:32:25 +01:00
|
|
|
# Try to find title
|
2026-02-09 18:50:14 +01:00
|
|
|
title = None
|
|
|
|
|
title_match = re.search(r'<title[^>]*>\s*([^<]+?)\s*</title>', content, re.IGNORECASE)
|
|
|
|
|
if title_match:
|
|
|
|
|
title = title_match.group(1).strip()
|
2026-02-09 18:07:14 +01:00
|
|
|
|
2026-02-09 18:50:14 +01:00
|
|
|
if not title:
|
|
|
|
|
og_match = re.search(r'<meta\s+property="og:title"\s+content="([^"]+)"', content, re.IGNORECASE)
|
|
|
|
|
if og_match:
|
|
|
|
|
title = og_match.group(1).strip()
|
|
|
|
|
|
|
|
|
|
if not title:
|
|
|
|
|
title = url.split('/')[-1] or "Untitled"
|
2026-02-09 18:07:14 +01:00
|
|
|
|
2026-02-09 18:46:45 +01:00
|
|
|
logger.debug(f" ✓ Fetched: {title}")
|
2026-02-09 18:07:14 +01:00
|
|
|
return {
|
|
|
|
|
"title": title,
|
2026-02-09 18:46:45 +01:00
|
|
|
"content": content,
|
2026-02-09 18:07:14 +01:00
|
|
|
"status": "ok"
|
|
|
|
|
}
|
2026-02-09 18:46:45 +01:00
|
|
|
except requests.Timeout:
|
|
|
|
|
logger.warning(f" ⏱️ Timeout: {url}")
|
|
|
|
|
return {"title": "Request timeout", "status": "timeout", "content": ""}
|
2026-02-09 18:07:14 +01:00
|
|
|
except Exception as e:
|
2026-02-09 18:46:45 +01:00
|
|
|
logger.error(f" ❌ Error: {e}")
|
2026-02-09 19:32:25 +01:00
|
|
|
return {"title": "Fetch failed", "status": "error", "content": ""}
|
2026-02-09 19:17:37 +01:00
|
|
|
|
2026-02-10 14:54:55 +01:00
|
|
|
# Analyze with local heuristic (fallback when gateway unavailable)
|
2026-02-09 18:56:26 +01:00
|
|
|
def analyze_content(url, title, content, link_type):
|
2026-02-10 14:54:55 +01:00
|
|
|
"""Simple local analysis when gateway is unavailable"""
|
|
|
|
|
logger.debug(f" 🤖 Local analysis: {url}")
|
2026-02-09 18:46:45 +01:00
|
|
|
|
|
|
|
|
try:
|
2026-02-10 14:54:55 +01:00
|
|
|
# Simple tag based on link type
|
|
|
|
|
tag = "to-read"
|
|
|
|
|
if link_type == "GitHub":
|
|
|
|
|
tag = "project"
|
|
|
|
|
elif link_type == "YouTube":
|
|
|
|
|
tag = "video"
|
|
|
|
|
elif link_type == "Reddit":
|
|
|
|
|
tag = "discussion"
|
|
|
|
|
elif link_type in ["Medium", "Dev.to"]:
|
|
|
|
|
tag = "article"
|
|
|
|
|
elif link_type == "arXiv":
|
|
|
|
|
tag = "learning"
|
2026-02-09 19:05:31 +01:00
|
|
|
|
2026-02-10 14:54:55 +01:00
|
|
|
# Simple summary based on title and link type
|
|
|
|
|
summary = f"lien {link_type.lower()} : {title}"
|
|
|
|
|
|
|
|
|
|
logger.info(f" ✓ Local analysis complete")
|
2026-02-09 18:46:45 +01:00
|
|
|
|
2026-02-10 14:54:55 +01:00
|
|
|
return {
|
|
|
|
|
"summary": summary,
|
|
|
|
|
"tag": tag
|
|
|
|
|
}
|
2026-02-09 19:05:31 +01:00
|
|
|
|
2026-02-09 18:46:45 +01:00
|
|
|
except Exception as e:
|
2026-02-09 19:05:31 +01:00
|
|
|
logger.error(f" Analysis error: {e}")
|
2026-02-09 19:32:25 +01:00
|
|
|
return None
|
2026-02-09 19:17:37 +01:00
|
|
|
|
2026-02-09 18:07:14 +01:00
|
|
|
# Send to Tududi inbox
|
2026-02-09 18:56:26 +01:00
|
|
|
def add_to_tududi(title, url, link_type, summary="", tag=""):
|
2026-02-09 19:32:25 +01:00
|
|
|
"""Add to Tududi inbox"""
|
2026-02-09 18:46:45 +01:00
|
|
|
logger.debug(f" 📌 Adding to Tududi: {title}")
|
|
|
|
|
|
2026-02-09 18:07:14 +01:00
|
|
|
try:
|
|
|
|
|
if not TUDUDI_API_KEY:
|
2026-02-09 18:46:45 +01:00
|
|
|
logger.warning(" TUDUDI_API_KEY not set")
|
2026-02-09 18:07:14 +01:00
|
|
|
return False
|
|
|
|
|
|
2026-02-09 18:46:45 +01:00
|
|
|
content = f"📌 **{link_type}**: {title}\n🔗 {url}"
|
2026-02-09 18:56:26 +01:00
|
|
|
if summary:
|
2026-02-09 19:32:25 +01:00
|
|
|
content += f"\n\n💡 {summary}"
|
2026-02-09 18:56:26 +01:00
|
|
|
if tag:
|
2026-02-09 19:32:25 +01:00
|
|
|
content += f"\n\n🏷️ {tag}"
|
2026-02-09 18:07:14 +01:00
|
|
|
|
|
|
|
|
response = requests.post(
|
|
|
|
|
f"{TUDUDI_API_URL}/inbox",
|
|
|
|
|
headers={
|
|
|
|
|
"Authorization": f"Bearer {TUDUDI_API_KEY}",
|
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
|
},
|
|
|
|
|
json={"content": content},
|
|
|
|
|
timeout=5
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-09 19:32:25 +01:00
|
|
|
if response.status_code in [200, 201]:
|
|
|
|
|
logger.info(f" ✓ Added to Tududi")
|
2026-02-09 18:07:14 +01:00
|
|
|
return True
|
|
|
|
|
else:
|
2026-02-09 18:46:45 +01:00
|
|
|
logger.warning(f" Tududi error: {response.status_code}")
|
2026-02-09 18:07:14 +01:00
|
|
|
return False
|
|
|
|
|
except Exception as e:
|
2026-02-09 18:46:45 +01:00
|
|
|
logger.error(f" Tududi error: {e}")
|
2026-02-09 18:07:14 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
# Discord bot
|
|
|
|
|
intents = discord.Intents.default()
|
|
|
|
|
intents.message_content = True
|
|
|
|
|
|
|
|
|
|
class LinkAnalyzerBot(discord.Client):
|
|
|
|
|
async def on_ready(self):
|
2026-02-09 18:46:45 +01:00
|
|
|
logger.info(f"✅ Bot logged in as {self.user}")
|
|
|
|
|
logger.info(f"📍 Watching channel #remora ({CHANNEL_ID})")
|
2026-02-09 18:07:14 +01:00
|
|
|
|
|
|
|
|
async def on_message(self, message):
|
|
|
|
|
# Ignore bot's own messages
|
|
|
|
|
if message.author == self.user:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Only process #remora channel
|
|
|
|
|
if message.channel.id != CHANNEL_ID:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Check for URLs
|
|
|
|
|
urls = extract_urls(message.content)
|
|
|
|
|
if not urls:
|
2026-02-09 18:46:45 +01:00
|
|
|
logger.debug(f"No URLs in message from {message.author}")
|
2026-02-09 18:07:14 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Skip if already processed
|
|
|
|
|
tracker = load_tracker()
|
|
|
|
|
if message.id in tracker["processed_message_ids"]:
|
2026-02-09 18:46:45 +01:00
|
|
|
logger.debug(f"Skipping already-processed message {message.id}")
|
2026-02-09 18:07:14 +01:00
|
|
|
return
|
|
|
|
|
|
2026-02-09 18:46:45 +01:00
|
|
|
logger.info(f"🔗 New link(s) from {message.author}: {message.content}")
|
2026-02-09 18:07:14 +01:00
|
|
|
|
|
|
|
|
# Process each URL
|
|
|
|
|
for url in urls:
|
2026-02-09 18:46:45 +01:00
|
|
|
try:
|
|
|
|
|
logger.info(f"Processing: {url}")
|
|
|
|
|
link_type = detect_link_type(url)
|
|
|
|
|
|
|
|
|
|
# Fetch content
|
|
|
|
|
fetch_result = fetch_url_content(url)
|
|
|
|
|
title = fetch_result["title"]
|
|
|
|
|
|
2026-02-09 19:32:25 +01:00
|
|
|
# Analyze with Haiku
|
2026-02-09 18:56:26 +01:00
|
|
|
analysis_data = None
|
|
|
|
|
if fetch_result["status"] == "ok":
|
2026-02-09 19:32:25 +01:00
|
|
|
logger.debug(f" Analyzing...")
|
|
|
|
|
analysis_data = analyze_content(url, title, fetch_result["content"], link_type)
|
2026-02-09 18:56:26 +01:00
|
|
|
|
2026-02-09 19:32:25 +01:00
|
|
|
# Prepare summary
|
2026-02-09 18:56:26 +01:00
|
|
|
summary_text = ""
|
|
|
|
|
tag = "interesting"
|
|
|
|
|
if analysis_data:
|
|
|
|
|
summary_text = analysis_data.get("summary", "")
|
|
|
|
|
tag = analysis_data.get("tag", "interesting")
|
2026-02-09 19:32:25 +01:00
|
|
|
logger.debug(f" Summary: {summary_text[:60]}")
|
2026-02-09 18:46:45 +01:00
|
|
|
|
2026-02-09 19:32:25 +01:00
|
|
|
# Add to Tududi
|
|
|
|
|
add_to_tududi(title, url, link_type, summary_text, tag)
|
2026-02-09 18:46:45 +01:00
|
|
|
|
2026-02-09 19:32:25 +01:00
|
|
|
# Format response
|
2026-02-09 18:46:45 +01:00
|
|
|
response_text = f"📌 **{link_type}**: {title}"
|
2026-02-09 18:56:26 +01:00
|
|
|
if summary_text:
|
|
|
|
|
response_text += f"\n\n💡 {summary_text}"
|
|
|
|
|
if tag:
|
2026-02-09 19:32:25 +01:00
|
|
|
response_text += f"\n\n🏷️ `{tag}`"
|
2026-02-09 18:46:45 +01:00
|
|
|
|
2026-02-09 19:32:25 +01:00
|
|
|
logger.debug(f"Posting response...")
|
2026-02-09 18:46:45 +01:00
|
|
|
|
|
|
|
|
# Post in channel
|
|
|
|
|
await message.reply(response_text, mention_author=False)
|
|
|
|
|
|
|
|
|
|
# Update tracker
|
|
|
|
|
tracker["links"].append({
|
|
|
|
|
"url": url,
|
|
|
|
|
"title": title,
|
|
|
|
|
"type": link_type,
|
|
|
|
|
"author": str(message.author),
|
|
|
|
|
"message_id": message.id,
|
|
|
|
|
"date": datetime.now().isoformat(),
|
2026-02-09 19:32:25 +01:00
|
|
|
"summary": summary_text,
|
|
|
|
|
"tag": tag
|
2026-02-09 18:46:45 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
logger.info(f"✓ Processed: {url}")
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
2026-02-09 19:32:25 +01:00
|
|
|
logger.error(f"❌ Error: {e}")
|
|
|
|
|
import traceback
|
|
|
|
|
logger.error(traceback.format_exc())
|
|
|
|
|
try:
|
|
|
|
|
await message.reply(f"❌ Error: {str(e)[:100]}", mention_author=False)
|
|
|
|
|
except:
|
|
|
|
|
pass
|
2026-02-09 18:07:14 +01:00
|
|
|
|
2026-02-09 19:32:25 +01:00
|
|
|
# Update tracker
|
2026-02-09 18:07:14 +01:00
|
|
|
tracker["processed_message_ids"].append(message.id)
|
|
|
|
|
save_tracker(tracker)
|
2026-02-09 19:32:25 +01:00
|
|
|
logger.info(f"Updated tracker: {len(tracker['links'])} links total")
|
2026-02-09 18:07:14 +01:00
|
|
|
|
|
|
|
|
# Main
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
token = os.getenv("DISCORD_BOT_TOKEN")
|
|
|
|
|
if not token:
|
2026-02-09 18:46:45 +01:00
|
|
|
logger.error("❌ DISCORD_BOT_TOKEN not set!")
|
2026-02-09 18:07:14 +01:00
|
|
|
exit(1)
|
|
|
|
|
|
2026-02-09 18:46:45 +01:00
|
|
|
logger.info("Starting bot...")
|
2026-02-09 18:07:14 +01:00
|
|
|
bot = LinkAnalyzerBot(intents=intents)
|
|
|
|
|
bot.run(token)
|