feat: CLI 테스트 클라이언트 추가
- /api/test 엔드포인트 추가 (Worker에서 직접 응답 반환) - scripts/chat.ts CLI 클라이언트 - npm run chat 스크립트 추가 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
58
src/index.ts
58
src/index.ts
@@ -390,6 +390,64 @@ export default {
|
||||
}
|
||||
}
|
||||
|
||||
// 테스트 API - 메시지 처리 후 응답 직접 반환
|
||||
if (url.pathname === '/api/test' && request.method === 'POST') {
|
||||
try {
|
||||
const body = await request.json() as { text: string; user_id?: string; secret?: string };
|
||||
|
||||
// 간단한 인증
|
||||
if (body.secret !== env.WEBHOOK_SECRET) {
|
||||
return Response.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
if (!body.text) {
|
||||
return Response.json({ error: 'text required' }, { status: 400 });
|
||||
}
|
||||
|
||||
const telegramUserId = body.user_id || '821596605';
|
||||
const chatIdStr = telegramUserId;
|
||||
|
||||
// 사용자 조회/생성
|
||||
const userId = await getOrCreateUser(env.DB, telegramUserId, 'TestUser', 'testuser');
|
||||
|
||||
let responseText: string;
|
||||
|
||||
// 명령어 처리
|
||||
if (body.text.startsWith('/')) {
|
||||
const [command, ...argParts] = body.text.split(' ');
|
||||
const args = argParts.join(' ');
|
||||
responseText = await handleCommand(env, userId, chatIdStr, command, args);
|
||||
} else {
|
||||
// 1. 사용자 메시지 버퍼에 추가
|
||||
await addToBuffer(env.DB, userId, chatIdStr, 'user', body.text);
|
||||
|
||||
// 2. AI 응답 생성
|
||||
responseText = await generateAIResponse(env, userId, chatIdStr, body.text, telegramUserId);
|
||||
|
||||
// 3. 봇 응답 버퍼에 추가
|
||||
await addToBuffer(env.DB, userId, chatIdStr, 'bot', responseText);
|
||||
|
||||
// 4. 임계값 도달시 프로필 업데이트
|
||||
const { summarized } = await processAndSummarize(env, userId, chatIdStr);
|
||||
if (summarized) {
|
||||
responseText += '\n\n👤 프로필이 업데이트되었습니다.';
|
||||
}
|
||||
}
|
||||
|
||||
// HTML 태그 제거 (CLI 출력용)
|
||||
const plainText = responseText.replace(/<[^>]*>/g, '');
|
||||
|
||||
return Response.json({
|
||||
input: body.text,
|
||||
response: plainText,
|
||||
user_id: telegramUserId,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[Test API] Error:', error);
|
||||
return Response.json({ error: String(error) }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// Telegram Webhook 처리
|
||||
if (url.pathname === '/webhook') {
|
||||
// 보안 검증
|
||||
|
||||
Reference in New Issue
Block a user