name: 自动回复
description: “自动响应规则、模式和计划消息”
emoji: “🤖”
自动回复 - 完整 API 参考
基于模式、关键词和条件创建自动响应规则。
聊天命令
列出规则
/auto-reply 列出所有规则
/auto-reply active 仅显示活动规则
/auto-reply stats 规则触发统计
创建规则
/auto-reply add "hello" "Hi there!" 简单关键词匹配
/auto-reply add /price.*btc/i "BTC: $X" 正则表达式模式
/auto-reply add --exact "!help" "..." 仅完全匹配
管理规则
/auto-reply enable <id> 启用规则
/auto-reply disable <id> 禁用规则
/auto-reply delete <id> 删除规则
/auto-reply edit <id> response "new text" 更新响应文本
测试
/auto-reply test "hello world" 测试哪些规则匹配
/auto-reply simulate "price btc" 预览响应
高级功能
/auto-reply cooldown <id> 60 设置 60 秒冷却时间
/auto-reply schedule <id> 9-17 仅在 9am-5pm 活动
/auto-reply priority <id> 10 设置优先级(高者优先)
/auto-reply channel <id> telegram 限制到特定频道
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}} |
当前时间 |
最佳实践
- 使用优先级 — 重要规则优先
- 设置冷却时间 — 防止垃圾消息
- 测试模式 — 启用前验证
- 使用条件 — 上下文感知响应
- 监控触发器 — 检查规则效果