#!/usr/bin/env bash # OpenClaw Bridge Script for Home Assistant # # Usage: ./openclaw-bridge.sh "message to send to OpenClaw" # Returns: JSON response suitable for HA TTS set -euo pipefail MESSAGE="${1:-}" AGENT="${2:-main}" TIMEOUT="${3:-30}" if [[ -z "$MESSAGE" ]]; then echo '{"error": "No message provided"}' >&2 exit 1 fi # Run OpenClaw agent and capture response # The CLI outputs the response to stdout RESPONSE=$(openclaw agent --message "$MESSAGE" --agent "$AGENT" 2>/dev/null || echo "Error: OpenClaw command failed") # Output JSON for HA using jq for proper escaping if command -v jq &>/dev/null; then echo "$RESPONSE" | jq -Rs '{response: .}' else # Fallback: use Python for JSON encoding if jq is not available python3 -c "import json,sys; print(json.dumps({'response': sys.stdin.read()}))" <<< "$RESPONSE" fi