import TelegramBot from "node-telegram-bot-api"; import fs from "fs"; process.env.NTBA_FIX_350 = "1"; export interface TelegramUser { site: string; chatId: number; name: string; phone: string; realtorId: string; brokerPhone?: string; } export const telegramUsers: TelegramUser[] = [ { site: "선방", chatId: 6824763190, name: "이성원", phone: "010-8098-0254", realtorId: "namyeong00", }, { site: "ALL", chatId: 6843597951, name: "강승원", phone: "010-5947-0000", realtorId: "namyeong00", }, { site: "부동산써브", chatId: 6876605367, name: "지주완", phone: "010-2716-0987", realtorId: "namyeong00", }, { site: "부동산뱅크", chatId: 6876605367, name: "지주완", phone: "010-2716-0987", realtorId: "namyeong00", }, { site: "부동산써브", chatId: 5313195485, name: "지주완", phone: "010-6377-2069", realtorId: "namyeong00", }, { site: "부동산써브", chatId: 6864925398, name: "박희영", phone: "010-5387-4521", realtorId: "namyeong00", }, { site: "부동산포스", chatId: 8155003662, name: "국성혜", phone: "010-8305-1291", realtorId: "namyeong00", brokerPhone: "010-8305-1291", }, { site: "부동산포스", chatId: 8329969238, name: "이경희", phone: "010-6346-0996", realtorId: "namyeong00", brokerPhone: "010-5947-0996", }, // { // site: "산업부동산", // chatId: 8358690326, // name: "정명진", // phone: "010-4199-9650", // realtorId: "namyeong00", // }, { site: "산업부동산", chatId: 8295512245, name: "???", phone: "010-7322-9633", realtorId: "namyeong00", }, // { // site: "ALL", // chatId: 6843597951, // name: "강승원", // phone: "010-5947-0000", // realtorId: "jdre0125", // }, ]; export const testUsers: TelegramUser[] = [ { site: "ALL", chatId: 141033632, name: "김제연", phone: "010-8873-8711", realtorId: "namyeong00", }, ]; export class TelegramService { private bot: TelegramBot; private token: string; constructor(token: string, enablePolling: boolean = false) { this.token = token; this.bot = new TelegramBot(token, { polling: enablePolling, filepath: false, }); if (enablePolling) { this.setupListeners(); } } /** * 봇 리스너 설정 */ private setupListeners() { // 모든 메시지 처리 this.bot.on("message", (msg) => { const chatId = msg.chat.id; console.log(`Message received from chatId: ${chatId}`); this.sendMessage(chatId, "Received your message"); }); } /** * 메시지 전송 */ async sendMessage(chatId: number, message: string): Promise { try { await this.bot.sendMessage(chatId, message); console.log(`✅ 메시지 전송 성공 - chatId: ${chatId}`); } catch (error) { console.error(`❌ 메시지 전송 실패 - chatId: ${chatId}`, error); throw error; } } /** * 문서 전송 */ async sendDocument( chatId: number, fileName: string, caption?: string ): Promise { try { const buffer = Buffer.from(await Bun.file(fileName).arrayBuffer()); await this.bot.sendDocument( chatId, buffer, { caption: caption || "" }, { filename: fileName.split("/").pop(), contentType: "application/octet-stream", } ); console.log(`✅ 문서 전송 성공 - chatId: ${chatId}, file: ${fileName}`); } catch (error) { console.error( `❌ 문서 전송 실패 - chatId: ${chatId}, file: ${fileName}`, error ); throw error; } } /** * 사진 전송 */ async sendPhoto( chatId: number, photo: string | Buffer, caption?: string ): Promise { try { await this.bot.sendPhoto(chatId, photo, { caption: caption || "", }); console.log(`✅ 사진 전송 성공 - chatId: ${chatId}`); } catch (error) { console.error(`❌ 사진 전송 실패 - chatId: ${chatId}`, error); throw error; } } /** * 여러 사용자에게 메시지 전송 */ async broadcastMessage( users: TelegramUser[], message: string ): Promise { const promises = users.map((user) => this.sendMessage(user.chatId, message).catch((error) => { console.error( `❌ ${user.name}(${user.chatId})에게 메시지 전송 실패:`, error ); }) ); await Promise.allSettled(promises); console.log(`✅ ${users.length}명에게 메시지 브로드캐스트 완료`); } /** * realtorId로 사용자 필터링 */ getUsersByRealtorId( users: TelegramUser[], realtorId: string ): TelegramUser[] { return users.filter((user) => user.realtorId === realtorId); } /** * site로 사용자 필터링 */ getUsersBySite(users: TelegramUser[], site: string): TelegramUser[] { return users.filter((user) => user.site === site || user.site === "ALL"); } /** * 봇 중지 */ stopPolling(): void { this.bot.stopPolling(); console.log("✅ 텔레그램 봇 폴링 중지"); } } // 기본 토큰으로 싱글톤 인스턴스 생성 const DEFAULT_TOKEN = "233460568:AAHWgRQo5IgcWR0uXdsiMEzNnsmIqjOgk24"; export const telegramService = new TelegramService(DEFAULT_TOKEN, false);