garc-gws-agent-runtime/lib/send.sh
林 駿甫 (Shunsuke Hayashi) 7b5951a1d5 fix: resolve all 17 playbook findings (P0–P3)
P0 fixes:
- agent register: upsert by agent_id (no duplicate rows)
- daemon poll-once: extract _gmail_poll_cycle, run synchronously
- garc_core.py: suppress urllib3/googleapiclient DeprecationWarnings

P1 fixes:
- OAuth: detect RefreshError → delete stale token → re-auth flow
- OAuth: scope coverage check before returning valid creds
- ingress: add stale-reset subcommand (reset in_progress > N min)
- sheets: trim-sheet / clean-all — deleteDimension for empty rows
- approval gate: send Gmail notification to GARC_APPROVAL_EMAIL

P2 additions:
- Google Chat: garc-chat-helper.py + garc send chat subcommands
- Service Account: garc auth service-account verify + DWD support
- Audit log: Sheets audit tab + garc audit list + bin/garc async hook
- garc auth revoke: POST /revoke + delete token file
- kg: pagination fix, shell injection fix, garc-kg-query.py
- docs: _doc_insert_text / append_doc / garc drive append-doc

P3 additions:
- Multi-tenant: lib/profile.sh (list/use/add/show/remove/current)
  bin/garc: auto-load profile config.env and token.json
- Google Forms pipeline: garc-forms-helper.py + lib/forms.sh
  garc forms list/responses/watch
- systemd: _daemon_install_service OS-detect → launchd or systemd units
- Python version gate (>=3.10) in bin/garc + pyproject.toml
- garc doctor command for environment diagnostics

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-15 09:55:33 +09:00

140 lines
3.6 KiB
Bash

#!/usr/bin/env bash
# GARC send.sh — Gmail / Google Chat message sending
# Replaces Lark IM with Gmail or Google Chat
garc_send() {
local subcommand="${1:-}"
# Support 'garc send chat ...' / 'garc send email ...' sub-dispatching
case "${subcommand}" in
chat)
shift
_garc_send_chat_cmd "$@"
return $?
;;
email)
shift
;; # fall through to default Gmail path
esac
local message=""
local to="${GARC_GMAIL_DEFAULT_TO:-}"
local subject="GARC Agent Notification"
local use_chat=false
local space_id="${GARC_CHAT_SPACE_ID:-}"
while [[ $# -gt 0 ]]; do
case "$1" in
--to) to="$2"; shift 2 ;;
--subject|-s) subject="$2"; shift 2 ;;
--chat) use_chat=true; shift ;;
--space) space_id="$2"; shift 2 ;;
*) message="$1"; shift ;;
esac
done
if [[ -z "${message}" ]]; then
cat <<EOF
Usage:
garc send "<message>" [--to <email>] [--subject <s>] # Gmail
garc send --chat "<message>" [--space <space_id>] # Chat
garc send chat <subcommand> # Chat management
chat send "<message>" [--space <id>] [--thread <key>]
chat list-spaces
chat list-messages [--space <id>] [--max N]
EOF
return 1
fi
if [[ "${use_chat}" == "true" ]]; then
_garc_send_chat "${message}" "${space_id}"
else
_garc_send_gmail "${message}" "${to}" "${subject}"
fi
}
_garc_send_chat_cmd() {
local sub="${1:-send}"
shift || true
local message="" space_id="${GARC_CHAT_SPACE_ID:-}" thread_key="" max=25
case "${sub}" in
list-spaces)
python3 "${GARC_DIR}/scripts/garc-chat-helper.py" list-spaces
return $?
;;
list-messages)
while [[ $# -gt 0 ]]; do
case "$1" in
--space) space_id="$2"; shift 2 ;;
--max) max="$2"; shift 2 ;;
*) shift ;;
esac
done
[[ -z "${space_id}" ]] && { echo "Error: --space required"; return 1; }
python3 "${GARC_DIR}/scripts/garc-chat-helper.py" list-messages \
--space-id "${space_id}" --max "${max}"
return $?
;;
send|*)
while [[ $# -gt 0 ]]; do
case "$1" in
--space) space_id="$2"; shift 2 ;;
--thread) thread_key="$2"; shift 2 ;;
*) message="${message:+${message} }$1"; shift ;;
esac
done
[[ -z "${message}" ]] && { echo "Usage: garc send chat send \"<message>\" [--space <id>]"; return 1; }
_garc_send_chat "${message}" "${space_id}" "${thread_key}"
return $?
;;
esac
}
_garc_send_gmail() {
local message="$1"
local to="$2"
local subject="$3"
if [[ -z "${to}" ]]; then
echo "Error: No recipient. Set GARC_GMAIL_DEFAULT_TO or use --to <email>" >&2
return 1
fi
echo "Sending Gmail to ${to}..."
echo "Subject: ${subject}"
echo "Message: ${message}"
if [[ "${DRY_RUN:-false}" == "true" ]]; then
echo "[dry-run] Would send Gmail to ${to}"
return 0
fi
python3 "${GARC_DIR}/scripts/garc-gmail-helper.py" send \
--to "${to}" \
--subject "${subject}" \
--body "${message}"
}
_garc_send_chat() {
local message="$1"
local space_id="${2:-${GARC_CHAT_SPACE_ID:-}}"
local thread_key="${3:-}"
if [[ -z "${space_id}" ]]; then
echo "Error: No Chat space. Set GARC_CHAT_SPACE_ID or use --space <space_id>" >&2
return 1
fi
echo "Sending Google Chat message to space ${space_id}..."
if [[ "${DRY_RUN:-false}" == "true" ]]; then
echo "[dry-run] Would send Chat message to ${space_id}"
return 0
fi
python3 "${GARC_DIR}/scripts/garc-chat-helper.py" send \
--space-id "${space_id}" \
--message "${message}" \
${thread_key:+--thread-key "${thread_key}"}
}