moltbook-automator/scripts/reply.sh

144 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
# =============================================================================
# Moltbook Automator - Reply 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"
log() {
echo -e "[$(date '+%Y-%m-%d %H:%M:%S')] [REPLY] $1" | tee -a "$LOG_FILE"
}
# 发送回复
send_reply() {
local post_id="$1"
local author="$2"
local template="$3"
# 替换模板变量
local message="${template//\{author\}/$author}"
log "正在回复帖子 $post_id (作者: $author)..."
local prompt="请帮我在 Moltbook 上回复帖子 ID: $post_id
回复内容: $message
请确认回复已发送。"
clawdbot agent --message "$prompt" --deliver >> "$LOG_FILE" 2>&1
# 更新统计
jq '.today_stats.auto_replies_sent += 1 | .lifetime_stats.total_auto_replies += 1' \
"$STATE_FILE" > "${STATE_FILE}.tmp" && mv "${STATE_FILE}.tmp" "$STATE_FILE"
log "✅ 回复已发送"
}
# 处理提及
handle_mention() {
local post_id="$1"
local author="$2"
local template=$(jq -r '.auto_reply.rules[] | select(.trigger == "mention") | .template' "$CONFIG_FILE")
if [ -n "$template" ] && [ "$template" != "null" ]; then
send_reply "$post_id" "$author" "$template"
fi
}
# 处理私信
handle_dm() {
local dm_id="$1"
local author="$2"
local preview="$3"
local template=$(jq -r '.auto_reply.rules[] | select(.trigger == "dm") | .template' "$CONFIG_FILE")
if [ -n "$template" ] && [ "$template" != "null" ]; then
local message="${template//\{author\}/$author}"
message="${message//\{preview\}/$preview}"
log "收到私信通知: $message"
# 通知用户而不是自动回复私信
local discord_user_id=$(jq -r '.discord_user_id' "$CONFIG_FILE")
clawdbot agent --channel discord --to "$discord_user_id" \
--message "📬 Moltbook 私信通知: $message" --deliver >> "$LOG_FILE" 2>&1
fi
}
# 处理关键词触发
handle_keyword() {
local post_id="$1"
local author="$2"
local content="$3"
# 获取关键词列表
local keywords=$(jq -r '.auto_reply.rules[] | select(.trigger == "keyword") | .keywords[]' "$CONFIG_FILE")
local template=$(jq -r '.auto_reply.rules[] | select(.trigger == "keyword") | .template' "$CONFIG_FILE")
for keyword in $keywords; do
if echo "$content" | grep -qi "$keyword"; then
log "检测到关键词: $keyword"
send_reply "$post_id" "$author" "$template"
return
fi
done
}
# 使用说明
usage() {
echo "用法: $0 <command> [args...]"
echo ""
echo "命令:"
echo " mention <post_id> <author> 处理提及"
echo " dm <dm_id> <author> <preview> 处理私信"
echo " keyword <post_id> <author> <content> 处理关键词"
echo ""
echo "示例:"
echo " $0 mention 12345 alice"
echo " $0 dm 67890 bob '你好,请问...'"
}
# 主函数
main() {
local command="$1"
shift
# 检查自动回复是否启用
local enabled=$(jq -r '.auto_reply.enabled' "$CONFIG_FILE")
if [ "$enabled" != "true" ]; then
log "⚠️ 自动回复未启用 (config.json -> auto_reply.enabled = false)"
exit 0
fi
case "$command" in
mention)
handle_mention "$@"
;;
dm)
handle_dm "$@"
;;
keyword)
handle_keyword "$@"
;;
*)
usage
exit 1
;;
esac
}
if [ $# -eq 0 ]; then
usage
exit 1
fi
main "$@"