Auto backup: 2026-02-21 07:01

This commit is contained in:
Krilly
2026-02-21 07:01:51 +00:00
parent 8757148122
commit 17b5b82d99
2012 changed files with 352552 additions and 331 deletions

View File

@@ -1,51 +1,101 @@
#!/bin/bash
# Check Krilly's AgentMail inbox and alert Anthony of new emails
# SILENT MODE: Only outputs anything if there are new emails to report
# 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 -e
set -euo pipefail
INBOX="krilly@agentmail.to"
API_KEY="am_us_22a6a04a84144467993d5b90be8bbd5d1482ca615a4e17561682dd3d6831f932"
LAST_CHECK_FILE="/tmp/krilly-last-email-check"
WORKDIR="/home/openclaw/.openclaw/workspace"
IMAP_TOOL="$WORKDIR/skills/imap-smtp-email/scripts/imap.js"
# Get current timestamp
CURRENT_TIME=$(date -u +"%Y-%m-%dT%H:%M:%S.000Z")
# Get last check time (default to 1 hour ago if file doesn't exist)
if [[ -f "$LAST_CHECK_FILE" ]]; then
LAST_CHECK=$(cat "$LAST_CHECK_FILE")
else
LAST_CHECK=$(date -u -d '1 hour ago' +"%Y-%m-%dT%H:%M:%S.000Z")
# Load Telegram bot settings (shared with other automations)
if [[ -f "$WORKDIR/.env" ]]; then
# shellcheck disable=SC1090
source "$WORKDIR/.env"
fi
# Get messages since last check
MESSAGES=$(curl -s -H "Authorization: Bearer $API_KEY" \
"https://api.agentmail.to/v0/inboxes/$INBOX/messages?limit=20&after=$LAST_CHECK")
send_ntfy() {
local title="$1"
local body="$2"
local priority="${3:-4}" # 1-5 or low|default|high|urgent
local sound="${4:-default}"
# Simple check for new messages (look for messages without "sent" label)
if echo "$MESSAGES" | grep -q '"messages":\s*\[\s*\]'; then
NEW_COUNT=0
elif echo "$MESSAGES" | grep -v '"sent"' | grep -q '"message_id"'; then
NEW_COUNT=$(echo "$MESSAGES" | grep -o '"message_id"' | grep -v -A5 -B5 '"sent"' | wc -l)
if [[ "$NEW_COUNT" -lt 1 ]]; then
NEW_COUNT=1 # At least one if we found messages
[[ -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
NEW_COUNT=0
echo "ERROR: TELEGRAM_BOT_TOKEN/TELEGRAM_CHAT not set; cannot notify" >&2
fi
# SILENT: Only output and notify if there are new emails
if [[ "$NEW_COUNT" -gt 0 ]]; then
# Send Telegram notification
MESSAGE="🦀 **Krilly received $NEW_COUNT new email(s)!**
# Send via ntfy (push)
send_ntfy "🦀 New email" "New unread email(s): $unread_count\n\n$preview" "4" "default"
📧 Check full inbox: https://sendclaw.com/dashboard"
/home/openclaw/.npm-global/bin/openclaw message send \
--channel telegram \
--target "telegram:1793951355" \
--message "$MESSAGE" 2>/dev/null || true
fi
# Always update last check time (silently)
echo "$CURRENT_TIME" > "$LAST_CHECK_FILE"
echo "$newest_uid" > "$STATE_FILE"