moltbook-automator/scripts/check.sh

141 lines
3.9 KiB
Bash
Executable File

#!/bin/bash
# =============================================================================
# Moltbook Automator - Check Script
# 检查 Moltbook 新消息、通知、私信,并根据配置执行相应操作
# =============================================================================
set -e
SKILL_DIR="$HOME/.clawdbot/skills/moltbook-automator"
CONFIG_FILE="$SKILL_DIR/config.json"
STATE_FILE="$SKILL_DIR/state.json"
LOG_FILE="$HOME/.clawdbot/moltbook-automator.log"
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
log() {
echo -e "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
# 检查依赖
check_dependencies() {
if ! command -v jq &> /dev/null; then
log "${RED}错误: 需要安装 jq${NC}"
log "运行: brew install jq"
exit 1
fi
if ! pgrep -f "clawdbot-gateway" > /dev/null; then
log "${YELLOW}警告: Clawdbot Gateway 未运行${NC}"
log "尝试启动 Gateway..."
~/.clawdbot/start-gateway.sh &
sleep 5
fi
}
# 读取配置
load_config() {
if [ ! -f "$CONFIG_FILE" ]; then
log "${RED}错误: 配置文件不存在: $CONFIG_FILE${NC}"
exit 1
fi
DISCORD_USER_ID=$(jq -r '.discord_user_id' "$CONFIG_FILE")
NOTIFICATION_CHANNEL=$(jq -r '.notification_channels[0]' "$CONFIG_FILE")
AUTO_REPLY_ENABLED=$(jq -r '.auto_reply.enabled' "$CONFIG_FILE")
}
# 读取状态
load_state() {
if [ ! -f "$STATE_FILE" ]; then
echo '{"last_check_at":null,"today_stats":{"posts_checked":0}}' > "$STATE_FILE"
fi
LAST_CHECK=$(jq -r '.last_check_at // "从未"' "$STATE_FILE")
LAST_POST_ID=$(jq -r '.last_seen_post_id // "null"' "$STATE_FILE")
}
# 更新状态
update_state() {
local now=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
local today=$(date +"%Y-%m-%d")
# 更新最后检查时间
jq --arg now "$now" --arg today "$today" '
.last_check_at = $now |
.lifetime_stats.total_checks += 1 |
if .today_stats.date != $today then
.today_stats = {
"date": $today,
"posts_checked": 0,
"notifications_processed": 0,
"dms_processed": 0,
"auto_replies_sent": 0,
"likes_given": 0
}
else
.
end
' "$STATE_FILE" > "${STATE_FILE}.tmp" && mv "${STATE_FILE}.tmp" "$STATE_FILE"
}
# 执行检查
do_check() {
log "${GREEN}🔍 开始检查 Moltbook...${NC}"
local prompt="请帮我检查 Moltbook:
1. 查看我的通知 (Notifications) - 有没有人 @ 我或回复我
2. 查看我的私信 (DMs) - 有没有新消息
3. 浏览我的主页动态 (Feed) - 有什么有趣的帖子
请汇报:
- 新通知数量及内容摘要
- 新私信数量及发送者
- 值得注意的帖子 (如果有)
如果一切正常,简短报告即可。"
# 根据配置选择通知渠道
case "$NOTIFICATION_CHANNEL" in
discord)
clawdbot agent --channel discord --to "$DISCORD_USER_ID" --message "$prompt" --deliver >> "$LOG_FILE" 2>&1
;;
telegram)
clawdbot agent --channel telegram --message "$prompt" --deliver >> "$LOG_FILE" 2>&1
;;
*)
clawdbot agent --message "$prompt" >> "$LOG_FILE" 2>&1
;;
esac
log "${GREEN}✅ 检查完成${NC}"
}
# 主函数
main() {
log "=========================================="
log "🦞 Moltbook Automator v1.0.0"
log "=========================================="
check_dependencies
load_config
load_state
log "上次检查: $LAST_CHECK"
log "通知渠道: $NOTIFICATION_CHANNEL"
log "自动回复: $AUTO_REPLY_ENABLED"
do_check
update_state
log "下次检查将在配置的间隔后进行"
log "=========================================="
}
# 执行
main "$@"