钉钉 AI 客服:错误处理最佳实践
错误处理是提升用户体验的关键。
一、错误类型
| 错误类型 | 原因 |
|---|---|
| 网络错误 | 连接超时 |
| API 错误 | 限流/故障 |
| 意图错误 | 识别失败 |
| 答案错误 | 知识库缺失 |
二、错误检测
2.1 API 错误
try {
const response = await ai.chat(message);
return response;
} catch (error) {
if (error.code === 'RATE_LIMIT') {
return handleRateLimit();
}
if (error.code === 'TIMEOUT') {
return handleTimeout();
}
return handleUnknownError(error);
}
2.2 意图识别错误
function detectIntentError(result) {
if (result.confidence < 0.5) {
return { error: 'LOW_CONFIDENCE', needsHuman: true };
}
return null;
}
三、错误恢复
3.1 重试机制
async function retryWithBackoff(fn, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
return await fn();
} catch (error) {
if (i === maxRetries - 1) throw error;
await sleep(Math.pow(2, i) * 1000);
}
}
}
3.2 降级策略
async function handleWithFallback(message) {
try {
return await ai.chat(message);
} catch (error) {
// 降级:使用模板回复
return getTemplateResponse(message);
}
}
四、用户提示
4.1 友好提示
❌ 不好的提示:系统错误
✅ 好的提示:抱歉,我暂时无法回答,请稍后再试
4.2 引导用户
function guideUser(error) {
switch (error.type) {
case 'NETWORK':
return '网络似乎不稳定,请稍后再试';
case 'UNKNOWN':
return '抱歉,我没理解您的问题,换个方式说说?';
case 'RATE_LIMIT':
return '服务繁忙,请稍后再试';
default:
return '为您转接人工客服';
}
}
五、错误监控
5.1 错误统计
async function logError(error, context) {
await db.insert('errors', {
type: error.type,
message: error.message,
context: JSON.stringify(context),
timestamp: Date.now()
});
}
5.2 告警规则
async function checkErrorRate() {
const rate = await getErrorRateLastHour();
if (rate > 0.05) {
await sendAlert(`错误率过高:${rate * 100}%`);
}
}
六、测试验证
6.1 错误场景测试
describe('Error Handling', () => {
test('should handle API timeout', async () => {
mockApi.timeout();
const response = await chat('你好');
expect(response).toContain('稍后再试');
});
});
项目地址:GitHub - dingtalk-connector-pro 有问题欢迎 Issue 或评论区交流