Skip to content
Shell
10.6 KB
Raw Blame
#!/bin/bash
# ═══════════════════════════════════════════════════════════════════════════════
# ID8Pipeline Messenger - iMessage Integration
# ═══════════════════════════════════════════════════════════════════════════════
# Sends pipeline notifications via iMessage and can listen for replies.
#
# Usage:
#   ./pipeline-messenger.sh notify "message"     - Send a notification
#   ./pipeline-messenger.sh alert "message"      - Send urgent alert
#   ./pipeline-messenger.sh ask "question"       - Ask and wait for reply
#   ./pipeline-messenger.sh setup                - Configure phone number
# ═══════════════════════════════════════════════════════════════════════════════

set -euo pipefail

CONFIG_DIR="$HOME/.claude/pipeline-memory"
CONFIG_FILE="$CONFIG_DIR/messenger-config.txt"
REPLY_LOG="$CONFIG_DIR/imessage-replies.log"

mkdir -p "$CONFIG_DIR"

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

# ─────────────────────────────────────────────────────────────────────────────
# Load configuration
# ─────────────────────────────────────────────────────────────────────────────
load_config() {
    if [[ -f "$CONFIG_FILE" ]]; then
        source "$CONFIG_FILE"
    else
        PHONE_NUMBER=""
    fi
}

save_config() {
    echo "PHONE_NUMBER=\"$PHONE_NUMBER\"" > "$CONFIG_FILE"
}

# ─────────────────────────────────────────────────────────────────────────────
# Setup - configure phone number
# ─────────────────────────────────────────────────────────────────────────────
cmd_setup() {
    echo ""
    echo -e "${GREEN}ID8Pipeline Messenger Setup${NC}"
    echo ""
    echo "Enter your phone number or iMessage email:"
    echo "(Format: +1234567890 or [email protected])"
    echo ""
    read -p "> " PHONE_NUMBER

    save_config

    echo ""
    echo -e "${GREEN}✓ Saved!${NC}"
    echo ""
    echo "Testing connection..."

    send_message "🔔 ID8Pipeline connected! You'll receive notifications here."

    echo ""
    echo -e "${GREEN}✓ Test message sent. Check your phone!${NC}"
}

# ─────────────────────────────────────────────────────────────────────────────
# Send iMessage
# ─────────────────────────────────────────────────────────────────────────────
send_message() {
    local message="$1"

    load_config

    if [[ -z "${PHONE_NUMBER:-}" ]]; then
        echo -e "${RED}Error: Phone number not configured. Run: pipeline-messenger.sh setup${NC}"
        return 1
    fi

    # Send via AppleScript
    osascript <<EOF
tell application "Messages"
    set targetService to 1st account whose service type = iMessage
    set targetBuddy to participant "$PHONE_NUMBER" of targetService
    send "$message" to targetBuddy
end tell
EOF

    # Log the send
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] SENT: $message" >> "$REPLY_LOG"

    return 0
}

# ─────────────────────────────────────────────────────────────────────────────
# Send notification (non-urgent)
# ─────────────────────────────────────────────────────────────────────────────
cmd_notify() {
    local message="$1"
    send_message "📋 Pipeline: $message"
    echo "Notification sent"
}

# ─────────────────────────────────────────────────────────────────────────────
# Send alert (urgent)
# ─────────────────────────────────────────────────────────────────────────────
cmd_alert() {
    local message="$1"
    send_message "🔴 PIPELINE ALERT: $message"
    echo "Alert sent"
}

# ─────────────────────────────────────────────────────────────────────────────
# Send stale project alert
# ─────────────────────────────────────────────────────────────────────────────
cmd_stale_alert() {
    local project="$1"
    local stage="$2"
    local hours="$3"

    local message="⏰ $project is stale!

Stage $stage hasn't been updated in ${hours}h.

Reply:
• Y - Authorize autonomous work
• N - I'll handle it
• [task] - Work on specific task"

    send_message "$message"
    echo "Stale alert sent for $project"
}

# ─────────────────────────────────────────────────────────────────────────────
# Ask question and wait for reply
# ─────────────────────────────────────────────────────────────────────────────
cmd_ask() {
    local question="$1"
    local timeout="${2:-300}"  # Default 5 minutes

    send_message "❓ $question

Reply within ${timeout}s or I'll wait."

    echo "Question sent, waiting for reply..."
    echo "(Note: Reply detection requires Messages.app automation permissions)"

    # For now, we can't easily detect replies without more complex setup
    # The user would need to:
    # 1. Grant automation permissions
    # 2. Set up a reply webhook or poll Messages database

    echo ""
    echo -e "${YELLOW}Tip: Full reply detection requires additional setup.${NC}"
    echo "For now, check manually or use a Telegram bot for two-way communication."
}

# ─────────────────────────────────────────────────────────────────────────────
# Send progress update
# ─────────────────────────────────────────────────────────────────────────────
cmd_progress() {
    local project="$1"
    local status="$2"
    local details="${3:-}"

    local message="📊 $project Progress

$status"

    if [[ -n "$details" ]]; then
        message="$message

$details"
    fi

    send_message "$message"
    echo "Progress update sent"
}

# ─────────────────────────────────────────────────────────────────────────────
# Send completion notification
# ─────────────────────────────────────────────────────────────────────────────
cmd_complete() {
    local project="$1"
    local summary="$2"

    send_message "✅ $project Task Complete!

$summary"

    echo "Completion notification sent"
}

# ─────────────────────────────────────────────────────────────────────────────
# Entry Point
# ─────────────────────────────────────────────────────────────────────────────
case "${1:-help}" in
    setup)
        cmd_setup
        ;;
    notify)
        cmd_notify "${2:-No message provided}"
        ;;
    alert)
        cmd_alert "${2:-Alert!}"
        ;;
    stale)
        cmd_stale_alert "${2:-Project}" "${3:-?}" "${4:-?}"
        ;;
    ask)
        cmd_ask "${2:-Question?}" "${3:-300}"
        ;;
    progress)
        cmd_progress "${2:-Project}" "${3:-In progress}" "${4:-}"
        ;;
    complete)
        cmd_complete "${2:-Project}" "${3:-Task completed}"
        ;;
    test)
        send_message "🧪 Test message from ID8Pipeline"
        echo "Test message sent"
        ;;
    help|--help|-h|"")
        echo "ID8Pipeline Messenger"
        echo ""
        echo "Usage: pipeline-messenger.sh [command] [args]"
        echo ""
        echo "Commands:"
        echo "  setup                    Configure your phone number"
        echo "  notify \"message\"         Send a notification"
        echo "  alert \"message\"          Send urgent alert"
        echo "  stale proj stage hours   Send stale project alert"
        echo "  ask \"question\" [timeout] Ask and await reply"
        echo "  progress proj status     Send progress update"
        echo "  complete proj summary    Send completion notice"
        echo "  test                     Send test message"
        echo ""
        ;;
    *)
        echo "Unknown command: $1"
        echo "Run 'pipeline-messenger.sh help' for usage"
        exit 1
        ;;
esac