自动回复管理 auto-reply

自动回复管理是一个用于创建和管理基于关键词、模式和条件的自动回复规则的系统,适用于聊天机器人和自动化客户服务,支持正则表达式、动态回复和优先级设置,以提高响应效率和准确性。

AI应用 0 次安装 0 次浏览 更新于 3/5/2026

自动回复 - 完整API参考

创建基于模式、关键词和条件的自动回复规则。


聊天命令

列出规则

/autoreply list                             列出所有规则
/autoreply active                           只显示激活的规则
/autoreply stats                            规则统计

创建规则

/autoreply add <pattern> <response>         简单关键词匹配
/autoreply add-regex <regex> <response>     正则表达式模式
/autoreply add-keywords <kw1,kw2> <resp>     关键词规则

管理规则

/autoreply enable <id>                      启用规则
/autoreply disable <id>                     禁用规则
/autoreply remove <id>                      删除规则
/autoreply edit <id> <new-response>         更新回复
/autoreply get <id>                         规则详情

测试

/autoreply test <message>                   测试哪些规则匹配
/autoreply simulate <message>               预览回复

高级

/autoreply cooldown <id> <seconds>           设置冷却时间
/autoreply schedule <id> <start-end>         激活时间(例如 9-17)
/autoreply priority <id> <number>            设置优先级(数值越大优先级越高)
/autoreply channel <id> <channel>           限制特定频道
/autoreply clear-cooldowns                  清除所有冷却时间
/autoreply reload                           从磁盘重新加载规则

TypeScript API参考

创建自动回复管理器

import { createAutoReplyManager } from 'clodds/auto-reply';

const autoReply = createAutoReplyManager({
  // 存储
  storage: 'sqlite',
  dbPath: './auto-reply.db',

  // 默认值
  defaultCooldownMs: 0,
  defaultPriority: 0,

  // 限制
  maxRulesPerUser: 100,
  maxResponseLength: 2000,
});

添加简单规则

// 关键词匹配
await autoReply.addRule({
  name: 'greeting',
  pattern: {
    type: 'keyword',
    value: 'hello',
    caseSensitive: false,
  },
  response: 'Hi there! How can I help?',
});

添加正则规则

// 正则表达式模式
await autoReply.addRule({
  name: 'price-query',
  pattern: {
    type: 'regex',
    value: /price\s+(btc|eth|sol)/i,
  },
  response: async (match, ctx) => {
    const symbol = match[1].toUpperCase();
    const price = await getPrice(symbol);
    return `${symbol} price: $${price}`;
  },
});

添加条件规则

// 带条件
await autoReply.addRule({
  name: 'trading-hours',
  pattern: {
    type: 'keyword',
    value: 'trade',
  },
  conditions: [
    // 仅在市场交易时间内
    {
      type: 'time',
      start: '09:30',
      end: '16:00',
      timezone: 'America/New_York',
    },
    // 仅在工作日
    {
      type: 'day',
      days: ['mon', 'tue', 'wed', 'thu', 'fri'],
    },
    // 仅针对特定用户
    {
      type: 'user',
      userIds: ['user-123', 'user-456'],
    },
  ],
  response: 'Markets are open! What would you like to trade?',
  elseResponse: 'Markets are closed. Try again during trading hours.',
});

添加冷却时间

// 防止滥用
await autoReply.addRule({
  name: 'faq',
  pattern: {
    type: 'keyword',
    value: 'faq',
  },
  response: 'Check our FAQ at https://...',
  cooldown: {
    perUser: 60000,    // 每个用户60秒
    perChannel: 10000, // 每个频道10秒
    global: 5000,      // 全局5秒
  },
});

动态回复

// 带变量的回复
await autoReply.addRule({
  name: 'welcome',
  pattern: {
    type: 'exact',
    value: '!welcome',
  },
  response: 'Welcome {{user.name}}! You joined {{user.joinDate}}.',
  variables: {
    'user.name': (ctx) => ctx.user.displayName,
    'user.joinDate': (ctx) => ctx.user.createdAt.toDateString(),
  },
});

// 带API调用的回复
await autoReply.addRule({
  name: 'portfolio',
  pattern: {
    type: 'keyword',
    value: 'portfolio',
  },
  response: async (match, ctx) => {
    const portfolio = await getPortfolio(ctx.user.id);
    return `Your portfolio: $${portfolio.totalValue.toFixed(2)}`;
  },
});

列出规则

const rules = await autoReply.listRules();

for (const rule of rules) {
  console.log(`${rule.id}: ${rule.name}`);
  console.log(`  Pattern: ${rule.pattern.value}`);
  console.log(`  Enabled: ${rule.enabled}`);
  console.log(`  Triggers: ${rule.triggerCount}`);
}

测试规则

// 测试哪些规则会匹配
const matches = await autoReply.test('hello world', {
  userId: 'user-123',
  channelId: 'telegram-456',
});

for (const match of matches) {
  console.log(`Rule: ${match.rule.name}`);
  console.log(`Response: ${match.response}`);
}

启用/禁用

await autoReply.enable('rule-id');
await autoReply.disable('rule-id');

删除规则

await autoReply.deleteRule('rule-id');

模式类型

类型 示例 描述
keyword hello 包含关键词
exact !help 仅完全匹配
regex /price\s+\w+/i 正则表达式
startsWith ! 以前缀开头
endsWith ? 以后缀结尾

条件类型

类型 描述
time 在时间窗口内激活
day 在特定日子激活
user 仅对特定用户
channel 仅在特定频道
role 仅对有角色的用户
custom 自定义函数

响应变量

变量 描述
{{user.name}} 用户显示名称
{{user.id}} 用户ID
{{channel.name}} 频道名称
{{match[0]}} 完整的正则匹配
{{match[1]}} 第一个捕获组
{{date}} 当前日期
{{time}} 当前时间

最佳实践

  1. 使用优先级 — 重要规则优先
  2. 设置冷却时间 — 防止滥用
  3. 测试模式 — 在启用前验证
  4. 使用条件 — 上下文感知的回复
  5. 监控触发器 — 检查规则的有效性