Files
skill-whatsapp/scripts/query.sh

61 lines
1.6 KiB
Bash

#!/usr/bin/env bash
# Helper para llamar al whatsapp-bridge API con el bearer ya aplicado.
#
# Uso:
# query.sh <path> # GET
# query.sh -X POST -d '<json>' <path> # POST con body
# query.sh -H 'X-Confirm-Wipe: yes' -X POST /api/auth/logout
#
# Requiere:
# ~/.claude/skills/whatsapp/.env con WA_BRIDGE_TOKEN=<valor>
# (opcional) WA_BRIDGE_URL (default https://whatsapp.nucleoriofrio.com)
set -euo pipefail
ENV_FILE="${HOME}/.claude/skills/whatsapp/.env"
if [ ! -f "$ENV_FILE" ]; then
echo "ERROR: $ENV_FILE missing. Create it with WA_BRIDGE_TOKEN=<token>" >&2
exit 2
fi
# shellcheck disable=SC1090
set -a; . "$ENV_FILE"; set +a
if [ -z "${WA_BRIDGE_TOKEN:-}" ]; then
echo "ERROR: WA_BRIDGE_TOKEN not set in $ENV_FILE" >&2
exit 2
fi
BASE="${WA_BRIDGE_URL:-https://whatsapp.nucleoriofrio.com}"
# El último argumento es el path; todo lo anterior son flags de curl pasados through.
args=()
path=""
while [ $# -gt 0 ]; do
case "$1" in
--) shift; break ;;
-*) args+=("$1"); [ $# -ge 2 ] && args+=("$2") && shift; shift ;;
*) path="$1"; shift ;;
esac
done
# Si quedó algún argumento posicional al final (tras --)
[ $# -gt 0 ] && path="${path:-$1}"
if [ -z "$path" ]; then
echo "Usage: query.sh [curl flags] <path>" >&2
exit 1
fi
# Asegura que path empiece con /
case "$path" in
/*) ;;
http*) echo "ERROR: pass only the path, not a full URL" >&2; exit 1 ;;
*) path="/$path" ;;
esac
exec curl -fsS \
-H "Authorization: Bearer ${WA_BRIDGE_TOKEN}" \
-H "Accept: application/json" \
"${args[@]}" \
"${BASE}${path}"