85 lines
2.7 KiB
Bash
Executable File
85 lines
2.7 KiB
Bash
Executable File
#!/bin/bash
|
|
# Check for new emails in krilly@agentmail.to and alert Anthony
|
|
# For krillyclaw@gmail.com: requires Gmail app password from user
|
|
|
|
WORKSPACE_DIR="/home/openclaw/.openclaw/workspace"
|
|
STATE_FILE="$WORKSPACE_DIR/memory/.agentmail-email-state.json"
|
|
API_KEY="am_us_22a6a04a84144467993d5b90be8bbd5d1482ca615a4e17561682dd3d6831f932"
|
|
|
|
# Ensure state directory exists
|
|
mkdir -p "$(dirname "$STATE_FILE")"
|
|
|
|
# Initialize state if needed
|
|
if [ ! -f "$STATE_FILE" ]; then
|
|
echo '{"last_message_id": "", "last_check": 0}' > "$STATE_FILE"
|
|
fi
|
|
|
|
# Get last seen message ID
|
|
LAST_MSG_ID=$(jq -r '.last_message_id // ""' "$STATE_FILE")
|
|
|
|
# Check for messages using agentmail API
|
|
MESSAGES=$(AGENTMAIL_API_KEY="$API_KEY" python3 -c "
|
|
from agentmail import AgentMail
|
|
import json
|
|
import os
|
|
|
|
client = AgentMail(api_key=os.environ.get('AGENTMAIL_API_KEY'))
|
|
msgs = client.inboxes.messages.list(inbox_id='krilly@agentmail.to', limit=10)
|
|
print(json.dumps([{'id': m.message_id, 'from': m.from_, 'subject': m.subject, 'labels': m.labels} for m in msgs.messages]))
|
|
" 2>&1)
|
|
|
|
if [ $? -ne 0 ]; then
|
|
echo "Error checking agentmail: $MESSAGES" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Parse messages
|
|
NEW_COUNT=0
|
|
NEWEST_ID="$LAST_MSG_ID"
|
|
ALERT_LINES=()
|
|
FOUND_LAST=false
|
|
|
|
while IFS= read -r MSG; do
|
|
MSG_ID=$(echo "$MSG" | jq -r '.id')
|
|
FROM=$(echo "$MSG" | jq -r '.from')
|
|
SUBJECT=$(echo "$MSG" | jq -r '.subject')
|
|
LABELS=$(echo "$MSG" | jq -r '.labels | join(",")')
|
|
|
|
# Stop if we hit the last seen message
|
|
if [ "$MSG_ID" = "$LAST_MSG_ID" ]; then
|
|
FOUND_LAST=true
|
|
break
|
|
fi
|
|
|
|
# Only alert for unread messages
|
|
if echo "$LABELS" | grep -q "unread"; then
|
|
NEW_COUNT=$((NEW_COUNT + 1))
|
|
NEWEST_ID="$MSG_ID"
|
|
|
|
# Don't include our own sent messages
|
|
if ! echo "$FROM" | grep -q "krilly@"; then
|
|
ALERT_LINES+=("• **$SUBJECT**\n From: $FROM")
|
|
fi
|
|
fi
|
|
done < <(echo "$MESSAGES" | jq -c '.[]')
|
|
|
|
# Update state
|
|
jq ".last_message_id = \"$NEWEST_ID\" | .last_check = $(date +%s)" "$STATE_FILE" > "$STATE_FILE.tmp" && mv "$STATE_FILE.tmp" "$STATE_FILE"
|
|
|
|
# Send alert if new messages found
|
|
if [ "$NEW_COUNT" -gt 0 ] && [ ${#ALERT_LINES[@]} -gt 0 ]; then
|
|
ALERT_MSG="📬 **$NEW_COUNT new email(s) in krilly@agentmail.to**:\n\n"
|
|
for LINE in "${ALERT_LINES[@]}"; do
|
|
ALERT_MSG+="$LINE\n\n"
|
|
done
|
|
ALERT_MSG+="— Krilly 🦀"
|
|
|
|
# Send via Telegram using gateway
|
|
curl -s -X POST "http://127.0.0.1:18789/api/message/send" \
|
|
-H "Content-Type: application/json" \
|
|
-d "{\"channel\": \"telegram\", \"to\": \"telegram:1793951355\", \"message\": $(echo "$ALERT_MSG" | jq -Rs .)}" \
|
|
> /dev/null 2>&1
|
|
|
|
echo "Alert sent for $NEW_COUNT new messages"
|
|
fi
|