- Email Routing에서 수신한 이메일 파싱 수정 - Quoted-Printable UTF-8 디코딩 함수 추가 - HTML <br/> 태그를 줄바꿈으로 변환 - SMS 키워드 위치 기반 본문 추출 - 레거시 코드 삭제 - /api/bank-notification 엔드포인트 제거 (Email Routing으로 대체) - BANK_API_SECRET 관련 코드 및 문서 제거 - DEPOSIT_AGENT_ID 제거 (Assistants API → 코드 직접 처리) - CLI 테스트 클라이언트 개선 - .env 파일 자동 로드 지원 - WEBHOOK_SECRET 환경변수 불필요 - 문서 업데이트 - NAMECHEAP_API_KEY 설명 명확화 (래퍼 인증 키) - CLI 테스트 섹션 추가 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
118 lines
2.8 KiB
TypeScript
118 lines
2.8 KiB
TypeScript
#!/usr/bin/env npx tsx
|
|
/**
|
|
* Telegram Bot CLI Chat Client
|
|
* - Worker의 /api/test 엔드포인트를 통해 직접 대화
|
|
* - 사용법: npm run chat
|
|
* 또는: npm run chat "메시지"
|
|
*/
|
|
|
|
import * as readline from 'readline';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
|
|
// .env 파일 로드
|
|
const envPath = path.join(process.cwd(), '.env');
|
|
if (fs.existsSync(envPath)) {
|
|
const envContent = fs.readFileSync(envPath, 'utf-8');
|
|
for (const line of envContent.split('\n')) {
|
|
const [key, ...valueParts] = line.split('=');
|
|
if (key && valueParts.length > 0) {
|
|
process.env[key.trim()] = valueParts.join('=').trim();
|
|
}
|
|
}
|
|
}
|
|
|
|
const WORKER_URL = process.env.WORKER_URL || 'https://telegram-summary-bot.kappa-d8e.workers.dev';
|
|
const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET;
|
|
const USER_ID = process.env.TELEGRAM_USER_ID || '821596605';
|
|
|
|
interface TestResponse {
|
|
input: string;
|
|
response: string;
|
|
user_id: string;
|
|
error?: string;
|
|
}
|
|
|
|
async function sendMessage(text: string): Promise<string> {
|
|
if (!WEBHOOK_SECRET) {
|
|
return '❌ WEBHOOK_SECRET 환경변수가 필요합니다.\n export WEBHOOK_SECRET="your-secret"';
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(`${WORKER_URL}/api/test`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
text,
|
|
user_id: USER_ID,
|
|
secret: WEBHOOK_SECRET,
|
|
}),
|
|
});
|
|
|
|
const data = await response.json() as TestResponse;
|
|
|
|
if (data.error) {
|
|
return `❌ Error: ${data.error}`;
|
|
}
|
|
|
|
return data.response;
|
|
} catch (error) {
|
|
return `❌ Request failed: ${error}`;
|
|
}
|
|
}
|
|
|
|
async function interactiveMode() {
|
|
console.log('🤖 Telegram Bot CLI');
|
|
console.log(`📡 ${WORKER_URL}`);
|
|
console.log(`👤 User: ${USER_ID}`);
|
|
console.log('─'.repeat(40));
|
|
console.log('메시지를 입력하세요. 종료: exit\n');
|
|
|
|
const rl = readline.createInterface({
|
|
input: process.stdin,
|
|
output: process.stdout,
|
|
});
|
|
|
|
const prompt = () => {
|
|
rl.question('\x1b[36m>\x1b[0m ', async (input) => {
|
|
const text = input.trim();
|
|
|
|
if (text === 'exit' || text === 'quit' || text === 'q') {
|
|
console.log('👋 종료');
|
|
rl.close();
|
|
process.exit(0);
|
|
}
|
|
|
|
if (!text) {
|
|
prompt();
|
|
return;
|
|
}
|
|
|
|
console.log('\x1b[33m⏳ 처리 중...\x1b[0m');
|
|
const response = await sendMessage(text);
|
|
console.log(`\n\x1b[32m🤖\x1b[0m ${response}\n`);
|
|
prompt();
|
|
});
|
|
};
|
|
|
|
prompt();
|
|
}
|
|
|
|
async function main() {
|
|
const args = process.argv.slice(2);
|
|
|
|
if (args.length > 0) {
|
|
// 단일 메시지 모드
|
|
const text = args.join(' ');
|
|
const response = await sendMessage(text);
|
|
console.log(response);
|
|
} else {
|
|
// 대화형 모드
|
|
await interactiveMode();
|
|
}
|
|
}
|
|
|
|
main().catch(console.error);
|