-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtelegram_bot.py
More file actions
156 lines (134 loc) ยท 9.69 KB
/
Copy pathtelegram_bot.py
File metadata and controls
156 lines (134 loc) ยท 9.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# ==========================================================
# FILE: telegram_bot.py
# ==========================================================
# ๐จ VERIFIED: [Zero-Defect ์ต์ข
์ธ์ฆ] 5๋ ์ ๋ ํ๋ฒ ๋ฐ 34๋ ์ฃ์ง ์ผ์ด์ค ์๋ฒฝ ๋ฐฉ์ด ํ์ธ.
# ๐จ MODIFIED: [Phase 1 ๋๋ฉ์ธ ํธ๋ค๋ฌ ์์กด์ฑ ์ฃผ์
] TelegramCommands ๋๋ฉ์ธ ํด๋์ค๋ฅผ Importํ์ฌ ๋ด ์ ์ด ๊ถํ์ 100% ์์(Delegation)ํ๋ ๋ผ์ฐํ
๋ฐฐ์ ๊ฒฐ์.
# ๐จ MODIFIED: [Phase 2 ์ด๊ฑฐ๋ ๋ฉ์๋ 100% ์ง๊ณต ์์ถ] ํ์ผ ๋ด๋ถ์ ์์กดํ๋ ์์ฒ ์ค์ cmd_sync, cmd_record ๋ฑ 16๊ฐ ๋ช
๋ น์ด ๋ฉ์๋์ ๋ฌ๋ ฅ ํฌํผ ๋ก์ง์ 100% ์๊ตฌ ์ญ์ (์ 2ํ๋ฒ ๋จ์ผ ์ฑ
์ ์์น ์ํธ).
# ๐จ MODIFIED: [Phase 3 ๋ผ์ฐํ
๋ฐฐ์ ๋ง ๊ต์ฒด] setup_handlers ๋ฐ handle_message ๋ด๋ถ์ ๋ชจ๋ ์คํ ๊ฒฝ๋ก๋ฅผ self.commands_handler๋ก ๋ค์ด๋ ํธ ๋ฐ์ดํจ์ค ์ฒ๋ฆฌ.
# ๐จ NEW: [๋ผ์ฐํ
์์ผ๋ฆฌ์ด์ค(Alias) ๋ฝ์จ] TelegramStates ๋ฑ ์ธ๋ถ ๋ชจ๋์์ controller.cmd_* ๋ก ํธ์ถํ๋ ํ์ ํธํ์ฑ ๋ถ๊ดด๋ฅผ ๋ง๊ธฐ ์ํด __init__ ๋ด๋ถ์ 16๋ ๋ช
๋ น์ด ์์ผ๋ฆฌ์ด์ค๋ฅผ ์์์ ๋งคํ ์๋ฃ.
# ๐จ MODIFIED: [๋ก๊น
์ฆ๋ฐ ์์ฒ ์ฐจ๋จ] _is_admin ๋ด๋ถ์ print() ๋ฐ๋์ฝ๋๋ฅผ logging.warning์ผ๋ก ํฉํธ ๊ต์ ์๋ฃ.
# ๐จ MODIFIED: [์ด๊ธฐํ ๋ฐํ์ ๋ถ๊ดด ์์ ] _is_admin ๋ด๋ถ config ํ์ผ I/O ๋๊ธฐ ์ค TimeoutError ๋ฐ์ ์ ๋ผ์ฐํฐ๊ฐ ์ฆ์ฌํ๋ ํ์์ ๋ง๊ธฐ ์ํด try-except ์๋๋ฐ์ค ๊ฐ์ ์ฃผ์
.
# ๐จ MODIFIED: [์ 2ํ๋ฒ ๋ฐ๋์ฝ๋ ๊ถ๊ทน ์๊ฐ] ๋น์ฆ๋์ค ๋ก์ง ์ด๊ด์ผ๋ก ์ธํด ๋ ์ด์ ์ฌ์ฉ๋์ง ์๋ ์ ๋ น ์ํฌํธ(datetime, ZoneInfo) ๋ฐ ํ
์คํธ ํธ๋ค๋ฌ ๋ด๋ถ์ ์ ๋ น ๋ณ์(state, chat_id) 100% ์๊ตฌ ์๊ฐ ์๋ฃ.
# ==========================================================
import logging
import math
import asyncio
from telegram import Update
from telegram.ext import ContextTypes, CommandHandler, CallbackQueryHandler, MessageHandler, filters
from telegram_view import TelegramView
from telegram_sync_engine import TelegramSyncEngine
from telegram_states import TelegramStates
from telegram_callbacks import TelegramCallbacks
from telegram_commands import TelegramCommands # ๐จ NEW: [๋๋ฉ์ธ ์ฃผ๋ ์ค๊ณ] ๋ช
๋ น์ด ์ ๋ด ํธ๋ค๋ฌ ๊ฒฐ์
class TelegramController:
def __init__(self, config, broker, strategy, tx_lock=None, queue_ledger=None, strategy_rev=None):
self.cfg = config
self.broker = broker
self.strategy = strategy
self.view = TelegramView()
self.user_states = {}
self.admin_id = None
self.sync_locks = {}
self.tx_lock = tx_lock or asyncio.Lock()
self.queue_ledger = queue_ledger
self.strategy_rev = strategy_rev
# ๐จ MODIFIED: [๋๋ฉ์ธ ํธ๋ค๋ฌ ์์กด์ฑ ์ฃผ์
๋ฐ ์ธ์คํด์คํ]
self.sync_engine = TelegramSyncEngine(self.cfg, self.broker, self.strategy, self.queue_ledger, self.view, self.tx_lock, self.sync_locks)
self.states_handler = TelegramStates(self.cfg, self.broker, self.queue_ledger, self.sync_engine)
self.callbacks_handler = TelegramCallbacks(self.cfg, self.broker, self.strategy, self.queue_ledger, self.sync_engine, self.view, self.tx_lock)
self.commands_handler = TelegramCommands(self.cfg, self.broker, self.strategy, self.queue_ledger, self.sync_engine, self.view, self.tx_lock)
# ๐จ NEW: [ํ์ ํธํ์ฑ ๋ณด์ฅ ๋ฝ์จ] States ๋ฐ Callbacks ๋ชจ๋์์ controller.cmd_* ํธ์ถ ์ ๋ถ๊ดด๋์ง ์๋๋ก 16๋ ์์ผ๋ฆฌ์ด์ค(Alias) ๋ฐฐ์ ์๋ฒฝ ๋งคํ
self.cmd_start = self.commands_handler.cmd_start
self.cmd_sync = self.commands_handler.cmd_sync
self.cmd_record = self.commands_handler.cmd_record
self.cmd_history = self.commands_handler.cmd_history
self.cmd_settlement = self.commands_handler.cmd_settlement
self.cmd_seed = self.commands_handler.cmd_seed
self.cmd_ticker = self.commands_handler.cmd_ticker
self.cmd_mode = self.commands_handler.cmd_mode
self.cmd_version = self.commands_handler.cmd_version
self.cmd_queue = self.commands_handler.cmd_queue
self.cmd_add_q = self.commands_handler.cmd_add_q
self.cmd_clear_q = self.commands_handler.cmd_clear_q
self.cmd_reset = self.commands_handler.cmd_reset
self.cmd_update = self.commands_handler.cmd_update
self.cmd_avwap = self.commands_handler.cmd_avwap
self.cmd_log = self.commands_handler.cmd_log
def _safe_float(self, val):
try:
f_val = float(str(val or 0.0).replace(',', ''))
if math.isnan(f_val) or math.isinf(f_val):
return 0.0
return f_val
except Exception:
return 0.0
async def _is_admin(self, update: Update):
if not update or not update.effective_chat:
return False
if self.admin_id is None:
# ๐จ MODIFIED: [์ด๊ธฐํ ๋ฐํ์ ๋ถ๊ดด ๋ฐฉ์ด] TimeoutError ๋ฐ ํ์ผ I/O ์๋ฌ ๋ฐ์ ์ ๋ผ์ฐํฐ ์ฆ์ฌ ๋ฐฉ์ด ์๋๋ฐ์ค ์ฃผ์
try:
raw_id = await asyncio.wait_for(asyncio.to_thread(self.cfg.get_chat_id), timeout=10.0)
self.admin_id = int(self._safe_float(raw_id)) if raw_id else None
except Exception as e:
logging.error(f"๐จ ๊ด๋ฆฌ์ ID ํธ์ถ ์ค ํ์์์/์๋ฌ ๋ฐ์: {e}")
self.admin_id = None
if self.admin_id is None or self.admin_id <= 0:
# ๐จ MODIFIED: [๋ก๊น
์ฆ๋ฐ ๋ฐฉ์ด] print ๋ฐ๋์ฝ๋ ์๊ฐ ๋ฐ ํ์ค ๋ก๊น
๋ํ
logging.warning("โ ๏ธ [๋ณด์ ๊ฒฝ๊ณ ] ADMIN_CHAT_ID๊ฐ ์ค์ ๋์ง ์์ ์ ์ ์๋ ์ฌ์ฉ์์ ์ ๊ทผ์ ์ฐจ๋จํ์ต๋๋ค.")
return False
return update.effective_chat.id == self.admin_id
def setup_handlers(self, application):
# ๐จ MODIFIED: [100% ์์ ๋ผ์ฐํ
] ๋ชจ๋ CommandHandler๊ฐ commands_handler๋ฅผ ๋ค์ด๋ ํธ๋ก ๋ฐ๋ผ๋ณด๋๋ก ๋ฐฐ์ ๊ต์ฒด
application.add_handler(CommandHandler("start", self.commands_handler.cmd_start))
application.add_handler(CommandHandler("sync", self.commands_handler.cmd_sync))
application.add_handler(CommandHandler("record", self.commands_handler.cmd_record))
application.add_handler(CommandHandler("history", self.commands_handler.cmd_history))
application.add_handler(CommandHandler("settlement", self.commands_handler.cmd_settlement))
application.add_handler(CommandHandler("seed", self.commands_handler.cmd_seed))
application.add_handler(CommandHandler("ticker", self.commands_handler.cmd_ticker))
application.add_handler(CommandHandler("mode", self.commands_handler.cmd_mode))
application.add_handler(CommandHandler("version", self.commands_handler.cmd_version))
application.add_handler(CommandHandler("queue", self.commands_handler.cmd_queue))
application.add_handler(CommandHandler("add_q", self.commands_handler.cmd_add_q))
application.add_handler(CommandHandler("clear_q", self.commands_handler.cmd_clear_q))
application.add_handler(CommandHandler("reset", self.commands_handler.cmd_reset))
application.add_handler(CommandHandler("update", self.commands_handler.cmd_update))
application.add_handler(CommandHandler("avwap", self.commands_handler.cmd_avwap))
application.add_handler(CommandHandler("log", self.commands_handler.cmd_log))
application.add_handler(CommandHandler("error", self.commands_handler.cmd_log))
application.add_handler(CallbackQueryHandler(self.handle_callback))
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, self.handle_message))
async def handle_callback(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
await self.callbacks_handler.handle_callback(update, context, self)
async def handle_message(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
if not await self._is_admin(update):
return
msg_obj = update.effective_message
text = ""
if msg_obj:
if msg_obj.text:
text = msg_obj.text.strip()
elif msg_obj.caption:
text = msg_obj.caption.strip()
# ๐จ MODIFIED: [100% ์์ ๋ผ์ฐํ
] ํ๊ธ ํ
์คํธ ๋ถ๊ธฐ ์ฒ๋ฆฌ ๋ผ์ฐํ
์ commands_handler๋ก ํฉํธ ๊ต์ฒด
if "ํตํฉ ์ง์์" in text or "์ง์์ ์กฐํ" in text:
return await self.commands_handler.cmd_sync(update, context)
elif "์ฅ๋ถ ๋๊ธฐํ" in text or "์ฅ๋ถ ์กฐํ" in text:
return await self.commands_handler.cmd_record(update, context)
elif "์๋ ๋ณ๊ฒฝ" in text:
return await self.commands_handler.cmd_seed(update, context)
elif "๋ชจ๋ ์ ํ" in text:
return await self.commands_handler.cmd_ticker(update, context)
elif "๋ถํ ๋ณ๊ฒฝ" in text or "ํ๊ฒฝ ์ค์ " in text or "์ธํ
" in text:
return await self.commands_handler.cmd_settlement(update, context)
elif "์ค๋์ดํผ" in text:
return await self.commands_handler.cmd_mode(update, context)
elif "๋ช
์์ ์ ๋น" in text or "์กธ์
" in text:
return await self.commands_handler.cmd_history(update, context)
elif "์์ด์" in text or "์กฐ๊ธฐ" in text or "avwap" in text.lower():
return await self.commands_handler.cmd_avwap(update, context)
elif "๋ก๊ทธ" in text or "์๋ฌ" in text:
return await self.commands_handler.cmd_log(update, context)
# ๋ช
๋ น์ด ๋ถ๊ธฐ์ ํด๋นํ์ง ์์ผ๋ฉด State ์ฒ๋ฆฌ๋ฅผ ์ํด states_handler ๋ก ์์
await self.states_handler.handle_message(update, context, self)