#!/usr/bin/env bash # scripts/connect/connect-channels.sh — S7: Channels connect helper # # Connects messaging channels (WhatsApp, Email, Telegram) via nemohermes. # # Platform-first: all mutations via nemohermes channels commands. # # Usage: # ./scripts/connect/connect-channels.sh [--dry-run|--apply] [--target whatsapp|email|telegram] # ./scripts/connect/connect-channels.sh --help set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Source shared helpers # shellcheck source=../lib/common.sh source "$SCRIPT_DIR/../lib/common.sh" # shellcheck source=../lib/env.sh source "$SCRIPT_DIR/../lib/env.sh" # shellcheck source=../lib/connect_state.sh source "$SCRIPT_DIR/../lib/connect_state.sh" # ── Defaults ─────────────────────────────────────────────────────────────── DRY_RUN=1 APPLY=0 TARGET="" # All available channels ALL_CHANNELS="whatsapp email telegram" # ── Parse args ───────────────────────────────────────────────────────────── while [[ $# -gt 0 ]]; do case "$1" in --help|-h) cat < Connect specific channel (default: all) --help Show this help Safety: --dry-run is the default. Use --apply to perform mutations. Channel changes may require a sandbox rebuild per the NemoClaw runtime matrix. Examples: $(basename "$0") --dry-run # preview all channels $(basename "$0") --target whatsapp --apply # connect WhatsApp only $(basename "$0") --apply # connect all channels EOF exit 0 ;; --dry-run) DRY_RUN=1 APPLY=0 shift ;; --apply) DRY_RUN=0 APPLY=1 shift ;; --target) shift TARGET="${1:-}" if [[ -z "$TARGET" ]]; then log_error "--target requires a value (whatsapp|email|telegram)" exit 1 fi # Validate target valid=0 for ch in $ALL_CHANNELS; do if [[ "$ch" == "$TARGET" ]]; then valid=1 break fi done if [[ $valid -eq 0 ]]; then log_error "Invalid channel: $TARGET" log_error "Valid channels: $ALL_CHANNELS" exit 1 fi shift ;; *) log_error "Unknown argument: $1" exit 1 ;; esac done # ── Load .env ────────────────────────────────────────────────────────────── load_env SANDBOX_NAME="$(get_sandbox_name)" log_section "Channels Connect" # ── Check prerequisites ──────────────────────────────────────────────────── require_cmd nemohermes "Install nemohermes CLI (part of NemoClaw platform)" if ! nemohermes "$SANDBOX_NAME" status &>/dev/null 2>&1; then log_error "Sandbox '$SANDBOX_NAME' not found. Run S4 first." exit 1 fi # ── Determine which channels to process ──────────────────────────────────── if [[ -n "$TARGET" ]]; then CHANNELS_TO_PROCESS="$TARGET" else CHANNELS_TO_PROCESS="$ALL_CHANNELS" fi # ── Channel connect functions ────────────────────────────────────────────── connect_whatsapp() { log_section "WhatsApp Channel" local current_status current_status="$(get_status "whatsapp")" log_info "Current status: ${current_status:-not configured}" if [[ $DRY_RUN -eq 1 ]]; then log_info "[DRY-RUN] Would configure WhatsApp channel." log_info "Steps:" log_info " 1. Owner completes WhatsApp Business API setup in Meta developer console" log_info " 2. Operator runs: nemohermes $SANDBOX_NAME channels add whatsapp --phone-number-id --verify-token " log_info " 3. Sandbox rebuild may be required per runtime matrix" log_info "" log_info "To execute: $(basename "$0") --target whatsapp --apply" return 0 fi # Apply mode: prompt for credentials log_info "WhatsApp channel configuration:" log_info "The owner must first set up WhatsApp Business API in the Meta developer console." log_info "You will need the Phone Number ID and a Verify Token." log_info "" local phone_number_id="" local verify_token="" read -rp "Phone Number ID: " phone_number_id if [[ -z "$phone_number_id" ]]; then log_warn "No Phone Number ID provided. Skipping WhatsApp." set_status "whatsapp" "skipped" "Operator skipped — no Phone Number ID" return 0 fi # Read verify token silently read -rsp "Verify Token: " verify_token echo "" if [[ -z "$verify_token" ]]; then log_warn "No Verify Token provided. Skipping WhatsApp." set_status "whatsapp" "skipped" "Operator skipped — no Verify Token" return 0 fi log_info "Adding WhatsApp channel…" # NOTE: Verify token passed as CLI arg is briefly visible in /proc/*/cmdline. # This is a platform limitation of nemohermes which does not yet support # --from-stdin for channel credentials. if nemohermes "$SANDBOX_NAME" channels add whatsapp \ --phone-number-id "$phone_number_id" \ --verify-token "$verify_token" 2>&1; then log_info "WhatsApp channel added successfully." set_status "whatsapp" "connected" "WhatsApp channel active" else log_warn "WhatsApp channel add failed (may need rebuild)." set_status "whatsapp" "error" "WhatsApp channel add failed — check nemohermes output" fi } connect_email() { log_section "Email Channel" local current_status current_status="$(get_status "email")" log_info "Current status: ${current_status:-not configured}" if [[ $DRY_RUN -eq 1 ]]; then log_info "[DRY-RUN] Would configure Email channel." log_info "Steps:" log_info " 1. Owner provides email address for the assistant" log_info " 2. Operator runs: nemohermes $SANDBOX_NAME channels add email --address " log_info " 3. Sandbox rebuild may be required per runtime matrix" log_info "" log_info "To execute: $(basename "$0") --target email --apply" return 0 fi log_info "Email channel configuration:" log_info "The owner provides the email address the assistant will respond to." log_info "" local email_address="" read -rp "Email address for assistant: " email_address if [[ -z "$email_address" ]]; then log_warn "No email address provided. Skipping Email." set_status "email" "skipped" "Operator skipped — no email address" return 0 fi log_info "Adding Email channel…" if nemohermes "$SANDBOX_NAME" channels add email \ --address "$email_address" 2>&1; then log_info "Email channel added successfully." set_status "email" "connected" "Email channel active" else log_warn "Email channel add failed (may need rebuild)." set_status "email" "error" "Email channel add failed — check nemohermes output" fi } connect_telegram() { log_section "Telegram Channel" local current_status current_status="$(get_status "telegram")" log_info "Current status: ${current_status:-not configured}" if [[ $DRY_RUN -eq 1 ]]; then log_info "[DRY-RUN] Would configure Telegram channel." log_info "Steps:" log_info " 1. Owner creates a bot via @BotFather on Telegram" log_info " 2. BotFather returns a Bot Token" log_info " 3. Operator runs: nemohermes $SANDBOX_NAME channels add telegram --bot-token " log_info " 4. Sandbox rebuild may be required per runtime matrix" log_info "" log_info "To execute: $(basename "$0") --target telegram --apply" return 0 fi log_info "Telegram channel configuration:" log_info "The owner must first create a bot via @BotFather on Telegram." log_info "BotFather returns a Bot Token." log_info "" local bot_token="" read -rsp "Telegram Bot Token: " bot_token echo "" if [[ -z "$bot_token" ]]; then log_warn "No Bot Token provided. Skipping Telegram." set_status "telegram" "skipped" "Operator skipped — no Bot Token" return 0 fi log_info "Adding Telegram channel…" # NOTE: Bot token passed as CLI arg is briefly visible in /proc/*/cmdline. # This is a platform limitation of nemohermes which does not yet support # --from-stdin for channel credentials. if nemohermes "$SANDBOX_NAME" channels add telegram \ --bot-token "$bot_token" 2>&1; then log_info "Telegram channel added successfully." set_status "telegram" "connected" "Telegram channel active" else log_warn "Telegram channel add failed (may need rebuild)." set_status "telegram" "error" "Telegram channel add failed — check nemohermes output" fi } # ── Execute ──────────────────────────────────────────────────────────────── for channel in $CHANNELS_TO_PROCESS; do case "$channel" in whatsapp) connect_whatsapp ;; email) connect_email ;; telegram) connect_telegram ;; *) log_error "Unknown channel: $channel" exit 1 ;; esac done log_info "Channels connect complete."