fix: 다른 사용자 무응답 버그 수정

- getOrCreateUser를 별도 try-catch로 감싸서 DB 오류 시 에러 메시지 전송
- 전체 메시지 처리 로직을 try-catch로 감싸서 모든 오류에 대해 사용자 응답 보장
- 기존: DB 오류 발생 시 webhook handler catch → 500 반환 (사용자 무응답)
- 변경: 오류 발생 시에도 "일시적인 오류" 메시지 전송

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
kappa
2026-01-19 09:45:25 +09:00
parent 2e4886a0a7
commit a1eaae3c04
2 changed files with 63 additions and 29 deletions

View File

@@ -62,38 +62,49 @@ async function handleMessage(
return;
}
// 사용자 처리
const userId = await getOrCreateUser(
env.DB,
telegramUserId,
message.from.first_name,
message.from.username
);
// 사용자 처리 (오류 시 사용자에게 알림)
let userId: number;
try {
userId = await getOrCreateUser(
env.DB,
telegramUserId,
message.from.first_name,
message.from.username
);
} catch (dbError) {
console.error('[handleMessage] 사용자 DB 오류:', dbError);
await sendMessage(
env.BOT_TOKEN,
chatId,
'⚠️ 일시적인 오류가 발생했습니다. 잠시 후 다시 시도해주세요.'
);
return;
}
let responseText: string;
// 명령어 처리
if (text.startsWith('/')) {
const [command, ...argParts] = text.split(' ');
const args = argParts.join(' ');
responseText = await handleCommand(env, userId, chatIdStr, command, args);
try {
// 명령어 처리
if (text.startsWith('/')) {
const [command, ...argParts] = text.split(' ');
const args = argParts.join(' ');
responseText = await handleCommand(env, userId, chatIdStr, command, args);
// /start 명령어는 미니앱 버튼과 함께 전송
if (command === '/start') {
await sendMessageWithKeyboard(env.BOT_TOKEN, chatId, responseText, [
[{ text: '🌐 서비스 보기', web_app: { url: 'https://hosting.anvil.it.com' } }],
[{ text: '💬 문의하기', url: 'https://t.me/AnvilForgeBot' }],
]);
return;
}
} else {
// 타이핑 표시
await sendChatAction(env.BOT_TOKEN, chatId, 'typing');
// /start 명령어는 미니앱 버튼과 함께 전송
if (command === '/start') {
await sendMessageWithKeyboard(env.BOT_TOKEN, chatId, responseText, [
[{ text: '🌐 서비스 보기', web_app: { url: 'https://hosting.anvil.it.com' } }],
[{ text: '💬 문의하기', url: 'https://t.me/AnvilForgeBot' }],
]);
return;
}
} else {
// 타이핑 표시
await sendChatAction(env.BOT_TOKEN, chatId, 'typing');
// 1. 사용자 메시지 버퍼에 추가
await addToBuffer(env.DB, userId, chatIdStr, 'user', text);
// 1. 사용자 메시지 버퍼에 추가
await addToBuffer(env.DB, userId, chatIdStr, 'user', text);
try {
// 2. AI 응답 생성
responseText = await generateAIResponse(env, userId, chatIdStr, text, telegramUserId);
@@ -106,10 +117,10 @@ async function handleMessage(
if (summarized) {
responseText += '\n\n<i>👤 프로필이 업데이트되었습니다.</i>';
}
} catch (error) {
console.error('AI Response error:', error);
responseText = `⚠️ AI 응답 생성 중 오류가 발생했습니다.\n\n<code>${String(error)}</code>`;
}
} catch (error) {
console.error('[handleMessage] 처리 오류:', error);
responseText = '⚠️ 메시지 처리 중 오류가 발생했습니다. 잠시 후 다시 시도해주세요.';
}
// 버튼 데이터 파싱