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>
This commit is contained in:
kappa
2026-01-19 15:36:17 +09:00
parent 3bf42947a7
commit ab6c9a2efa
18 changed files with 2578 additions and 1958 deletions

View File

@@ -1,252 +1,10 @@
import { Env, TelegramUpdate, EmailMessage, BankNotification } from './types';
import { validateWebhookRequest, checkRateLimit } from './security';
import { sendMessage, sendMessageWithKeyboard, setWebhook, getWebhookInfo, sendChatAction, answerCallbackQuery, editMessageText } from './telegram';
import { executeDomainRegister } from './domain-register';
import {
addToBuffer,
processAndSummarize,
generateAIResponse,
} from './summary-service';
import { handleCommand } from './commands';
// 사용자 조회/생성
async function getOrCreateUser(
db: D1Database,
telegramId: string,
firstName: string,
username?: string
): Promise<number> {
const existing = await db
.prepare('SELECT id FROM users WHERE telegram_id = ?')
.bind(telegramId)
.first<{ id: number }>();
if (existing) {
// 마지막 활동 시간 업데이트
await db
.prepare('UPDATE users SET updated_at = CURRENT_TIMESTAMP WHERE id = ?')
.bind(existing.id)
.run();
return existing.id;
}
// 새 사용자 생성
const result = await db
.prepare('INSERT INTO users (telegram_id, first_name, username) VALUES (?, ?, ?)')
.bind(telegramId, firstName, username || null)
.run();
return result.meta.last_row_id as number;
}
// 메시지 처리
async function handleMessage(
env: Env,
update: TelegramUpdate
): Promise<void> {
if (!update.message?.text) return;
const { message } = update;
const chatId = message.chat.id;
const chatIdStr = chatId.toString();
const text = message.text;
const telegramUserId = message.from.id.toString();
// Rate Limiting 체크 (KV 기반)
if (!(await checkRateLimit(env.RATE_LIMIT_KV, telegramUserId))) {
await sendMessage(
env.BOT_TOKEN,
chatId,
'⚠️ 너무 많은 요청입니다. 잠시 후 다시 시도해주세요.'
);
return;
}
// 사용자 처리 (오류 시 사용자에게 알림)
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;
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');
// 1. 사용자 메시지 버퍼에 추가
await addToBuffer(env.DB, userId, chatIdStr, 'user', text);
// 2. AI 응답 생성
responseText = await generateAIResponse(env, userId, chatIdStr, text, telegramUserId);
// 3. 봇 응답 버퍼에 추가
await addToBuffer(env.DB, userId, chatIdStr, 'bot', responseText);
// 4. 임계값 도달시 프로필 업데이트
const { summarized } = await processAndSummarize(env, userId, chatIdStr);
if (summarized) {
responseText += '\n\n<i>👤 프로필이 업데이트되었습니다.</i>';
}
}
} catch (error) {
console.error('[handleMessage] 처리 오류:', error);
responseText = '⚠️ 메시지 처리 중 오류가 발생했습니다. 잠시 후 다시 시도해주세요.';
}
// 버튼 데이터 파싱
const keyboardMatch = responseText.match(/__KEYBOARD__(.+?)__END__\n?/);
if (keyboardMatch) {
const cleanText = responseText.replace(/__KEYBOARD__.+?__END__\n?/, '');
try {
const keyboardData = JSON.parse(keyboardMatch[1]);
if (keyboardData.type === 'domain_register') {
// 도메인 등록 확인 버튼
const callbackData = `domain_reg:${keyboardData.domain}:${keyboardData.price}`;
await sendMessageWithKeyboard(env.BOT_TOKEN, chatId, cleanText, [
[
{ text: '✅ 등록하기', callback_data: callbackData },
{ text: '❌ 취소', callback_data: 'domain_cancel' }
]
]);
return;
}
} catch (e) {
console.error('[Keyboard] 파싱 오류:', e);
}
}
await sendMessage(env.BOT_TOKEN, chatId, responseText);
}
// Callback Query 처리 (인라인 버튼 클릭)
async function handleCallbackQuery(
env: Env,
callbackQuery: TelegramUpdate['callback_query']
): Promise<void> {
if (!callbackQuery) return;
const { id: queryId, from, message, data } = callbackQuery;
if (!data || !message) {
await answerCallbackQuery(env.BOT_TOKEN, queryId, { text: '잘못된 요청입니다.' });
return;
}
const chatId = message.chat.id;
const messageId = message.message_id;
const telegramUserId = from.id.toString();
// 사용자 조회
const user = await env.DB.prepare(
'SELECT id FROM users WHERE telegram_id = ?'
).bind(telegramUserId).first<{ id: number }>();
if (!user) {
await answerCallbackQuery(env.BOT_TOKEN, queryId, { text: '사용자를 찾을 수 없습니다.' });
return;
}
// 도메인 등록 처리
if (data.startsWith('domain_reg:')) {
const parts = data.split(':');
if (parts.length !== 3) {
await answerCallbackQuery(env.BOT_TOKEN, queryId, { text: '잘못된 데이터입니다.' });
return;
}
const domain = parts[1];
const price = parseInt(parts[2]);
// 처리 중 표시
await answerCallbackQuery(env.BOT_TOKEN, queryId, { text: '등록 처리 중...' });
await editMessageText(
env.BOT_TOKEN,
chatId,
messageId,
`⏳ <b>${domain}</b> 등록 처리 중...`
);
// 도메인 등록 실행
const result = await executeDomainRegister(env, user.id, telegramUserId, domain, price);
if (result.success) {
const expiresInfo = result.expiresAt ? `\n• 만료일: ${result.expiresAt}` : '';
const nsInfo = result.nameservers && result.nameservers.length > 0
? `\n\n🌐 <b>현재 네임서버:</b>\n${result.nameservers.map(ns => `• <code>${ns}</code>`).join('\n')}`
: '';
await editMessageText(
env.BOT_TOKEN,
chatId,
messageId,
`✅ <b>도메인 등록 완료!</b>
• 도메인: <code>${result.domain}</code>
• 결제 금액: ${result.price?.toLocaleString()}
• 현재 잔액: ${result.newBalance?.toLocaleString()}${expiresInfo}${nsInfo}
🎉 축하합니다! 도메인이 성공적으로 등록되었습니다.
네임서버 변경이 필요하면 말씀해주세요.`
);
} else {
await editMessageText(
env.BOT_TOKEN,
chatId,
messageId,
`❌ <b>등록 실패</b>
${result.error}
다시 시도하시려면 도메인 등록을 요청해주세요.`
);
}
return;
}
// 도메인 등록 취소
if (data === 'domain_cancel') {
await answerCallbackQuery(env.BOT_TOKEN, queryId, { text: '취소되었습니다.' });
await editMessageText(
env.BOT_TOKEN,
chatId,
messageId,
'❌ 도메인 등록이 취소되었습니다.'
);
return;
}
await answerCallbackQuery(env.BOT_TOKEN, queryId);
}
import { Env, EmailMessage } from './types';
import { sendMessage, setWebhook, getWebhookInfo } from './telegram';
import { handleWebhook } from './routes/webhook';
import { handleApiRequest } from './routes/api';
import { handleHealthCheck } from './routes/health';
import { parseBankSMS } from './services/bank-sms-parser';
import { matchPendingDeposit } from './services/deposit-matcher';
export default {
// HTTP 요청 핸들러
@@ -278,291 +36,17 @@ export default {
// 헬스 체크 (공개 - 최소 정보만)
if (url.pathname === '/health') {
return Response.json({
status: 'ok',
timestamp: new Date().toISOString(),
});
return handleHealthCheck();
}
// Deposit API - 잔액 조회 (namecheap-api 전용)
if (url.pathname === '/api/deposit/balance' && request.method === 'GET') {
try {
const apiSecret = env.DEPOSIT_API_SECRET;
const authHeader = request.headers.get('X-API-Key');
if (!apiSecret || authHeader !== apiSecret) {
return Response.json({ error: 'Unauthorized' }, { status: 401 });
}
const telegramId = url.searchParams.get('telegram_id');
if (!telegramId) {
return Response.json({ error: 'telegram_id required' }, { status: 400 });
}
// 사용자 조회
const user = await env.DB.prepare(
'SELECT id FROM users WHERE telegram_id = ?'
).bind(telegramId).first<{ id: number }>();
if (!user) {
return Response.json({ error: 'User not found' }, { status: 404 });
}
// 잔액 조회
const deposit = await env.DB.prepare(
'SELECT balance FROM user_deposits WHERE user_id = ?'
).bind(user.id).first<{ balance: number }>();
return Response.json({
telegram_id: telegramId,
balance: deposit?.balance || 0,
});
} catch (error) {
console.error('[API] Deposit balance error:', error);
return Response.json({ error: String(error) }, { status: 500 });
}
}
// Deposit API - 잔액 차감 (namecheap-api 전용)
if (url.pathname === '/api/deposit/deduct' && request.method === 'POST') {
try {
const apiSecret = env.DEPOSIT_API_SECRET;
const authHeader = request.headers.get('X-API-Key');
if (!apiSecret || authHeader !== apiSecret) {
return Response.json({ error: 'Unauthorized' }, { status: 401 });
}
const body = await request.json() as {
telegram_id: string;
amount: number;
reason: string;
reference_id?: string;
};
if (!body.telegram_id || !body.amount || !body.reason) {
return Response.json({ error: 'telegram_id, amount, reason required' }, { status: 400 });
}
if (body.amount <= 0) {
return Response.json({ error: 'Amount must be positive' }, { status: 400 });
}
// 사용자 조회
const user = await env.DB.prepare(
'SELECT id FROM users WHERE telegram_id = ?'
).bind(body.telegram_id).first<{ id: number }>();
if (!user) {
return Response.json({ error: 'User not found' }, { status: 404 });
}
// 현재 잔액 확인
const deposit = await env.DB.prepare(
'SELECT balance FROM user_deposits WHERE user_id = ?'
).bind(user.id).first<{ balance: number }>();
const currentBalance = deposit?.balance || 0;
if (currentBalance < body.amount) {
return Response.json({
error: 'Insufficient balance',
current_balance: currentBalance,
required: body.amount,
}, { status: 400 });
}
// 트랜잭션: 잔액 차감 + 거래 기록
await env.DB.batch([
env.DB.prepare(
'UPDATE user_deposits SET balance = balance - ?, updated_at = CURRENT_TIMESTAMP WHERE user_id = ?'
).bind(body.amount, user.id),
env.DB.prepare(
`INSERT INTO deposit_transactions (user_id, type, amount, status, description, confirmed_at)
VALUES (?, 'withdrawal', ?, 'confirmed', ?, CURRENT_TIMESTAMP)`
).bind(user.id, body.amount, body.reason),
]);
const newBalance = currentBalance - body.amount;
console.log(`[API] Deposit deducted: user=${body.telegram_id}, amount=${body.amount}, reason=${body.reason}`);
return Response.json({
success: true,
telegram_id: body.telegram_id,
deducted: body.amount,
previous_balance: currentBalance,
new_balance: newBalance,
});
} catch (error) {
console.error('[API] Deposit deduct error:', error);
return Response.json({ error: String(error) }, { status: 500 });
}
}
// 테스트 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 });
}
}
// 문의 폼 API (웹사이트용)
if (url.pathname === '/api/contact' && request.method === 'POST') {
// CORS: hosting.anvil.it.com만 허용
const corsHeaders = {
'Access-Control-Allow-Origin': 'https://hosting.anvil.it.com',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
};
try {
const body = await request.json() as {
email: string;
message: string;
};
// 필수 필드 검증
if (!body.email || !body.message) {
return Response.json(
{ error: '이메일과 메시지는 필수 항목입니다.' },
{ status: 400, headers: corsHeaders }
);
}
// 이메일 형식 검증
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(body.email)) {
return Response.json(
{ error: '올바른 이메일 형식이 아닙니다.' },
{ status: 400, headers: corsHeaders }
);
}
// 메시지 길이 제한
if (body.message.length > 2000) {
return Response.json(
{ error: '메시지는 2000자 이내로 작성해주세요.' },
{ status: 400, headers: corsHeaders }
);
}
// 관리자에게 텔레그램 알림
const adminId = env.DEPOSIT_ADMIN_ID || env.DOMAIN_OWNER_ID;
if (env.BOT_TOKEN && adminId) {
const timestamp = new Date().toLocaleString('ko-KR', { timeZone: 'Asia/Seoul' });
await sendMessage(
env.BOT_TOKEN,
parseInt(adminId),
`📬 <b>웹사이트 문의</b>\n\n` +
`📧 이메일: <code>${body.email}</code>\n` +
`🕐 시간: ${timestamp}\n\n` +
`💬 내용:\n${body.message}`
);
}
console.log(`[Contact] 문의 수신: ${body.email}`);
return Response.json(
{ success: true, message: '문의가 성공적으로 전송되었습니다.' },
{ headers: corsHeaders }
);
} catch (error) {
console.error('[Contact] 오류:', error);
return Response.json(
{ error: '문의 전송 중 오류가 발생했습니다.' },
{ status: 500, headers: corsHeaders }
);
}
}
// CORS preflight for contact API
if (url.pathname === '/api/contact' && request.method === 'OPTIONS') {
return new Response(null, {
headers: {
'Access-Control-Allow-Origin': 'https://hosting.anvil.it.com',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
},
});
// API 엔드포인트 처리
if (url.pathname.startsWith('/api/')) {
return handleApiRequest(request, env, url);
}
// Telegram Webhook 처리
if (url.pathname === '/webhook') {
// 보안 검증
const validation = await validateWebhookRequest(request, env);
if (!validation.valid) {
console.error('Webhook validation failed:', validation.error);
return new Response(validation.error, { status: 401 });
}
try {
const update = validation.update!;
// Callback Query 처리 (인라인 버튼 클릭)
if (update.callback_query) {
await handleCallbackQuery(env, update.callback_query);
return new Response('OK');
}
// 일반 메시지 처리
await handleMessage(env, update);
return new Response('OK');
} catch (error) {
console.error('Message handling error:', error);
return new Response('Error', { status: 500 });
}
return handleWebhook(request, env);
}
// 루트 경로
@@ -618,7 +102,7 @@ Documentation: https://github.com/your-repo
console.log('[Email] 알림 저장 완료, ID:', notificationId);
// 자동 매칭 시도
const matched = await tryAutoMatch(env.DB, notificationId, notification);
const matched = await matchPendingDeposit(env.DB, notificationId, notification);
// 매칭 성공 시 사용자에게 알림
if (matched && env.BOT_TOKEN) {
@@ -666,7 +150,7 @@ Documentation: https://github.com/your-repo
},
// Cron Trigger: 만료된 입금 대기 자동 취소 (24시간)
async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext): Promise<void> {
async scheduled(_event: ScheduledEvent, env: Env, _ctx: ExecutionContext): Promise<void> {
console.log('[Cron] 만료된 입금 대기 정리 시작');
try {
@@ -719,202 +203,3 @@ Documentation: https://github.com/your-repo
}
},
};
// Quoted-Printable UTF-8 디코딩
function decodeQuotedPrintableUTF8(str: string): string {
// 줄 연속 문자 제거
str = str.replace(/=\r?\n/g, '');
// =XX 패턴을 바이트로 변환
const bytes: number[] = [];
let i = 0;
while (i < str.length) {
if (str[i] === '=' && i + 2 < str.length) {
const hex = str.slice(i + 1, i + 3);
if (/^[0-9A-Fa-f]{2}$/.test(hex)) {
bytes.push(parseInt(hex, 16));
i += 3;
continue;
}
}
bytes.push(str.charCodeAt(i));
i++;
}
// UTF-8 바이트를 문자열로 변환
try {
return new TextDecoder('utf-8').decode(new Uint8Array(bytes));
} catch {
return str;
}
}
// 은행 SMS 파싱 함수
function parseBankSMS(content: string): BankNotification | null {
// MIME 이메일 전처리
let text = content;
// Quoted-Printable UTF-8 디코딩
text = decodeQuotedPrintableUTF8(text);
// HTML <br/> 태그를 줄바꿈으로 변환
text = text.replace(/<br\s*\/?>/gi, '\n');
// 줄바꿈 정규화
text = text.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
// [Web발신] 또는 은행 키워드가 있는 부분만 추출
const smsStartMatch = text.match(/\[Web발신\]|\[하나은행\]|\[KB\]|\[신한\]|\[우리\]|\[농협\]/);
if (smsStartMatch && smsStartMatch.index !== undefined) {
// SMS 시작점부터 500자 추출
text = text.slice(smsStartMatch.index, smsStartMatch.index + 500);
}
// 하나은행 Web발신 패턴 (여러 줄):
// [Web발신]
// 하나,01/16, 22:12
// 427******27104
// 입금1원
// 황병하
const hanaWebPattern = /\[Web발신\]\s*하나[,\s]*(\d{1,2}\/\d{1,2})[,\s]*(\d{1,2}:\d{2})\s*[\d*]+\s*입금([\d,]+)원\s*(\S+)/;
const hanaWebMatch = text.match(hanaWebPattern);
if (hanaWebMatch) {
const [, date, time, amountStr, depositor] = hanaWebMatch;
return {
bankName: '하나은행',
depositorName: depositor.trim(),
amount: parseInt(amountStr.replace(/,/g, '')),
transactionTime: parseDateTime(date, time),
rawMessage: text.slice(0, 500),
};
}
// 하나은행 기존 패턴: [하나은행] 01/16 14:30 입금 50,000원 홍길동 잔액 1,234,567원
const hanaPattern = /\[하나은행\]\s*(\d{1,2}\/\d{1,2})\s*(\d{1,2}:\d{2})?\s*입금\s*([\d,]+)원\s*(\S+?)(?:\s+잔액\s*([\d,]+)원)?/;
const hanaMatch = text.match(hanaPattern);
if (hanaMatch) {
const [, date, time, amountStr, depositor, balanceStr] = hanaMatch;
return {
bankName: '하나은행',
depositorName: depositor,
amount: parseInt(amountStr.replace(/,/g, '')),
balanceAfter: balanceStr ? parseInt(balanceStr.replace(/,/g, '')) : undefined,
transactionTime: parseDateTime(date, time),
rawMessage: text.slice(0, 500),
};
}
// KB국민은행 패턴: [KB] 입금 50,000원 01/16 14:30 홍길동
const kbPattern = /\[KB\]\s*입금\s*([\d,]+)원\s*(\d{1,2}\/\d{1,2})?\s*(\d{1,2}:\d{2})?\s*(\S+)/;
const kbMatch = text.match(kbPattern);
if (kbMatch) {
const [, amountStr, date, time, depositor] = kbMatch;
return {
bankName: 'KB국민은행',
depositorName: depositor,
amount: parseInt(amountStr.replace(/,/g, '')),
transactionTime: date ? parseDateTime(date, time) : undefined,
rawMessage: text.slice(0, 500),
};
}
// 신한은행 패턴: [신한] 01/16 입금 50,000원 홍길동
const shinhanPattern = /\[신한\]\s*(\d{1,2}\/\d{1,2})?\s*입금\s*([\d,]+)원\s*(\S+)/;
const shinhanMatch = text.match(shinhanPattern);
if (shinhanMatch) {
const [, date, amountStr, depositor] = shinhanMatch;
return {
bankName: '신한은행',
depositorName: depositor,
amount: parseInt(amountStr.replace(/,/g, '')),
transactionTime: date ? parseDateTime(date) : undefined,
rawMessage: text.slice(0, 500),
};
}
// 일반 입금 패턴: 입금 50,000원 홍길동 또는 홍길동 50,000원 입금
const genericPattern1 = /입금\s*([\d,]+)원?\s*(\S{2,10})/;
const genericPattern2 = /(\S{2,10})\s*([\d,]+)원?\s*입금/;
const genericMatch1 = text.match(genericPattern1);
if (genericMatch1) {
return {
bankName: '알수없음',
depositorName: genericMatch1[2],
amount: parseInt(genericMatch1[1].replace(/,/g, '')),
rawMessage: text.slice(0, 500),
};
}
const genericMatch2 = text.match(genericPattern2);
if (genericMatch2) {
return {
bankName: '알수없음',
depositorName: genericMatch2[1],
amount: parseInt(genericMatch2[2].replace(/,/g, '')),
rawMessage: text.slice(0, 500),
};
}
return null;
}
// 날짜/시간 파싱
function parseDateTime(dateStr: string, timeStr?: string): Date {
const now = new Date();
const [month, day] = dateStr.split('/').map(Number);
const year = now.getFullYear();
let hours = 0, minutes = 0;
if (timeStr) {
[hours, minutes] = timeStr.split(':').map(Number);
}
return new Date(year, month - 1, day, hours, minutes);
}
// 자동 매칭 시도
async function tryAutoMatch(
db: D1Database,
notificationId: number,
notification: BankNotification
): Promise<{ transactionId: number; userId: number; amount: number } | null> {
// 매칭 조건: 입금자명(앞 7글자) + 금액이 일치하는 pending 거래
// 은행 SMS는 입금자명이 7글자까지만 표시됨
const pendingTx = await db.prepare(
`SELECT dt.id, dt.user_id, dt.amount
FROM deposit_transactions dt
WHERE dt.status = 'pending'
AND dt.type = 'deposit'
AND SUBSTR(dt.depositor_name, 1, 7) = ?
AND dt.amount = ?
ORDER BY dt.created_at ASC
LIMIT 1`
).bind(notification.depositorName, notification.amount).first<{
id: number;
user_id: number;
amount: number;
}>();
if (!pendingTx) {
console.log('[AutoMatch] 매칭되는 pending 거래 없음');
return null;
}
console.log('[AutoMatch] 매칭 발견:', pendingTx);
// 트랜잭션: 거래 확정 + 잔액 증가 + 알림 매칭 업데이트
await db.batch([
db.prepare(
"UPDATE deposit_transactions SET status = 'confirmed', confirmed_at = CURRENT_TIMESTAMP WHERE id = ?"
).bind(pendingTx.id),
db.prepare(
'UPDATE user_deposits SET balance = balance + ?, updated_at = CURRENT_TIMESTAMP WHERE user_id = ?'
).bind(pendingTx.amount, pendingTx.user_id),
db.prepare(
'UPDATE bank_notifications SET matched_transaction_id = ? WHERE id = ?'
).bind(pendingTx.id, notificationId),
]);
return { transactionId: pendingTx.id, userId: pendingTx.user_id, amount: pendingTx.amount };
}

View File

@@ -1,17 +1,15 @@
import { Env, IntentAnalysis, N8nResponse } from './types';
// n8n으로 처리할 기능 목록
const N8N_CAPABILITIES = [
'weather', // 날씨
'search', // 검색
'image', // 이미지 생성
'translate', // 번역
'schedule', // 일정
'reminder', // 알림
'news', // 뉴스
'calculate', // 계산
'summarize_url', // URL 요약
];
// n8n으로 처리할 기능 목록 (참고용)
// - weather: 날씨
// - search: 검색
// - image: 이미지 생성
// - translate: 번역
// - schedule: 일정
// - reminder: 알림
// - news: 뉴스
// - calculate: 계산
// - summarize_url: URL 요약
// AI가 의도를 분석하여 n8n 호출 여부 결정
export async function analyzeIntent(
@@ -45,10 +43,10 @@ ${userMessage}
JSON:`;
try {
const response = await ai.run('@cf/meta/llama-3.1-8b-instruct', {
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 || '';

File diff suppressed because it is too large Load Diff

318
src/routes/api.ts Normal file
View File

@@ -0,0 +1,318 @@
import { Env } from '../types';
import { sendMessage } from '../telegram';
import {
addToBuffer,
processAndSummarize,
generateAIResponse,
} from '../summary-service';
import { handleCommand } from '../commands';
// 사용자 조회/생성
async function getOrCreateUser(
db: D1Database,
telegramId: string,
firstName: string,
username?: string
): Promise<number> {
const existing = await db
.prepare('SELECT id FROM users WHERE telegram_id = ?')
.bind(telegramId)
.first<{ id: number }>();
if (existing) {
// 마지막 활동 시간 업데이트
await db
.prepare('UPDATE users SET updated_at = CURRENT_TIMESTAMP WHERE id = ?')
.bind(existing.id)
.run();
return existing.id;
}
// 새 사용자 생성
const result = await db
.prepare('INSERT INTO users (telegram_id, first_name, username) VALUES (?, ?, ?)')
.bind(telegramId, firstName, username || null)
.run();
return result.meta.last_row_id as number;
}
/**
* API 엔드포인트 처리
*
* Manual Test:
* 1. wrangler dev
* 2. Test deposit balance:
* curl http://localhost:8787/api/deposit/balance?telegram_id=123 \
* -H "X-API-Key: your-secret"
* 3. Test deposit deduct:
* curl -X POST http://localhost:8787/api/deposit/deduct \
* -H "X-API-Key: your-secret" \
* -H "Content-Type: application/json" \
* -d '{"telegram_id":"123","amount":1000,"reason":"test"}'
* 4. Test API:
* curl -X POST http://localhost:8787/api/test \
* -H "Content-Type: application/json" \
* -d '{"text":"hello","secret":"your-secret"}'
* 5. Test contact (from allowed origin):
* curl -X POST http://localhost:8787/api/contact \
* -H "Origin: https://hosting.anvil.it.com" \
* -H "Content-Type: application/json" \
* -d '{"email":"test@example.com","message":"test message"}'
*/
export async function handleApiRequest(request: Request, env: Env, url: URL): Promise<Response> {
// Deposit API - 잔액 조회 (namecheap-api 전용)
if (url.pathname === '/api/deposit/balance' && request.method === 'GET') {
try {
const apiSecret = env.DEPOSIT_API_SECRET;
const authHeader = request.headers.get('X-API-Key');
if (!apiSecret || authHeader !== apiSecret) {
return Response.json({ error: 'Unauthorized' }, { status: 401 });
}
const telegramId = url.searchParams.get('telegram_id');
if (!telegramId) {
return Response.json({ error: 'telegram_id required' }, { status: 400 });
}
// 사용자 조회
const user = await env.DB.prepare(
'SELECT id FROM users WHERE telegram_id = ?'
).bind(telegramId).first<{ id: number }>();
if (!user) {
return Response.json({ error: 'User not found' }, { status: 404 });
}
// 잔액 조회
const deposit = await env.DB.prepare(
'SELECT balance FROM user_deposits WHERE user_id = ?'
).bind(user.id).first<{ balance: number }>();
return Response.json({
telegram_id: telegramId,
balance: deposit?.balance || 0,
});
} catch (error) {
console.error('[API] Deposit balance error:', error);
return Response.json({ error: String(error) }, { status: 500 });
}
}
// Deposit API - 잔액 차감 (namecheap-api 전용)
if (url.pathname === '/api/deposit/deduct' && request.method === 'POST') {
try {
const apiSecret = env.DEPOSIT_API_SECRET;
const authHeader = request.headers.get('X-API-Key');
if (!apiSecret || authHeader !== apiSecret) {
return Response.json({ error: 'Unauthorized' }, { status: 401 });
}
const body = await request.json() as {
telegram_id: string;
amount: number;
reason: string;
reference_id?: string;
};
if (!body.telegram_id || !body.amount || !body.reason) {
return Response.json({ error: 'telegram_id, amount, reason required' }, { status: 400 });
}
if (body.amount <= 0) {
return Response.json({ error: 'Amount must be positive' }, { status: 400 });
}
// 사용자 조회
const user = await env.DB.prepare(
'SELECT id FROM users WHERE telegram_id = ?'
).bind(body.telegram_id).first<{ id: number }>();
if (!user) {
return Response.json({ error: 'User not found' }, { status: 404 });
}
// 현재 잔액 확인
const deposit = await env.DB.prepare(
'SELECT balance FROM user_deposits WHERE user_id = ?'
).bind(user.id).first<{ balance: number }>();
const currentBalance = deposit?.balance || 0;
if (currentBalance < body.amount) {
return Response.json({
error: 'Insufficient balance',
current_balance: currentBalance,
required: body.amount,
}, { status: 400 });
}
// 트랜잭션: 잔액 차감 + 거래 기록
await env.DB.batch([
env.DB.prepare(
'UPDATE user_deposits SET balance = balance - ?, updated_at = CURRENT_TIMESTAMP WHERE user_id = ?'
).bind(body.amount, user.id),
env.DB.prepare(
`INSERT INTO deposit_transactions (user_id, type, amount, status, description, confirmed_at)
VALUES (?, 'withdrawal', ?, 'confirmed', ?, CURRENT_TIMESTAMP)`
).bind(user.id, body.amount, body.reason),
]);
const newBalance = currentBalance - body.amount;
console.log(`[API] Deposit deducted: user=${body.telegram_id}, amount=${body.amount}, reason=${body.reason}`);
return Response.json({
success: true,
telegram_id: body.telegram_id,
deducted: body.amount,
previous_balance: currentBalance,
new_balance: newBalance,
});
} catch (error) {
console.error('[API] Deposit deduct error:', error);
return Response.json({ error: String(error) }, { status: 500 });
}
}
// 테스트 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 });
}
}
// 문의 폼 API (웹사이트용)
if (url.pathname === '/api/contact' && request.method === 'POST') {
// CORS: hosting.anvil.it.com만 허용
const corsHeaders = {
'Access-Control-Allow-Origin': 'https://hosting.anvil.it.com',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
};
try {
const body = await request.json() as {
email: string;
message: string;
};
// 필수 필드 검증
if (!body.email || !body.message) {
return Response.json(
{ error: '이메일과 메시지는 필수 항목입니다.' },
{ status: 400, headers: corsHeaders }
);
}
// 이메일 형식 검증
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(body.email)) {
return Response.json(
{ error: '올바른 이메일 형식이 아닙니다.' },
{ status: 400, headers: corsHeaders }
);
}
// 메시지 길이 제한
if (body.message.length > 2000) {
return Response.json(
{ error: '메시지는 2000자 이내로 작성해주세요.' },
{ status: 400, headers: corsHeaders }
);
}
// 관리자에게 텔레그램 알림
const adminId = env.DEPOSIT_ADMIN_ID || env.DOMAIN_OWNER_ID;
if (env.BOT_TOKEN && adminId) {
const timestamp = new Date().toLocaleString('ko-KR', { timeZone: 'Asia/Seoul' });
await sendMessage(
env.BOT_TOKEN,
parseInt(adminId),
`📬 <b>웹사이트 문의</b>\n\n` +
`📧 이메일: <code>${body.email}</code>\n` +
`🕐 시간: ${timestamp}\n\n` +
`💬 내용:\n${body.message}`
);
}
console.log(`[Contact] 문의 수신: ${body.email}`);
return Response.json(
{ success: true, message: '문의가 성공적으로 전송되었습니다.' },
{ headers: corsHeaders }
);
} catch (error) {
console.error('[Contact] 오류:', error);
return Response.json(
{ error: '문의 전송 중 오류가 발생했습니다.' },
{ status: 500, headers: corsHeaders }
);
}
}
// CORS preflight for contact API
if (url.pathname === '/api/contact' && request.method === 'OPTIONS') {
return new Response(null, {
headers: {
'Access-Control-Allow-Origin': 'https://hosting.anvil.it.com',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
},
});
}
return new Response('Not Found', { status: 404 });
}

14
src/routes/health.ts Normal file
View File

@@ -0,0 +1,14 @@
/**
* Health Check 엔드포인트
*
* Manual Test:
* 1. wrangler dev
* 2. curl http://localhost:8787/health
* 3. Expected: {"status":"ok","timestamp":"..."}
*/
export async function handleHealthCheck(): Promise<Response> {
return Response.json({
status: 'ok',
timestamp: new Date().toISOString(),
});
}

287
src/routes/webhook.ts Normal file
View File

@@ -0,0 +1,287 @@
import { Env, TelegramUpdate } from '../types';
import { validateWebhookRequest, checkRateLimit } from '../security';
import { sendMessage, sendMessageWithKeyboard, sendChatAction, answerCallbackQuery, editMessageText } from '../telegram';
import { executeDomainRegister } from '../domain-register';
import {
addToBuffer,
processAndSummarize,
generateAIResponse,
} from '../summary-service';
import { handleCommand } from '../commands';
// 사용자 조회/생성
async function getOrCreateUser(
db: D1Database,
telegramId: string,
firstName: string,
username?: string
): Promise<number> {
const existing = await db
.prepare('SELECT id FROM users WHERE telegram_id = ?')
.bind(telegramId)
.first<{ id: number }>();
if (existing) {
// 마지막 활동 시간 업데이트
await db
.prepare('UPDATE users SET updated_at = CURRENT_TIMESTAMP WHERE id = ?')
.bind(existing.id)
.run();
return existing.id;
}
// 새 사용자 생성
const result = await db
.prepare('INSERT INTO users (telegram_id, first_name, username) VALUES (?, ?, ?)')
.bind(telegramId, firstName, username || null)
.run();
return result.meta.last_row_id as number;
}
// 메시지 처리
async function handleMessage(
env: Env,
update: TelegramUpdate
): Promise<void> {
if (!update.message?.text) return;
const { message } = update;
const chatId = message.chat.id;
const chatIdStr = chatId.toString();
const text = message.text!; // Already checked above
const telegramUserId = message.from.id.toString();
// Rate Limiting 체크 (KV 기반)
if (!(await checkRateLimit(env.RATE_LIMIT_KV, telegramUserId))) {
await sendMessage(
env.BOT_TOKEN,
chatId,
'⚠️ 너무 많은 요청입니다. 잠시 후 다시 시도해주세요.'
);
return;
}
// 사용자 처리 (오류 시 사용자에게 알림)
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;
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');
// 1. 사용자 메시지 버퍼에 추가
await addToBuffer(env.DB, userId, chatIdStr, 'user', text);
// 2. AI 응답 생성
responseText = await generateAIResponse(env, userId, chatIdStr, text, telegramUserId);
// 3. 봇 응답 버퍼에 추가
await addToBuffer(env.DB, userId, chatIdStr, 'bot', responseText);
// 4. 임계값 도달시 프로필 업데이트
const { summarized } = await processAndSummarize(env, userId, chatIdStr);
if (summarized) {
responseText += '\n\n<i>👤 프로필이 업데이트되었습니다.</i>';
}
}
} catch (error) {
console.error('[handleMessage] 처리 오류:', error);
responseText = '⚠️ 메시지 처리 중 오류가 발생했습니다. 잠시 후 다시 시도해주세요.';
}
// 버튼 데이터 파싱
const keyboardMatch = responseText.match(/__KEYBOARD__(.+?)__END__\n?/);
if (keyboardMatch) {
const cleanText = responseText.replace(/__KEYBOARD__.+?__END__\n?/, '');
try {
const keyboardData = JSON.parse(keyboardMatch[1]);
if (keyboardData.type === 'domain_register') {
// 도메인 등록 확인 버튼
const callbackData = `domain_reg:${keyboardData.domain}:${keyboardData.price}`;
await sendMessageWithKeyboard(env.BOT_TOKEN, chatId, cleanText, [
[
{ text: '✅ 등록하기', callback_data: callbackData },
{ text: '❌ 취소', callback_data: 'domain_cancel' }
]
]);
return;
}
} catch (e) {
console.error('[Keyboard] 파싱 오류:', e);
}
}
await sendMessage(env.BOT_TOKEN, chatId, responseText);
}
// Callback Query 처리 (인라인 버튼 클릭)
async function handleCallbackQuery(
env: Env,
callbackQuery: TelegramUpdate['callback_query']
): Promise<void> {
if (!callbackQuery) return;
const { id: queryId, from, message, data } = callbackQuery;
if (!data || !message) {
await answerCallbackQuery(env.BOT_TOKEN, queryId, { text: '잘못된 요청입니다.' });
return;
}
const chatId = message.chat.id;
const messageId = message.message_id;
const telegramUserId = from.id.toString();
// 사용자 조회
const user = await env.DB.prepare(
'SELECT id FROM users WHERE telegram_id = ?'
).bind(telegramUserId).first<{ id: number }>();
if (!user) {
await answerCallbackQuery(env.BOT_TOKEN, queryId, { text: '사용자를 찾을 수 없습니다.' });
return;
}
// 도메인 등록 처리
if (data.startsWith('domain_reg:')) {
const parts = data.split(':');
if (parts.length !== 3) {
await answerCallbackQuery(env.BOT_TOKEN, queryId, { text: '잘못된 데이터입니다.' });
return;
}
const domain = parts[1];
const price = parseInt(parts[2]);
// 처리 중 표시
await answerCallbackQuery(env.BOT_TOKEN, queryId, { text: '등록 처리 중...' });
await editMessageText(
env.BOT_TOKEN,
chatId,
messageId,
`⏳ <b>${domain}</b> 등록 처리 중...`
);
// 도메인 등록 실행
const result = await executeDomainRegister(env, user.id, telegramUserId, domain, price);
if (result.success) {
const expiresInfo = result.expiresAt ? `\n• 만료일: ${result.expiresAt}` : '';
const nsInfo = result.nameservers && result.nameservers.length > 0
? `\n\n🌐 <b>현재 네임서버:</b>\n${result.nameservers.map(ns => `• <code>${ns}</code>`).join('\n')}`
: '';
await editMessageText(
env.BOT_TOKEN,
chatId,
messageId,
`✅ <b>도메인 등록 완료!</b>
• 도메인: <code>${result.domain}</code>
• 결제 금액: ${result.price?.toLocaleString()}
• 현재 잔액: ${result.newBalance?.toLocaleString()}${expiresInfo}${nsInfo}
🎉 축하합니다! 도메인이 성공적으로 등록되었습니다.
네임서버 변경이 필요하면 말씀해주세요.`
);
} else {
await editMessageText(
env.BOT_TOKEN,
chatId,
messageId,
`❌ <b>등록 실패</b>
${result.error}
다시 시도하시려면 도메인 등록을 요청해주세요.`
);
}
return;
}
// 도메인 등록 취소
if (data === 'domain_cancel') {
await answerCallbackQuery(env.BOT_TOKEN, queryId, { text: '취소되었습니다.' });
await editMessageText(
env.BOT_TOKEN,
chatId,
messageId,
'❌ 도메인 등록이 취소되었습니다.'
);
return;
}
await answerCallbackQuery(env.BOT_TOKEN, queryId);
}
/**
* Telegram Webhook 요청 처리
*
* Manual Test:
* 1. wrangler dev
* 2. curl -X POST http://localhost:8787/webhook \
* -H "Content-Type: application/json" \
* -H "X-Telegram-Bot-Api-Secret-Token: test-secret" \
* -d '{"message":{"chat":{"id":123},"text":"테스트"}}'
* 3. Expected: OK response, message processed
*/
export async function handleWebhook(request: Request, env: Env): Promise<Response> {
// 보안 검증
const validation = await validateWebhookRequest(request, env);
if (!validation.valid) {
console.error('[Webhook] 검증 실패:', validation.error);
return new Response(validation.error, { status: 401 });
}
try {
const update = validation.update!;
// Callback Query 처리 (인라인 버튼 클릭)
if (update.callback_query) {
await handleCallbackQuery(env, update.callback_query);
return new Response('OK');
}
// 일반 메시지 처리
await handleMessage(env, update);
return new Response('OK');
} catch (error) {
console.error('[Webhook] 메시지 처리 오류:', error);
return new Response('Error', { status: 500 });
}
}

View File

@@ -0,0 +1,143 @@
import { BankNotification } from '../types';
import { parseQuotedPrintable } from '../utils/email-decoder';
/**
* 은행 SMS 파싱 함수
*
* 지원 은행:
* - 하나은행 (Web발신 + 기존 패턴)
* - KB국민은행
* - 신한은행
* - 일반 패턴 (은행 불명)
*
* @param content - 이메일 원본 내용 (MIME 포함 가능)
* @returns 파싱된 은행 알림 또는 null (파싱 실패)
*/
export function parseBankSMS(content: string): BankNotification | null {
// MIME 이메일 전처리
let text = content;
// Quoted-Printable UTF-8 디코딩
text = parseQuotedPrintable(text);
// HTML <br/> 태그를 줄바꿈으로 변환
text = text.replace(/<br\s*\/?>/gi, '\n');
// 줄바꿈 정규화
text = text.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
// [Web발신] 또는 은행 키워드가 있는 부분만 추출
const smsStartMatch = text.match(/\[Web발신\]|\[하나은행\]|\[KB\]|\[신한\]|\[우리\]|\[농협\]/);
if (smsStartMatch && smsStartMatch.index !== undefined) {
// SMS 시작점부터 500자 추출
text = text.slice(smsStartMatch.index, smsStartMatch.index + 500);
}
// 하나은행 Web발신 패턴 (여러 줄):
// [Web발신]
// 하나,01/16, 22:12
// 427******27104
// 입금1원
// 황병하
const hanaWebPattern = /\[Web발신\]\s*하나[,\s]*(\d{1,2}\/\d{1,2})[,\s]*(\d{1,2}:\d{2})\s*[\d*]+\s*입금([\d,]+)원\s*(\S+)/;
const hanaWebMatch = text.match(hanaWebPattern);
if (hanaWebMatch) {
const [, date, time, amountStr, depositor] = hanaWebMatch;
return {
bankName: '하나은행',
depositorName: depositor.trim(),
amount: parseInt(amountStr.replace(/,/g, '')),
transactionTime: parseDateTime(date, time),
rawMessage: text.slice(0, 500),
};
}
// 하나은행 기존 패턴: [하나은행] 01/16 14:30 입금 50,000원 홍길동 잔액 1,234,567원
const hanaPattern = /\[하나은행\]\s*(\d{1,2}\/\d{1,2})\s*(\d{1,2}:\d{2})?\s*입금\s*([\d,]+)원\s*(\S+?)(?:\s+잔액\s*([\d,]+)원)?/;
const hanaMatch = text.match(hanaPattern);
if (hanaMatch) {
const [, date, time, amountStr, depositor, balanceStr] = hanaMatch;
return {
bankName: '하나은행',
depositorName: depositor,
amount: parseInt(amountStr.replace(/,/g, '')),
balanceAfter: balanceStr ? parseInt(balanceStr.replace(/,/g, '')) : undefined,
transactionTime: parseDateTime(date, time),
rawMessage: text.slice(0, 500),
};
}
// KB국민은행 패턴: [KB] 입금 50,000원 01/16 14:30 홍길동
const kbPattern = /\[KB\]\s*입금\s*([\d,]+)원\s*(\d{1,2}\/\d{1,2})?\s*(\d{1,2}:\d{2})?\s*(\S+)/;
const kbMatch = text.match(kbPattern);
if (kbMatch) {
const [, amountStr, date, time, depositor] = kbMatch;
return {
bankName: 'KB국민은행',
depositorName: depositor,
amount: parseInt(amountStr.replace(/,/g, '')),
transactionTime: date ? parseDateTime(date, time) : undefined,
rawMessage: text.slice(0, 500),
};
}
// 신한은행 패턴: [신한] 01/16 입금 50,000원 홍길동
const shinhanPattern = /\[신한\]\s*(\d{1,2}\/\d{1,2})?\s*입금\s*([\d,]+)원\s*(\S+)/;
const shinhanMatch = text.match(shinhanPattern);
if (shinhanMatch) {
const [, date, amountStr, depositor] = shinhanMatch;
return {
bankName: '신한은행',
depositorName: depositor,
amount: parseInt(amountStr.replace(/,/g, '')),
transactionTime: date ? parseDateTime(date) : undefined,
rawMessage: text.slice(0, 500),
};
}
// 일반 입금 패턴: 입금 50,000원 홍길동 또는 홍길동 50,000원 입금
const genericPattern1 = /입금\s*([\d,]+)원?\s*(\S{2,10})/;
const genericPattern2 = /(\S{2,10})\s*([\d,]+)원?\s*입금/;
const genericMatch1 = text.match(genericPattern1);
if (genericMatch1) {
return {
bankName: '알수없음',
depositorName: genericMatch1[2],
amount: parseInt(genericMatch1[1].replace(/,/g, '')),
rawMessage: text.slice(0, 500),
};
}
const genericMatch2 = text.match(genericPattern2);
if (genericMatch2) {
return {
bankName: '알수없음',
depositorName: genericMatch2[1],
amount: parseInt(genericMatch2[2].replace(/,/g, '')),
rawMessage: text.slice(0, 500),
};
}
return null;
}
/**
* 날짜/시간 파싱 헬퍼 함수
*
* @param dateStr - "MM/DD" 형식
* @param timeStr - "HH:MM" 형식 (선택)
* @returns Date 객체
*/
function parseDateTime(dateStr: string, timeStr?: string): Date {
const now = new Date();
const [month, day] = dateStr.split('/').map(Number);
const year = now.getFullYear();
let hours = 0, minutes = 0;
if (timeStr) {
[hours, minutes] = timeStr.split(':').map(Number);
}
return new Date(year, month - 1, day, hours, minutes);
}

View File

@@ -0,0 +1,88 @@
import { BankNotification } from '../types';
/**
* 자동 매칭 결과
*/
export interface MatchResult {
transactionId: number;
userId: number;
amount: number;
}
/**
* 입금 SMS와 대기 중인 거래를 자동 매칭
*
* 매칭 조건:
* - 입금자명 앞 7글자 일치 (은행 SMS가 7글자까지만 표시)
* - 금액 일치
* - 상태가 'pending'인 거래
*
* 매칭 성공 시:
* - deposit_transactions.status = 'confirmed'
* - user_deposits.balance 증가
* - bank_notifications.matched_transaction_id 업데이트
*
* @param db - D1 Database 인스턴스
* @param notificationId - bank_notifications 테이블의 ID
* @param notification - 파싱된 은행 알림
* @returns 매칭 결과 또는 null (매칭 실패)
*/
export async function matchPendingDeposit(
db: D1Database,
notificationId: number,
notification: BankNotification
): Promise<MatchResult | null> {
// 매칭 조건: 입금자명(앞 7글자) + 금액이 일치하는 pending 거래
// 은행 SMS는 입금자명이 7글자까지만 표시됨
const pendingTx = await db.prepare(
`SELECT dt.id, dt.user_id, dt.amount
FROM deposit_transactions dt
WHERE dt.status = 'pending'
AND dt.type = 'deposit'
AND SUBSTR(dt.depositor_name, 1, 7) = ?
AND dt.amount = ?
ORDER BY dt.created_at ASC
LIMIT 1`
).bind(notification.depositorName, notification.amount).first<{
id: number;
user_id: number;
amount: number;
}>();
if (!pendingTx) {
console.log('[matchPendingDeposit] 매칭되는 pending 거래 없음');
return null;
}
console.log('[matchPendingDeposit] 매칭 발견:', pendingTx);
try {
// 트랜잭션: 거래 확정 + 잔액 증가 + 알림 매칭 업데이트
await db.batch([
db.prepare(
"UPDATE deposit_transactions SET status = 'confirmed', confirmed_at = CURRENT_TIMESTAMP WHERE id = ?"
).bind(pendingTx.id),
db.prepare(
'UPDATE user_deposits SET balance = balance + ?, updated_at = CURRENT_TIMESTAMP WHERE user_id = ?'
).bind(pendingTx.amount, pendingTx.user_id),
db.prepare(
'UPDATE bank_notifications SET matched_transaction_id = ? WHERE id = ?'
).bind(pendingTx.id, notificationId),
]);
console.log('[matchPendingDeposit] 매칭 완료:', {
transactionId: pendingTx.id,
userId: pendingTx.user_id,
amount: pendingTx.amount,
});
return {
transactionId: pendingTx.id,
userId: pendingTx.user_id,
amount: pendingTx.amount,
};
} catch (error) {
console.error('[matchPendingDeposit] DB 업데이트 실패:', error);
throw error;
}
}

View File

@@ -46,7 +46,7 @@ export async function getBufferedMessages(
.bind(userId, chatId)
.all();
return (results || []) as BufferedMessage[];
return (results || []) as unknown as BufferedMessage[];
}
// 최신 요약 조회
@@ -86,7 +86,7 @@ export async function getAllSummaries(
.bind(userId, chatId)
.all();
return (results || []) as Summary[];
return (results || []) as unknown as Summary[];
}
// 전체 컨텍스트 조회
@@ -187,10 +187,10 @@ ${userMessages}
}
// 폴백: Workers AI
const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct' as any, {
messages: [{ role: 'user', content: prompt }],
max_tokens: 500,
});
}) as any;
return response.response || '프로필 생성 실패';
}
@@ -279,7 +279,7 @@ export async function generateAIResponse(
? context.summaries
.slice()
.reverse() // 오래된 것부터 표시
.map((s, i) => `[v${s.generation}] ${s.summary}`)
.map((s) => `[v${s.generation}] ${s.summary}`)
.join('\n\n')
: null;
@@ -312,14 +312,14 @@ ${integratedProfile}
}
// 폴백: Workers AI
const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct' as any, {
messages: [
{ role: 'system', content: systemPrompt },
...recentContext,
{ role: 'user', content: userMessage },
],
max_tokens: 500,
});
}) as any;
return response.response || '응답을 생성할 수 없습니다.';
}

183
src/tools/deposit-tool.ts Normal file
View File

@@ -0,0 +1,183 @@
import { executeDepositFunction, type DepositContext } from '../deposit-agent';
import type { Env } from '../types';
export const manageDepositTool = {
type: 'function',
function: {
name: 'manage_deposit',
description: '예치금을 관리합니다. "입금", "충전", "잔액", "계좌", "계좌번호", "송금", "거래내역" 등의 키워드가 포함되면 반드시 이 도구를 사용하세요.',
parameters: {
type: 'object',
properties: {
action: {
type: 'string',
enum: ['balance', 'account', 'request', 'history', 'cancel', 'pending', 'confirm', 'reject'],
description: 'balance: 잔액 조회, account: 입금 계좌 안내, request: 입금 신고(충전 요청), history: 거래 내역, cancel: 입금 취소, pending: 대기 목록(관리자), confirm: 입금 확인(관리자), reject: 입금 거절(관리자)',
},
depositor_name: {
type: 'string',
description: '입금자명. request action에서 필수',
},
amount: {
type: 'number',
description: '금액. request action에서 필수. 자연어 금액은 숫자로 변환 (만원→10000, 5천원→5000)',
},
transaction_id: {
type: 'number',
description: '거래 ID. cancel, confirm, reject action에서 필수',
},
limit: {
type: 'number',
description: '조회 개수. history action에서 사용 (기본 10)',
},
},
required: ['action'],
},
},
};
// 예치금 결과 포맷팅 (고정 형식)
function formatDepositResult(action: string, result: any): string {
if (result.error) {
return `🚫 ${result.error}`;
}
switch (action) {
case 'balance':
return `💰 현재 잔액: ${result.formatted}`;
case 'account':
return `💳 입금 계좌 안내
• 은행: ${result.bank}
• 계좌번호: ${result.account}
• 예금주: ${result.holder}
📌 ${result.instruction}`;
case 'request':
if (result.auto_matched) {
return `✅ 입금 확인 완료!
• 입금액: ${result.amount.toLocaleString()}
• 입금자: ${result.depositor_name}
• 현재 잔액: ${result.new_balance.toLocaleString()}
${result.message}`;
} else {
return `📋 입금 요청 등록 (#${result.transaction_id})
• 입금액: ${result.amount.toLocaleString()}
• 입금자: ${result.depositor_name}
💳 입금 계좌
${result.account_info.bank} ${result.account_info.account}
(${result.account_info.holder})
📌 ${result.message}`;
}
case 'history': {
if (result.message && !result.transactions?.length) {
return `📋 ${result.message}`;
}
const statusIcon = (s: string) => s === 'confirmed' ? '✓' : s === 'pending' ? '⏳' : '✗';
const typeLabel = (t: string) => t === 'deposit' ? '입금' : t === 'withdrawal' ? '출금' : t === 'refund' ? '환불' : t;
const txList = result.transactions.map((tx: any) => {
const date = tx.confirmed_at || tx.created_at;
const dateStr = date ? new Date(date).toLocaleDateString('ko-KR', { month: '2-digit', day: '2-digit' }) : '';
const desc = tx.description ? ` - ${tx.description}` : '';
return `#${tx.id}: ${typeLabel(tx.type)} ${tx.amount.toLocaleString()}${statusIcon(tx.status)} (${dateStr})${desc}`;
}).join('\n');
return `📋 거래 내역\n\n${txList}`;
}
case 'cancel':
return `✅ 거래 #${result.transaction_id} 취소 완료`;
case 'pending': {
if (result.message && !result.pending?.length) {
return `📋 ${result.message}`;
}
const pendingList = result.pending.map((p: any) =>
`#${p.id}: ${p.depositor_name} ${p.amount.toLocaleString()}원 (${p.user})`
).join('\n');
return `📋 대기 중인 입금 요청\n\n${pendingList}`;
}
case 'confirm':
return `✅ 입금 확인 완료 (#${result.transaction_id}, ${result.amount.toLocaleString()}원)`;
case 'reject':
return `❌ 입금 거절 완료 (#${result.transaction_id})`;
default:
return `💰 ${JSON.stringify(result)}`;
}
}
export async function executeManageDeposit(
args: { action: string; depositor_name?: string; amount?: number; transaction_id?: number; limit?: number },
env?: Env,
telegramUserId?: string,
db?: D1Database
): Promise<string> {
const { action, depositor_name, amount, transaction_id, limit } = args;
console.log('[manage_deposit] 시작:', { action, depositor_name, amount, telegramUserId });
if (!telegramUserId || !db) {
return '🚫 예치금 기능을 사용할 수 없습니다.';
}
// 사용자 조회
const user = await db.prepare(
'SELECT id FROM users WHERE telegram_id = ?'
).bind(telegramUserId).first<{ id: number }>();
if (!user) {
return '🚫 사용자 정보를 찾을 수 없습니다.';
}
const isAdmin = telegramUserId === env?.DEPOSIT_ADMIN_ID;
const context: DepositContext = {
userId: user.id,
telegramUserId,
isAdmin,
db,
};
// action → executeDepositFunction 매핑
const actionMap: Record<string, string> = {
balance: 'get_balance',
account: 'get_account_info',
request: 'request_deposit',
history: 'get_transactions',
cancel: 'cancel_transaction',
pending: 'get_pending_list',
confirm: 'confirm_deposit',
reject: 'reject_deposit',
};
const funcName = actionMap[action];
if (!funcName) {
return `🚫 알 수 없는 작업: ${action}`;
}
try {
const funcArgs: Record<string, any> = {};
if (depositor_name) funcArgs.depositor_name = depositor_name;
if (amount) funcArgs.amount = Number(amount);
if (transaction_id) funcArgs.transaction_id = Number(transaction_id);
if (limit) funcArgs.limit = Number(limit);
console.log('[manage_deposit] executeDepositFunction 호출:', funcName, funcArgs);
const result = await executeDepositFunction(funcName, funcArgs, context);
console.log('[manage_deposit] 결과:', JSON.stringify(result).slice(0, 200));
// 결과 포맷팅 (고정 형식)
return formatDepositResult(action, result);
} catch (error) {
console.error('[manage_deposit] 오류:', error);
return `🚫 예치금 처리 오류: ${String(error)}`;
}
}

725
src/tools/domain-tool.ts Normal file
View File

@@ -0,0 +1,725 @@
import type { Env } from '../types';
// Cloudflare AI Gateway를 통해 OpenAI API 호출 (지역 제한 우회)
const OPENAI_API_URL = 'https://gateway.ai.cloudflare.com/v1/d8e5997eb4040f8b489f09095c0f623c/telegram-bot/openai/chat/completions';
export const manageDomainTool = {
type: 'function',
function: {
name: 'manage_domain',
description: '도메인 관리 및 정보 조회. ".com 가격", ".io 가격" 같은 TLD 가격 조회, 도메인 등록, WHOIS 조회, 네임서버 관리 등을 처리합니다.',
parameters: {
type: 'object',
properties: {
action: {
type: 'string',
enum: ['register', 'check', 'whois', 'list', 'info', 'get_ns', 'set_ns', 'price', 'cheapest'],
description: 'price: TLD 가격 조회 (.com 가격, .io 가격), cheapest: 가장 저렴한 TLD 목록 조회, register: 도메인 등록, check: 가용성 확인, whois: WHOIS 조회, list: 내 도메인 목록, info: 도메인 상세정보, get_ns/set_ns: 네임서버 조회/변경',
},
domain: {
type: 'string',
description: '대상 도메인 또는 TLD (예: example.com, .com, com). price action에서는 TLD만 전달 가능',
},
nameservers: {
type: 'array',
items: { type: 'string' },
description: '설정할 네임서버 목록. set_ns action에만 필요 (예: ["ns1.example.com", "ns2.example.com"])',
},
tld: {
type: 'string',
description: 'TLD. price action에서 사용 (예: tld="com" 또는 domain=".com" 또는 domain="com" 모두 가능)',
},
},
required: ['action'],
},
},
};
export const suggestDomainsTool = {
type: 'function',
function: {
name: 'suggest_domains',
description: '키워드나 비즈니스 설명을 기반으로 도메인 이름을 추천합니다. 창의적인 도메인 아이디어를 생성하고 가용성을 확인하여 등록 가능한 도메인만 가격과 함께 제안합니다. "도메인 추천", "도메인 제안", "도메인 아이디어" 등의 요청에 사용하세요.',
parameters: {
type: 'object',
properties: {
keywords: {
type: 'string',
description: '도메인 추천을 위한 키워드나 비즈니스 설명 (예: 커피숍, IT 스타트업, 서버 호스팅)',
},
},
required: ['keywords'],
},
},
};
// Namecheap API 호출 (allowedDomains로 필터링)
async function callNamecheapApi(
funcName: string,
funcArgs: Record<string, any>,
allowedDomains: string[],
env?: Env,
telegramUserId?: string,
db?: D1Database,
userId?: number
): Promise<any> {
if (!env?.NAMECHEAP_API_KEY_INTERNAL) {
return { error: 'Namecheap API 키가 설정되지 않았습니다.' };
}
const apiKey = env.NAMECHEAP_API_KEY_INTERNAL;
const apiUrl = 'https://namecheap-api.anvil.it.com';
// 도메인 권한 체크 (쓰기 작업만)
// 읽기 작업(get_domain_info, get_nameservers)은 누구나 조회 가능
if (['set_nameservers', 'create_child_ns', 'delete_child_ns'].includes(funcName)) {
if (!allowedDomains.includes(funcArgs.domain)) {
return { error: `권한 없음: ${funcArgs.domain}은 관리할 수 없는 도메인입니다.` };
}
}
switch (funcName) {
case 'list_domains': {
const result = await fetch(`${apiUrl}/domains?page=${funcArgs.page || 1}&page_size=${funcArgs.page_size || 100}`, {
headers: { 'X-API-Key': apiKey },
}).then(r => r.json()) as any[];
// MM/DD/YYYY → YYYY-MM-DD 변환 (Namecheap은 미국 형식 사용)
const convertDate = (date: string) => {
const [month, day, year] = date.split('/');
return `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}`;
};
// 허용된 도메인만 필터링, 날짜는 ISO 형식으로 변환
return result
.filter((d: any) => allowedDomains.includes(d.name))
.map((d: any) => ({
...d,
created: convertDate(d.created),
expires: convertDate(d.expires),
user: undefined, // 민감 정보 제거
}));
}
case 'get_domain_info': {
// 목록 API에서 더 많은 정보 조회 (단일 API는 정보 부족)
const domains = await fetch(`${apiUrl}/domains?page=1&page_size=100`, {
headers: { 'X-API-Key': apiKey },
}).then(r => r.json()) as any[];
const domainInfo = domains.find((d: any) => d.name === funcArgs.domain);
if (!domainInfo) {
return { error: `도메인을 찾을 수 없습니다: ${funcArgs.domain}` };
}
// MM/DD/YYYY → YYYY-MM-DD 변환 (Namecheap은 미국 형식 사용)
const convertDate = (date: string) => {
const [month, day, year] = date.split('/');
return `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}`;
};
// 민감 정보 필터링 (user/owner 제거), 날짜는 ISO 형식으로 변환
return {
domain: domainInfo.name,
created: convertDate(domainInfo.created),
expires: convertDate(domainInfo.expires),
is_expired: domainInfo.is_expired,
auto_renew: domainInfo.auto_renew,
is_locked: domainInfo.is_locked,
whois_guard: domainInfo.whois_guard,
};
}
case 'get_nameservers':
return fetch(`${apiUrl}/dns/${funcArgs.domain}/nameservers`, {
headers: { 'X-API-Key': apiKey },
}).then(r => r.json());
case 'set_nameservers': {
const res = await fetch(`${apiUrl}/dns/${funcArgs.domain}/nameservers`, {
method: 'PUT',
headers: { 'X-API-Key': apiKey, 'Content-Type': 'application/json' },
body: JSON.stringify({ domain: funcArgs.domain, nameservers: funcArgs.nameservers }),
});
const text = await res.text();
if (!res.ok) {
// Namecheap 에러 메시지 파싱
if (text.includes('subordinate hosts') || text.includes('Non existen')) {
return {
error: `네임서버 변경 실패: ${funcArgs.nameservers.join(', ')}는 등록되지 않은 네임서버입니다. 자기 도메인을 네임서버로 사용하려면 먼저 Namecheap에서 Child Nameserver(글루 레코드)를 IP 주소와 함께 등록해야 합니다.`
};
}
return { error: `네임서버 변경 실패: ${text}` };
}
try {
return JSON.parse(text);
} catch {
return { success: true, message: text };
}
}
case 'create_child_ns': {
const res = await fetch(`${apiUrl}/dns/${funcArgs.domain}/childns`, {
method: 'POST',
headers: { 'X-API-Key': apiKey, 'Content-Type': 'application/json' },
body: JSON.stringify({ nameserver: funcArgs.nameserver, ip: funcArgs.ip }),
});
const data = await res.json() as any;
if (!res.ok) {
return { error: data.detail || `Child NS 생성 실패` };
}
return data;
}
case 'get_child_ns': {
const res = await fetch(`${apiUrl}/dns/${funcArgs.domain}/childns/${funcArgs.nameserver}`, {
headers: { 'X-API-Key': apiKey },
});
const data = await res.json() as any;
if (!res.ok) {
return { error: data.detail || `Child NS 조회 실패` };
}
return data;
}
case 'delete_child_ns': {
const res = await fetch(`${apiUrl}/dns/${funcArgs.domain}/childns/${funcArgs.nameserver}`, {
method: 'DELETE',
headers: { 'X-API-Key': apiKey },
});
const data = await res.json() as any;
if (!res.ok) {
return { error: data.detail || `Child NS 삭제 실패` };
}
return data;
}
case 'get_balance':
return fetch(`${apiUrl}/account/balance`, {
headers: { 'X-API-Key': apiKey },
}).then(r => r.json());
case 'get_price': {
const tld = funcArgs.tld?.replace(/^\./, ''); // .com → com
return fetch(`${apiUrl}/prices/${tld}`, {
headers: { 'X-API-Key': apiKey },
}).then(r => r.json());
}
case 'get_all_prices': {
return fetch(`${apiUrl}/prices`, {
headers: { 'X-API-Key': apiKey },
}).then(r => r.json());
}
case 'check_domains': {
return fetch(`${apiUrl}/domains/check`, {
method: 'POST',
headers: { 'X-API-Key': apiKey, 'Content-Type': 'application/json' },
body: JSON.stringify({ domains: funcArgs.domains }),
}).then(r => r.json());
}
case 'whois_lookup': {
// 자체 WHOIS API 서버 사용 (모든 TLD 지원)
const domain = funcArgs.domain;
try {
const whoisRes = await fetch(`https://whois-api-kappa-inoutercoms-projects.vercel.app/api/whois/${domain}`);
if (!whoisRes.ok) {
return { error: `WHOIS 조회 실패: HTTP ${whoisRes.status}` };
}
const whois = await whoisRes.json() as any;
if (whois.error) {
return { error: `WHOIS 조회 오류: ${whois.error}` };
}
// ccSLD WHOIS 미지원 처리
if (whois.whois_supported === false) {
return {
domain: whois.domain,
whois_supported: false,
ccSLD: whois.ccSLD,
message: whois.message_ko,
suggestion: whois.suggestion_ko,
};
}
// raw WHOIS 응답을 그대로 반환 (AI가 파싱)
return {
domain: whois.domain,
available: whois.available,
whois_server: whois.whois_server,
raw: whois.raw,
query_time_ms: whois.query_time_ms,
};
} catch (error) {
return { error: `WHOIS 조회 오류: ${String(error)}` };
}
}
case 'register_domain': {
if (!telegramUserId) {
return { error: '도메인 등록에는 로그인이 필요합니다.' };
}
const res = await fetch(`${apiUrl}/domains/register`, {
method: 'POST',
headers: { 'X-API-Key': apiKey, 'Content-Type': 'application/json' },
body: JSON.stringify({
domain: funcArgs.domain,
years: funcArgs.years || 1,
telegram_id: telegramUserId,
}),
});
const result = await res.json() as any;
if (!res.ok) {
return { error: result.detail || '도메인 등록 실패' };
}
// 등록 성공 시 user_domains 테이블에 추가
if (result.registered && db && userId) {
try {
await db.prepare(
'INSERT INTO user_domains (user_id, domain, verified, created_at) VALUES (?, ?, 1, datetime("now"))'
).bind(userId, funcArgs.domain).run();
console.log(`[register_domain] user_domains에 추가: user_id=${userId}, domain=${funcArgs.domain}`);
} catch (dbError) {
console.error('[register_domain] user_domains 추가 실패:', dbError);
result.warning = result.warning || '';
result.warning += ' (DB 기록 실패 - 수동 추가 필요)';
}
}
return result;
}
default:
return { error: `Unknown function: ${funcName}` };
}
}
// 도메인 작업 직접 실행 (Agent 없이 코드로 처리)
async function executeDomainAction(
action: string,
args: { domain?: string; nameservers?: string[]; tld?: string },
allowedDomains: string[],
env?: Env,
telegramUserId?: string,
db?: D1Database,
userId?: number
): Promise<string> {
const { domain, nameservers, tld } = args;
switch (action) {
case 'list': {
const result = await callNamecheapApi('list_domains', {}, allowedDomains, env, telegramUserId, db, userId);
if (result.error) return `🚫 ${result.error}`;
if (!result.length) return '📋 등록된 도메인이 없습니다.';
const list = result.map((d: any) => `${d.name} (만료: ${d.expires})`).join('\n');
return `📋 내 도메인 목록 (${result.length}개)\n\n${list}`;
}
case 'info': {
if (!domain) return '🚫 도메인을 지정해주세요.';
const result = await callNamecheapApi('get_domain_info', { domain }, allowedDomains, env, telegramUserId, db, userId);
if (result.error) return `🚫 ${result.error}`;
return `📋 ${domain} 정보\n\n• 생성일: ${result.created}\n• 만료일: ${result.expires}\n• 자동갱신: ${result.auto_renew ? '✅' : '❌'}\n• 잠금: ${result.is_locked ? '🔒' : '🔓'}\n• WHOIS Guard: ${result.whois_guard ? '✅' : '❌'}`;
}
case 'get_ns': {
if (!domain) return '🚫 도메인을 지정해주세요.';
const result = await callNamecheapApi('get_nameservers', { domain }, allowedDomains, env, telegramUserId, db, userId);
if (result.error) return `🚫 ${result.error}`;
const nsList = (result.nameservers || result).map((ns: string) => `${ns}`).join('\n');
return `🌐 ${domain} 네임서버\n\n${nsList}`;
}
case 'set_ns': {
if (!domain) return '🚫 도메인을 지정해주세요.';
if (!nameservers?.length) return '🚫 네임서버를 지정해주세요.';
if (!allowedDomains.includes(domain)) return `🚫 ${domain}은 관리 권한이 없습니다.`;
const result = await callNamecheapApi('set_nameservers', { domain, nameservers }, allowedDomains, env, telegramUserId, db, userId);
if (result.error) return `🚫 ${result.error}`;
return `${domain} 네임서버 변경 완료\n\n${nameservers.map(ns => `${ns}`).join('\n')}`;
}
case 'check': {
if (!domain) return '🚫 도메인을 지정해주세요.';
const result = await callNamecheapApi('check_domains', { domains: [domain] }, allowedDomains, env, telegramUserId, db, userId);
if (result.error) return `🚫 ${result.error}`;
const available = result[domain];
if (available) {
// 가격도 함께 조회
const domainTld = domain.split('.').pop() || '';
const priceResult = await callNamecheapApi('get_price', { tld: domainTld }, allowedDomains, env, telegramUserId, db, userId);
const price = priceResult.krw || priceResult.register_krw;
return `${domain}은 등록 가능합니다.\n\n💰 가격: ${price?.toLocaleString()}원/년\n\n등록하시려면 "${domain} 등록해줘"라고 말씀해주세요.`;
}
return `${domain}은 이미 등록된 도메인입니다.`;
}
case 'whois': {
if (!domain) return '🚫 도메인을 지정해주세요.';
const result = await callNamecheapApi('whois_lookup', { domain }, allowedDomains, env, telegramUserId, db, userId);
if (result.error) return `🚫 ${result.error}`;
// ccSLD WHOIS 미지원
if (result.whois_supported === false) {
return `🔍 ${domain} WHOIS\n\n⚠ ${result.message}\n💡 ${result.suggestion}`;
}
// raw WHOIS 데이터에서 주요 정보 추출
const raw = result.raw || '';
const extractField = (patterns: RegExp[]): string => {
for (const pattern of patterns) {
const match = raw.match(pattern);
if (match) return match[1].trim();
}
return '-';
};
const created = extractField([
/Creation Date:\s*(.+)/i,
/Created Date:\s*(.+)/i,
/Registration Date:\s*(.+)/i,
/created:\s*(.+)/i,
]);
const expires = extractField([
/Registry Expiry Date:\s*(.+)/i,
/Expiration Date:\s*(.+)/i,
/Expiry Date:\s*(.+)/i,
/expires:\s*(.+)/i,
]);
const updated = extractField([
/Updated Date:\s*(.+)/i,
/Last Updated:\s*(.+)/i,
/modified:\s*(.+)/i,
]);
const registrar = extractField([
/Registrar:\s*(.+)/i,
/Sponsoring Registrar:\s*(.+)/i,
]);
const registrarUrl = extractField([
/Registrar URL:\s*(.+)/i,
]);
const registrant = extractField([
/Registrant Organization:\s*(.+)/i,
/Registrant Name:\s*(.+)/i,
/org:\s*(.+)/i,
]);
const registrantCountry = extractField([
/Registrant Country:\s*(.+)/i,
/Registrant State\/Province:\s*(.+)/i,
]);
const statusMatch = raw.match(/Domain Status:\s*(.+)/gi);
const statuses = statusMatch
? statusMatch.map((s: string) => s.replace(/Domain Status:\s*/i, '').split(' ')[0].trim()).slice(0, 3)
: [];
const dnssec = extractField([
/DNSSEC:\s*(.+)/i,
]);
const nsMatch = raw.match(/Name Server:\s*(.+)/gi);
const nameservers = nsMatch
? nsMatch.map((ns: string) => ns.replace(/Name Server:\s*/i, '').trim()).slice(0, 4)
: [];
let response = `🔍 ${domain} WHOIS 정보\n\n`;
response += `📅 날짜\n`;
response += `• 등록일: ${created}\n`;
response += `• 만료일: ${expires}\n`;
if (updated !== '-') response += `• 수정일: ${updated}\n`;
response += `\n🏢 등록 정보\n`;
response += `• 등록기관: ${registrar}\n`;
if (registrarUrl !== '-') response += `• URL: ${registrarUrl}\n`;
if (registrant !== '-') response += `• 등록자: ${registrant}\n`;
if (registrantCountry !== '-') response += `• 국가: ${registrantCountry}\n`;
response += `\n🌐 기술 정보\n`;
response += `• 네임서버: ${nameservers.length ? nameservers.join(', ') : '-'}\n`;
if (statuses.length) response += `• 상태: ${statuses.join(', ')}\n`;
if (dnssec !== '-') response += `• DNSSEC: ${dnssec}`;
if (result.available === true) {
response += `\n\n✅ 이 도메인은 등록 가능합니다!`;
}
return response.trim();
}
case 'price': {
// tld, domain, 또는 ".com" 형식 모두 지원
let targetTld = tld || domain?.replace(/^\./, '').split('.').pop();
if (!targetTld) return '🚫 TLD를 지정해주세요. (예: com, io, net)';
const result = await callNamecheapApi('get_price', { tld: targetTld }, allowedDomains, env, telegramUserId, db, userId);
if (result.error) return `🚫 ${result.error}`;
// API 응답: { tld, usd, krw }
const price = result.krw || result.register_krw;
return `💰 .${targetTld} 도메인 가격\n\n• 등록/갱신: ${price?.toLocaleString()}원/년`;
}
case 'cheapest': {
const result = await callNamecheapApi('get_all_prices', {}, allowedDomains, env, telegramUserId, db, userId);
if (result.error) return `🚫 ${result.error}`;
// 가격 > 0인 TLD만 필터링, krw 기준 정렬
const sorted = (result as any[])
.filter((p: any) => p.krw > 0)
.sort((a: any, b: any) => a.krw - b.krw)
.slice(0, 15);
if (sorted.length === 0) {
return '🚫 TLD 가격 정보를 가져올 수 없습니다.';
}
const list = sorted.map((p: any, i: number) =>
`${i + 1}. .${p.tld} - ${p.krw.toLocaleString()}원/년`
).join('\n');
return `💰 가장 저렴한 TLD TOP 15\n\n${list}\n\n💡 특정 TLD 가격은 ".com 가격" 형식으로 조회`;
}
case 'register': {
if (!domain) return '🚫 등록할 도메인을 지정해주세요.';
if (!telegramUserId) return '🚫 도메인 등록에는 로그인이 필요합니다.';
// 1. 가용성 확인
const checkResult = await callNamecheapApi('check_domains', { domains: [domain] }, allowedDomains, env, telegramUserId, db, userId);
if (checkResult.error) return `🚫 ${checkResult.error}`;
if (!checkResult[domain]) return `${domain}은 이미 등록된 도메인입니다.`;
// 2. 가격 조회
const domainTld = domain.split('.').pop() || '';
const priceResult = await callNamecheapApi('get_price', { tld: domainTld }, allowedDomains, env, telegramUserId, db, userId);
if (priceResult.error) return `🚫 가격 조회 실패: ${priceResult.error}`;
const price = priceResult.krw || priceResult.register_krw;
// 3. 잔액 조회
let balance = 0;
if (db && userId) {
const balanceRow = await db.prepare('SELECT balance FROM user_deposits WHERE user_id = ?').bind(userId).first<{ balance: number }>();
balance = balanceRow?.balance || 0;
}
// 4. 확인 페이지 생성 (인라인 버튼 포함)
if (balance >= price) {
// 버튼 데이터를 특수 마커로 포함
const keyboardData = JSON.stringify({
type: 'domain_register',
domain: domain,
price: price
});
return `__KEYBOARD__${keyboardData}__END__
📋 <b>도메인 등록 확인</b>
• 도메인: <code>${domain}</code>
• 가격: ${price.toLocaleString()}원 (예치금에서 차감)
• 현재 잔액: ${balance.toLocaleString()}원 ✅
• 등록 기간: 1년
📌 <b>등록자 정보</b>
서비스 기본 정보로 등록됩니다.
(WHOIS Guard가 적용되어 개인정보는 비공개)
⚠️ <b>주의사항</b>
도메인 등록 후에는 취소 및 환불이 불가능합니다.`;
} else {
const shortage = price - balance;
return `📋 <b>도메인 등록 확인</b>
• 도메인: <code>${domain}</code>
• 가격: ${price.toLocaleString()}
• 현재 잔액: ${balance.toLocaleString()}원 ⚠️ 부족
• 부족 금액: ${shortage.toLocaleString()}
💳 <b>입금 계좌</b>
하나은행 427-910018-27104 (주식회사 아이언클래드)
입금 후 '홍길동 ${shortage}원 입금' 형식으로 알려주세요.`;
}
}
default:
return `🚫 알 수 없는 작업: ${action}`;
}
}
export async function executeManageDomain(
args: { action: string; domain?: string; nameservers?: string[]; tld?: string },
env?: Env,
telegramUserId?: string,
db?: D1Database
): Promise<string> {
const { action, domain, nameservers, tld } = args;
console.log('[manage_domain] 시작:', { action, domain, telegramUserId, hasDb: !!db });
// 소유권 검증 (DB 조회)
if (!telegramUserId || !db) {
console.log('[manage_domain] 실패: telegramUserId 또는 db 없음');
return '🚫 도메인 관리 권한이 없습니다.';
}
let userDomains: string[] = [];
let userId: number | undefined;
try {
const user = await db.prepare(
'SELECT id FROM users WHERE telegram_id = ?'
).bind(telegramUserId).first<{ id: number }>();
if (!user) {
return '🚫 도메인 관리 권한이 없습니다.';
}
userId = user.id;
// 사용자 소유 도메인 전체 목록 조회
const domains = await db.prepare(
'SELECT domain FROM user_domains WHERE user_id = ? AND verified = 1'
).bind(user.id).all<{ domain: string }>();
userDomains = domains.results?.map(d => d.domain) || [];
console.log('[manage_domain] 소유 도메인:', userDomains);
} catch (error) {
console.log('[manage_domain] DB 오류:', error);
return '🚫 권한 확인 중 오류가 발생했습니다.';
}
// 코드로 직접 처리 (Agent 없이)
try {
const result = await executeDomainAction(
action,
{ domain, nameservers, tld },
userDomains,
env,
telegramUserId,
db,
userId
);
console.log('[manage_domain] 완료:', result?.slice(0, 100));
return result;
} catch (error) {
console.log('[manage_domain] 오류:', error);
return `🚫 도메인 관리 오류: ${String(error)}`;
}
}
export async function executeSuggestDomains(args: { keywords: string }, env?: Env): Promise<string> {
const { keywords } = args;
console.log('[suggest_domains] 시작:', { keywords });
if (!env?.OPENAI_API_KEY) {
return '🚫 도메인 추천 기능이 설정되지 않았습니다. (OPENAI_API_KEY 미설정)';
}
if (!env?.NAMECHEAP_API_KEY) {
return '🚫 도메인 추천 기능이 설정되지 않았습니다. (NAMECHEAP_API_KEY 미설정)';
}
try {
const namecheapApiUrl = 'https://namecheap-api.anvil.it.com';
const TARGET_COUNT = 10;
const MAX_RETRIES = 3;
const availableDomains: { domain: string; price?: number }[] = [];
const checkedDomains = new Set<string>();
let retryCount = 0;
// 10개 이상 등록 가능 도메인을 찾을 때까지 반복
while (availableDomains.length < TARGET_COUNT && retryCount < MAX_RETRIES) {
retryCount++;
const excludeList = [...checkedDomains].slice(-30).join(', ');
// Step 1: GPT에게 도메인 아이디어 생성 요청
const ideaResponse = await fetch(OPENAI_API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${env.OPENAI_API_KEY}`,
},
body: JSON.stringify({
model: 'gpt-4o-mini',
messages: [
{
role: 'system',
content: `당신은 도메인 이름 전문가입니다. 주어진 키워드/비즈니스 설명을 바탕으로 창의적이고 기억하기 쉬운 도메인 이름을 제안합니다.
규칙:
- 정확히 15개의 도메인 이름을 제안하세요
- 다양한 TLD 사용: .com, .io, .net, .co, .app, .dev, .site, .xyz, .me
- 짧고 기억하기 쉬운 이름 (2-3 단어 조합)
- 트렌디한 접미사 활용: hub, lab, spot, nest, base, cloud, stack, flow, zone, pro
- JSON 배열로만 응답하세요. 설명 없이 도메인 목록만.
${excludeList ? `- 다음 도메인은 제외하세요: ${excludeList}` : ''}
예시 응답:
["coffeenest.com", "brewlab.io", "beanspot.co"]`
},
{
role: 'user',
content: `키워드: ${keywords}`
}
],
max_tokens: 500,
temperature: 0.9,
}),
});
if (!ideaResponse.ok) {
if (availableDomains.length > 0) break; // 이미 찾은 게 있으면 그것으로 진행
return '🚫 도메인 아이디어 생성 중 오류가 발생했습니다.';
}
const ideaData = await ideaResponse.json() as any;
const ideaContent = ideaData.choices?.[0]?.message?.content || '[]';
let domains: string[];
try {
domains = JSON.parse(ideaContent);
if (!Array.isArray(domains)) domains = [];
} catch {
const domainRegex = /[\w-]+\.(com|io|net|co|app|dev|site|org|xyz|me)/gi;
domains = ideaContent.match(domainRegex) || [];
}
// 이미 체크한 도메인 제외
const newDomains = domains.filter(d => !checkedDomains.has(d.toLowerCase()));
if (newDomains.length === 0) continue;
newDomains.forEach(d => checkedDomains.add(d.toLowerCase()));
// Step 2: 가용성 확인
const checkResponse = await fetch(`${namecheapApiUrl}/domains/check`, {
method: 'POST',
headers: {
'X-API-Key': env.NAMECHEAP_API_KEY,
'Content-Type': 'application/json',
},
body: JSON.stringify({ domains: newDomains }),
});
if (!checkResponse.ok) continue;
const checkRaw = await checkResponse.json() as Record<string, boolean>;
// 등록 가능한 도메인만 추가
for (const [domain, isAvailable] of Object.entries(checkRaw)) {
if (isAvailable && availableDomains.length < TARGET_COUNT) {
availableDomains.push({ domain });
}
}
}
if (availableDomains.length === 0) {
return `🎯 **${keywords}** 관련 도메인:\n\n❌ 등록 가능한 도메인을 찾지 못했습니다.\n다른 키워드로 다시 시도해주세요.`;
}
// Step 3: 가격 조회
const tldPrices: Record<string, number> = {};
const uniqueTlds = [...new Set(availableDomains.map(d => d.domain.split('.').pop() || ''))];
for (const tld of uniqueTlds) {
try {
const priceRes = await fetch(`${namecheapApiUrl}/prices/${tld}`, {
headers: { 'X-API-Key': env.NAMECHEAP_API_KEY },
});
if (priceRes.ok) {
const priceData = await priceRes.json() as { krw?: number };
tldPrices[tld] = priceData.krw || 0;
}
} catch {
// 가격 조회 실패 시 무시
}
}
// Step 4: 결과 포맷팅 (등록 가능한 것만)
let response = `🎯 **${keywords}** 관련 도메인:\n\n`;
availableDomains.forEach((d, i) => {
const tld = d.domain.split('.').pop() || '';
const price = tldPrices[tld];
const priceStr = price ? `${price.toLocaleString()}원/년` : '가격 조회 중';
response += `${i + 1}. ${d.domain} - ${priceStr}\n`;
});
response += `\n등록하시려면 번호나 도메인명을 말씀해주세요.`;
return response;
} catch (error) {
console.error('[suggestDomains] 오류:', error);
return `🚫 도메인 추천 중 오류가 발생했습니다: ${String(error)}`;
}
}

104
src/tools/index.ts Normal file
View File

@@ -0,0 +1,104 @@
// Tool Registry - All tools exported from here
import { weatherTool, executeWeather } from './weather-tool';
import { searchWebTool, lookupDocsTool, executeSearchWeb, executeLookupDocs } from './search-tool';
import { manageDomainTool, suggestDomainsTool, executeManageDomain, executeSuggestDomains } from './domain-tool';
import { manageDepositTool, executeManageDeposit } from './deposit-tool';
import { getCurrentTimeTool, calculateTool, executeGetCurrentTime, executeCalculate } from './utility-tools';
import type { Env } from '../types';
// All tools array (used by OpenAI API)
export const tools = [
weatherTool,
searchWebTool,
getCurrentTimeTool,
calculateTool,
lookupDocsTool,
manageDomainTool,
manageDepositTool,
suggestDomainsTool,
];
// Tool categories for dynamic loading
export const TOOL_CATEGORIES: Record<string, string[]> = {
domain: ['manage_domain', 'suggest_domains'],
deposit: ['manage_deposit'],
weather: ['get_weather'],
search: ['search_web', 'lookup_docs'],
utility: ['get_current_time', 'calculate'],
};
// Category detection patterns
export const CATEGORY_PATTERNS: Record<string, RegExp> = {
domain: /도메인|네임서버|whois|dns|tld|등록|\.com|\.net|\.io|\.kr|\.org/i,
deposit: /입금|충전|잔액|계좌|예치금|송금|돈/i,
weather: /날씨|기온|비|눈|맑|흐림|더워|추워/i,
search: /검색|찾아|뭐야|뉴스|최신/i,
};
// Message-based tool selection
export function selectToolsForMessage(message: string): typeof tools {
const selectedCategories = new Set<string>(['utility']); // 항상 포함
for (const [category, pattern] of Object.entries(CATEGORY_PATTERNS)) {
if (pattern.test(message)) {
selectedCategories.add(category);
}
}
// 패턴 매칭 없으면 전체 도구 사용 (폴백)
if (selectedCategories.size === 1) {
console.log('[ToolSelector] 패턴 매칭 없음 → 전체 도구 사용');
return tools;
}
const selectedNames = new Set(
[...selectedCategories].flatMap(cat => TOOL_CATEGORIES[cat] || [])
);
const selectedTools = tools.filter(t => selectedNames.has(t.function.name));
console.log('[ToolSelector] 메시지:', message);
console.log('[ToolSelector] 카테고리:', [...selectedCategories].join(', '));
console.log('[ToolSelector] 선택된 도구:', selectedTools.map(t => t.function.name).join(', '));
return selectedTools;
}
// Tool execution dispatcher
export async function executeTool(
name: string,
args: Record<string, any>,
env?: Env,
telegramUserId?: string,
db?: D1Database
): Promise<string> {
switch (name) {
case 'get_weather':
return executeWeather(args as { city: string });
case 'search_web':
return executeSearchWeb(args as { query: string }, env);
case 'lookup_docs':
return executeLookupDocs(args as { library: string; query: string });
case 'get_current_time':
return executeGetCurrentTime(args as { timezone?: string });
case 'calculate':
return executeCalculate(args as { expression: string });
case 'manage_domain':
return executeManageDomain(args as { action: string; domain?: string; nameservers?: string[]; tld?: string }, env, telegramUserId, db);
case 'suggest_domains':
return executeSuggestDomains(args as { keywords: string }, env);
case 'manage_deposit':
return executeManageDeposit(args as { action: string; depositor_name?: string; amount?: number; transaction_id?: number; limit?: number }, env, telegramUserId, db);
default:
return `알 수 없는 도구: ${name}`;
}
}

156
src/tools/search-tool.ts Normal file
View File

@@ -0,0 +1,156 @@
import type { Env } from '../types';
// Cloudflare AI Gateway를 통해 OpenAI API 호출 (지역 제한 우회)
const OPENAI_API_URL = 'https://gateway.ai.cloudflare.com/v1/d8e5997eb4040f8b489f09095c0f623c/telegram-bot/openai/chat/completions';
export const searchWebTool = {
type: 'function',
function: {
name: 'search_web',
description: '웹에서 최신 정보를 검색합니다. 실시간 가격, 뉴스, 현재 날짜 이후 정보, 특정 사실 확인이 필요할 때 반드시 사용하세요. "비트코인 가격", "오늘 뉴스", "~란", "~뭐야" 등의 질문에 사용합니다.',
parameters: {
type: 'object',
properties: {
query: {
type: 'string',
description: '검색 쿼리',
},
},
required: ['query'],
},
},
};
export const lookupDocsTool = {
type: 'function',
function: {
name: 'lookup_docs',
description: '프로그래밍 라이브러리의 공식 문서를 조회합니다. React, OpenAI, Cloudflare Workers 등의 최신 문서와 코드 예제를 검색할 수 있습니다.',
parameters: {
type: 'object',
properties: {
library: {
type: 'string',
description: '라이브러리 이름 (예: react, openai, cloudflare-workers, next.js)',
},
query: {
type: 'string',
description: '찾고 싶은 내용 (예: hooks 사용법, API 호출 방법)',
},
},
required: ['library', 'query'],
},
},
};
export async function executeSearchWeb(args: { query: string }, env?: Env): Promise<string> {
let query = args.query;
try {
if (!env?.BRAVE_API_KEY) {
return `🔍 검색 기능이 설정되지 않았습니다.`;
}
// 한글이 포함된 경우 영문으로 번역 (기술 용어, 제품명 등)
const hasKorean = /[가-힣]/.test(query);
let translatedQuery = query;
if (hasKorean && env?.OPENAI_API_KEY) {
try {
const translateRes = await fetch(OPENAI_API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${env.OPENAI_API_KEY}`,
},
body: JSON.stringify({
model: 'gpt-4o-mini',
messages: [
{
role: 'system',
content: `사용자의 검색어를 영문으로 번역하세요.
- 외래어/기술용어는 원래 영문 표기로 변환 (예: 판골린→Pangolin, 도커→Docker)
- 일반 한국어는 영문으로 번역
- 검색에 최적화된 키워드로 변환
- 번역된 검색어만 출력, 설명 없이`
},
{ role: 'user', content: query }
],
max_tokens: 100,
temperature: 0.3,
}),
});
if (translateRes.ok) {
const translateData = await translateRes.json() as any;
translatedQuery = translateData.choices?.[0]?.message?.content?.trim() || query;
console.log(`[search_web] 번역: "${query}" → "${translatedQuery}"`);
}
} catch {
// 번역 실패 시 원본 사용
}
}
const response = await fetch(
`https://api.search.brave.com/res/v1/web/search?q=${encodeURIComponent(translatedQuery)}&count=5`,
{
headers: {
'Accept': 'application/json',
'X-Subscription-Token': env.BRAVE_API_KEY,
},
}
);
if (!response.ok) {
return `🔍 검색 오류: ${response.status}`;
}
const data = await response.json() as any;
// Web 검색 결과 파싱
const webResults = data.web?.results || [];
if (webResults.length === 0) {
return `🔍 "${query}"에 대한 검색 결과가 없습니다.`;
}
const results = webResults.slice(0, 3).map((r: any, i: number) =>
`${i + 1}. <b>${r.title}</b>\n ${r.description}\n ${r.url}`
).join('\n\n');
// 번역된 경우 원본 쿼리도 표시
const queryDisplay = (hasKorean && translatedQuery !== query)
? `${query} (→ ${translatedQuery})`
: query;
return `🔍 검색 결과: ${queryDisplay}\n\n${results}`;
} catch (error) {
return `검색 중 오류가 발생했습니다: ${String(error)}`;
}
}
export async function executeLookupDocs(args: { library: string; query: string }): Promise<string> {
const { library, query } = args;
try {
// Context7 REST API 직접 호출
// 1. 라이브러리 검색
const searchUrl = `https://context7.com/api/v2/libs/search?libraryName=${encodeURIComponent(library)}&query=${encodeURIComponent(query)}`;
const searchResponse = await fetch(searchUrl);
const searchData = await searchResponse.json() as any;
if (!searchData.libraries?.length) {
return `📚 "${library}" 라이브러리를 찾을 수 없습니다.`;
}
const libraryId = searchData.libraries[0].id;
// 2. 문서 조회
const docsUrl = `https://context7.com/api/v2/context?libraryId=${encodeURIComponent(libraryId)}&query=${encodeURIComponent(query)}`;
const docsResponse = await fetch(docsUrl);
const docsData = await docsResponse.json() as any;
if (docsData.error) {
return `📚 문서 조회 실패: ${docsData.message || docsData.error}`;
}
const content = docsData.context || docsData.content || JSON.stringify(docsData, null, 2);
return `📚 ${library} 문서 (${query}):\n\n${content.slice(0, 1500)}`;
} catch (error) {
return `📚 문서 조회 중 오류: ${String(error)}`;
}
}

View File

@@ -0,0 +1,60 @@
// Utility Tools - Time and Calculator
export const getCurrentTimeTool = {
type: 'function',
function: {
name: 'get_current_time',
description: '현재 시간을 가져옵니다',
parameters: {
type: 'object',
properties: {
timezone: {
type: 'string',
description: '타임존 (예: Asia/Seoul, UTC)',
},
},
required: [],
},
},
};
export const calculateTool = {
type: 'function',
function: {
name: 'calculate',
description: '수학 계산을 수행합니다',
parameters: {
type: 'object',
properties: {
expression: {
type: 'string',
description: '계산할 수식 (예: 2+2, 100*5)',
},
},
required: ['expression'],
},
},
};
export async function executeGetCurrentTime(args: { timezone?: string }): Promise<string> {
const timezone = args.timezone || 'Asia/Seoul';
try {
const now = new Date();
const formatted = now.toLocaleString('ko-KR', { timeZone: timezone });
return `🕐 현재 시간 (${timezone}): ${formatted}`;
} catch (error) {
return `시간 정보를 가져올 수 없습니다.`;
}
}
export async function executeCalculate(args: { expression: string }): Promise<string> {
const expression = args.expression;
try {
// 안전한 수식 계산 (기본 연산만)
const sanitized = expression.replace(/[^0-9+\-*/().% ]/g, '');
const result = Function('"use strict"; return (' + sanitized + ')')();
return `🔢 계산 결과: ${expression} = ${result}`;
} catch (error) {
return `계산할 수 없는 수식입니다: ${expression}`;
}
}

37
src/tools/weather-tool.ts Normal file
View File

@@ -0,0 +1,37 @@
// Weather Tool - wttr.in integration
export const weatherTool = {
type: 'function',
function: {
name: 'get_weather',
description: '특정 도시의 현재 날씨 정보를 가져옵니다',
parameters: {
type: 'object',
properties: {
city: {
type: 'string',
description: '도시 이름 (예: Seoul, Tokyo, New York)',
},
},
required: ['city'],
},
},
};
export async function executeWeather(args: { city: string }): Promise<string> {
const city = args.city || 'Seoul';
try {
const response = await fetch(
`https://wttr.in/${encodeURIComponent(city)}?format=j1`
);
const data = await response.json() as any;
const current = data.current_condition[0];
return `🌤 ${city} 날씨
온도: ${current.temp_C}°C (체감 ${current.FeelsLikeC}°C)
상태: ${current.weatherDesc[0].value}
습도: ${current.humidity}%
풍속: ${current.windspeedKmph} km/h`;
} catch (error) {
return `날씨 정보를 가져올 수 없습니다: ${city}`;
}
}

View File

@@ -0,0 +1,48 @@
/**
* Email Decoder Utility
*
* Quoted-Printable 인코딩된 이메일 본문을 UTF-8 문자열로 디코딩합니다.
* RFC 2045 Quoted-Printable 표준을 준수합니다.
*/
/**
* Quoted-Printable UTF-8 디코딩
*
* @param str - Quoted-Printable 인코딩된 문자열
* @returns 디코딩된 UTF-8 문자열
*
* @example
* ```typescript
* const encoded = "=ED=99=8D=EA=B8=B8=EB=8F=99";
* const decoded = parseQuotedPrintable(encoded);
* console.log(decoded); // "홍길동"
* ```
*/
export function parseQuotedPrintable(str: string): string {
// 줄 연속 문자 제거 (=\r\n 또는 =\n)
str = str.replace(/=\r?\n/g, '');
// =XX 패턴을 바이트로 변환
const bytes: number[] = [];
let i = 0;
while (i < str.length) {
if (str[i] === '=' && i + 2 < str.length) {
const hex = str.slice(i + 1, i + 3);
if (/^[0-9A-Fa-f]{2}$/.test(hex)) {
bytes.push(parseInt(hex, 16));
i += 3;
continue;
}
}
bytes.push(str.charCodeAt(i));
i++;
}
// UTF-8 바이트를 문자열로 변환
try {
return new TextDecoder('utf-8').decode(new Uint8Array(bytes));
} catch {
// 디코딩 실패 시 원본 반환
return str;
}
}