102 lines
3.1 KiB
Bash
Executable File
102 lines
3.1 KiB
Bash
Executable File
#!/bin/bash
|
|
# Check Anthony's Gmail inbox (IMAP) and alert Anthony of new unread emails.
|
|
#
|
|
# SILENT MODE: only sends a Telegram notification if there are NEW unread emails
|
|
# since the last run.
|
|
|
|
set -euo pipefail
|
|
|
|
WORKDIR="/home/openclaw/.openclaw/workspace"
|
|
IMAP_TOOL="$WORKDIR/skills/imap-smtp-email/scripts/imap.js"
|
|
|
|
# Load Telegram bot settings (shared with other automations)
|
|
if [[ -f "$WORKDIR/.env" ]]; then
|
|
# shellcheck disable=SC1090
|
|
source "$WORKDIR/.env"
|
|
fi
|
|
|
|
send_ntfy() {
|
|
local title="$1"
|
|
local body="$2"
|
|
local priority="${3:-4}" # 1-5 or low|default|high|urgent
|
|
local sound="${4:-default}"
|
|
|
|
[[ -z "${NTFY_URL:-}" ]] && return 0
|
|
[[ -z "${NTFY_TOPIC:-}" ]] && return 0
|
|
|
|
# Enforce minimum priority (default 4)
|
|
local minp="${NTFY_MIN_PRIORITY:-4}"
|
|
if [[ "$priority" =~ ^[0-9]+$ ]] && [[ "$minp" =~ ^[0-9]+$ ]]; then
|
|
if (( priority < minp )); then
|
|
priority="$minp"
|
|
fi
|
|
fi
|
|
|
|
curl -s -X POST "${NTFY_URL%/}/$NTFY_TOPIC" \
|
|
-H "Title: $title" \
|
|
-H "Priority: $priority" \
|
|
-H "Sound: $sound" \
|
|
-d "$body" >/dev/null 2>&1 || true
|
|
}
|
|
|
|
# Track the newest unread UID we've already notified about to avoid repeat pings.
|
|
STATE_FILE="/tmp/krilly-gmail-last-notified-uid"
|
|
|
|
# How far back to look on each run.
|
|
RECENT_WINDOW="30m"
|
|
LIMIT="10"
|
|
|
|
result_json=$(node "$IMAP_TOOL" check --unseen true --recent "$RECENT_WINDOW" --limit "$LIMIT" 2>/dev/null || true)
|
|
|
|
# If IMAP tool errored, stay silent (cron will still have logs if needed).
|
|
if [[ -z "$result_json" ]]; then
|
|
exit 0
|
|
fi
|
|
|
|
unread_count=$(echo "$result_json" | jq 'length' 2>/dev/null || echo 0)
|
|
if [[ "$unread_count" -lt 1 ]]; then
|
|
exit 0
|
|
fi
|
|
|
|
newest_uid=$(echo "$result_json" | jq -r 'map(.uid // 0) | max' 2>/dev/null || echo 0)
|
|
last_uid=0
|
|
if [[ -f "$STATE_FILE" ]]; then
|
|
last_uid=$(cat "$STATE_FILE" 2>/dev/null || echo 0)
|
|
fi
|
|
|
|
if [[ "$newest_uid" -le "$last_uid" ]]; then
|
|
# Nothing newer than what we've already notified.
|
|
exit 0
|
|
fi
|
|
|
|
# Build a short preview list (subject + from). In this tool, `from` is usually a
|
|
# single string like: "Name" <email@domain>
|
|
preview=$(echo "$result_json" | jq -r '.[:5][] | "• \(.from // "unknown") — \(.subject // "(no subject)")"' 2>/dev/null)
|
|
|
|
MESSAGE=$(cat <<EOF
|
|
🦀 New unread email(s) in Gmail: *$unread_count*
|
|
|
|
$preview
|
|
EOF
|
|
)
|
|
|
|
# Send via Telegram Bot API directly (more reliable than routing through CLI)
|
|
if [[ -n "${TELEGRAM_BOT_TOKEN:-}" && -n "${TELEGRAM_CHAT:-}" ]]; then
|
|
tg_payload=$(jq -nc --arg chat_id "$TELEGRAM_CHAT" --arg text "$MESSAGE" '{chat_id:$chat_id,text:$text,parse_mode:"Markdown",disable_web_page_preview:true}')
|
|
tg_resp=$(curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
|
|
-H "Content-Type: application/json" \
|
|
-d "$tg_payload" || true)
|
|
|
|
if ! echo "$tg_resp" | jq -e '.ok == true' >/dev/null 2>&1; then
|
|
# Stay non-fatal (cron should not spam errors), but log for debugging
|
|
echo "ERROR: Telegram send failed: $tg_resp" >&2
|
|
fi
|
|
else
|
|
echo "ERROR: TELEGRAM_BOT_TOKEN/TELEGRAM_CHAT not set; cannot notify" >&2
|
|
fi
|
|
|
|
# Send via ntfy (push)
|
|
send_ntfy "🦀 New email" "New unread email(s): $unread_count\n\n$preview" "4" "default"
|
|
|
|
echo "$newest_uid" > "$STATE_FILE"
|