Files
telegram-bot-workers/src/n8n-service.ts
kappa ab6c9a2efa refactor: 파일 분리 리팩토링 (routes, services, tools, utils)
아키텍처 개선:
- index.ts: 921줄 → 205줄 (77% 감소)
- openai-service.ts: 1,356줄 → 148줄 (89% 감소)

새로운 디렉토리 구조:
- src/routes/ - Webhook, API, Health check 핸들러
  - webhook.ts (287줄)
  - api.ts (318줄)
  - health.ts (14줄)

- src/services/ - 비즈니스 로직
  - bank-sms-parser.ts (143줄)
  - deposit-matcher.ts (88줄)

- src/tools/ - Function Calling 도구 모듈화
  - weather-tool.ts (37줄)
  - search-tool.ts (156줄)
  - domain-tool.ts (725줄)
  - deposit-tool.ts (183줄)
  - utility-tools.ts (60줄)
  - index.ts (104줄) - 도구 레지스트리

- src/utils/ - 유틸리티 함수
  - email-decoder.ts - Quoted-Printable 디코더

타입 에러 수정:
- routes/webhook.ts: text undefined 체크
- summary-service.ts: D1 타입 캐스팅
- summary-service.ts: Workers AI 타입 처리
- n8n-service.ts: Workers AI 타입 + 미사용 변수 제거

빌드 검증:
- TypeScript 타입 체크 통과
- Wrangler dev 로컬 빌드 성공

문서:
- REFACTORING_SUMMARY.md 추가
- ROUTE_ARCHITECTURE.md 추가

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-19 15:36:17 +09:00

154 lines
3.8 KiB
TypeScript

import { Env, IntentAnalysis, N8nResponse } from './types';
// n8n으로 처리할 기능 목록 (참고용)
// - weather: 날씨
// - search: 검색
// - image: 이미지 생성
// - translate: 번역
// - schedule: 일정
// - reminder: 알림
// - news: 뉴스
// - calculate: 계산
// - summarize_url: URL 요약
// AI가 의도를 분석하여 n8n 호출 여부 결정
export async function analyzeIntent(
ai: Ai,
userMessage: string
): Promise<IntentAnalysis> {
const prompt = `사용자 메시지를 분석하여 어떤 처리가 필요한지 JSON으로 응답하세요.
## 외부 기능이 필요한 경우 (action: "n8n")
- 날씨 정보: type = "weather"
- 웹 검색: type = "search"
- 이미지 생성: type = "image"
- 번역: type = "translate"
- 일정/캘린더: type = "schedule"
- 알림 설정: type = "reminder"
- 뉴스: type = "news"
- 복잡한 계산: type = "calculate"
- URL/링크 요약: type = "summarize_url"
## 일반 대화인 경우 (action: "chat")
- 인사, 잡담, 질문, 조언 요청 등
## 응답 형식 (JSON만 출력)
{"action": "n8n", "type": "weather", "confidence": 0.9}
또는
{"action": "chat", "confidence": 0.95}
## 사용자 메시지
${userMessage}
JSON:`;
try {
const response = await ai.run('@cf/meta/llama-3.1-8b-instruct' as any, {
messages: [{ role: 'user', content: prompt }],
max_tokens: 100,
}) as any;
const text = response.response || '';
// JSON 추출
const jsonMatch = text.match(/\{[^}]+\}/);
if (jsonMatch) {
const parsed = JSON.parse(jsonMatch[0]);
return {
action: parsed.action || 'chat',
type: parsed.type,
confidence: parsed.confidence || 0.5,
};
}
} catch (error) {
console.error('Intent analysis error:', error);
}
// 기본값: 일반 대화
return { action: 'chat', confidence: 0.5 };
}
// n8n Webhook 호출
export async function callN8n(
env: Env,
type: string,
userMessage: string,
userId: number,
chatId: string,
userProfile?: string | null
): Promise<N8nResponse> {
if (!env.N8N_WEBHOOK_URL) {
return { error: 'n8n이 설정되지 않았습니다.' };
}
const webhookUrl = `${env.N8N_WEBHOOK_URL}/webhook/telegram-bot`;
try {
const response = await fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
type,
message: userMessage,
user_id: userId,
chat_id: chatId,
profile: userProfile,
timestamp: new Date().toISOString(),
}),
});
if (!response.ok) {
console.error('n8n error:', response.status, await response.text());
return { error: `n8n 호출 실패 (${response.status})` };
}
const data = await response.json() as N8nResponse;
return data;
} catch (error) {
console.error('n8n fetch error:', error);
return { error: 'n8n 연결 실패' };
}
}
// 스마트 라우팅: AI 판단 → n8n 또는 로컬 AI
export async function smartRoute(
env: Env,
userMessage: string,
userId: number,
chatId: string,
userProfile?: string | null
): Promise<{ useN8n: boolean; n8nType?: string; n8nResponse?: N8nResponse }> {
// n8n URL이 없으면 항상 로컬 AI 사용
if (!env.N8N_WEBHOOK_URL) {
return { useN8n: false };
}
// 의도 분석
const intent = await analyzeIntent(env.AI, userMessage);
// n8n 호출이 필요하고 신뢰도가 높은 경우
if (intent.action === 'n8n' && intent.confidence >= 0.7 && intent.type) {
const n8nResponse = await callN8n(
env,
intent.type,
userMessage,
userId,
chatId,
userProfile
);
// n8n 응답이 성공적인 경우
if (n8nResponse.reply && !n8nResponse.error) {
return {
useN8n: true,
n8nType: intent.type,
n8nResponse,
};
}
}
return { useN8n: false };
}