Files
link-analyzer/analyze-links.sh

67 lines
2.1 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# Scan #remora for new links, analyze, and add to Tududi inbox
# Run via cron every 5 minutes
set -euo pipefail
CHANNEL_ID="1467557082583535729"
TRACKER_FILE="$(dirname "$0")/tracker.json"
GATEWAY_URL="${OPENCLAW_GATEWAY:-http://127.0.0.1:18789}"
GATEWAY_TOKEN="${OPENCLAW_GATEWAY_TOKEN:-}"
if [ -z "$GATEWAY_TOKEN" ]; then
echo "⚠️ OPENCLAW_GATEWAY_TOKEN not set"
exit 1
fi
# Read last check timestamp
last_check=$(jq -r '.last_check' "$TRACKER_FILE")
processed_ids=$(jq -r '.processed_message_ids[]' "$TRACKER_FILE" 2>/dev/null || echo "")
# Fetch recent messages from #remora
echo "📡 Scanning #remora for new links..."
messages=$(curl -s \
-H "Authorization: Bearer ${DISCORD_BOT_TOKEN:-}" \
"https://discord.com/api/v10/channels/$CHANNEL_ID/messages?limit=50" 2>/dev/null || echo "[]")
# Extract URLs and process new ones
echo "$messages" | jq -r '.[] | select(.content | test("http")) | "\(.id)|\(.content)|\(.author.username)"' | while IFS='|' read -r msg_id content author; do
# Skip if already processed
if echo "$processed_ids" | grep -q "$msg_id"; then
continue
fi
echo "🔗 Found message from $author: $content"
# Extract URL (simple regex)
url=$(echo "$content" | grep -oP 'https?://[^\s]+' | head -1)
if [ -z "$url" ]; then
continue
fi
echo " URL: $url"
# Fetch and analyze
title=$(curl -s -I "$url" 2>/dev/null | grep -i "title" | cut -d' ' -f2- || echo "Unknown")
# Send to Tududi inbox with summary
if [ -n "$title" ]; then
echo " Adding to Tududi inbox: $title"
curl -s -X POST "https://todo.dilain.com/api/v1/inbox" \
-H "Authorization: Bearer ${TUDUDI_API_KEY:-}" \
-H "Content-Type: application/json" \
-d "{\"content\":\"📌 $title\n🔗 $url\"}" > /dev/null 2>&1
fi
# Update tracker
jq ".processed_message_ids += [\"$msg_id\"]" "$TRACKER_FILE" > "$TRACKER_FILE.tmp"
mv "$TRACKER_FILE.tmp" "$TRACKER_FILE"
done
# Update last check
jq ".last_check = \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\"" "$TRACKER_FILE" > "$TRACKER_FILE.tmp"
mv "$TRACKER_FILE.tmp" "$TRACKER_FILE"
echo "✅ Scan complete"