mcpField
A
auto-reply

auto-reply

github.com/alsk1992生产力3.0k70 次安装7 周前更新
代码仓库

关于 auto-reply

auto-reply 是用于创建和管理自动回复的 TypeScript 库及命令集,可按消息匹配规则触发回复,并支持条件、时间设置和模板变量。

主要功能

  1. 01关键词、精确、前缀、后缀及正则匹配
  2. 02按时间、日期、用户、频道和角色设定条件
  3. 03优先级、时段和冷却时间设置
  4. 04动态回复变量
  5. 05规则测试与回复模拟

适用场景

  1. 01自动回复常见关键词消息
  2. 02按用户、频道或时段限制回复
  3. 03启用规则前预览匹配结果

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/alsk1992/cloddsbot --skill auto-reply

安装后在 Claude Code 中输入/auto-reply确认已加载。

附带文件index.ts

详细介绍

自动回复 - 完整 API 参考

根据模式、关键词和条件创建自动回复规则。


聊天命令

列出规则
/autoreply list                             List all rules
/autoreply active                           Show active rules only
/autoreply stats                            Rule statistics
创建规则
/autoreply add <pattern> <response>         Simple keyword match
/autoreply add-regex <regex> <response>     Regex pattern
/autoreply add-keywords <kw1,kw2> <resp>    Keyword rule
管理规则
/autoreply enable <id>                      Enable rule
/autoreply disable <id>                     Disable rule
/autoreply remove <id>                      Remove rule
/autoreply edit <id> <new-response>         Update response
/autoreply get <id>                         Rule details
测试
/autoreply test <message>                   Test which rules match
/autoreply simulate <message>               Preview response
高级
/autoreply cooldown <id> <seconds>          Set cooldown
/autoreply schedule <id> <start-end>        Active hours (e.g. 9-17)
/autoreply priority <id> <number>           Set priority (higher first)
/autoreply channel <id> <channel>           Restrict to channel
/autoreply clear-cooldowns                  Clear all cooldowns
/autoreply reload                           Reload rules from disk

TypeScript API 参考

创建自动回复管理器
import { createAutoReplyManager } from 'clodds/auto-reply';

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

  // Defaults
  defaultCooldownMs: 0,
  defaultPriority: 0,

  // Limits
  maxRulesPerUser: 100,
  maxResponseLength: 2000,
});
添加简单规则
// Keyword match
await autoReply.addRule({
  name: 'greeting',
  pattern: {
    type: 'keyword',
    value: 'hello',
    caseSensitive: false,
  },
  response: 'Hi there! How can I help?',
});
添加正则表达式规则
// Regex pattern
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}`;
  },
});
添加条件规则
// With conditions
await autoReply.addRule({
  name: 'trading-hours',
  pattern: {
    type: 'keyword',
    value: 'trade',
  },
  conditions: [
    // Only during market hours
    {
      type: 'time',
      start: '09:30',
      end: '16:00',
      timezone: 'America/New_York',
    },
    // Only on weekdays
    {
      type: 'day',
      days: ['mon', 'tue', 'wed', 'thu', 'fri'],
    },
    // Only for certain users
    {
      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.',
});
添加冷却时间
// Prevent spam
await autoReply.addRule({
  name: 'faq',
  pattern: {
    type: 'keyword',
    value: 'faq',
  },
  response: 'Check our FAQ at https://...',
  cooldown: {
    perUser: 60000,    // 60s per user
    perChannel: 10000, // 10s per channel
    global: 5000,      // 5s global
  },
});
动态回复
// Response with variables
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(),
  },
});

// Response with API call
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}`);
}
测试规则
// Test which rules would match
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');

模式类型

类型示例描述
keywordhello包含关键词
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. 监控触发情况 — 检查规则效果

评论

支持 Markdown