forked from Alishahryar1/free-claude-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram.py
More file actions
300 lines (266 loc) · 10.4 KB
/
Copy pathtelegram.py
File metadata and controls
300 lines (266 loc) · 10.4 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
"""Telegram messaging runtime."""
import asyncio
import contextlib
import os
from collections.abc import Awaitable, Callable
# Opt-in to future behavior for python-telegram-bot (retry_after as timedelta).
os.environ["PTB_TIMEDELTA"] = "1"
from loguru import logger
from free_claude_code.core.diagnostics import format_user_error_preview
from ..limiter import MessagingRateLimiter
from ..models import IncomingMessage, MessageScope
from ..rendering.telegram_markdown import escape_md_v2
from ..voice import Transcriber, VoiceCancellationResult
from .ports import InboundMessageHandler
from .telegram_inbound import (
telegram_text_message_from_update,
telegram_voice_request_from_update,
)
from .telegram_io import TelegramMessenger
from .voice_flow import VoiceNoteFlow
try:
from telegram import Update
from telegram.ext import (
Application,
CommandHandler,
ContextTypes,
MessageHandler,
filters,
)
from telegram.request import HTTPXRequest
TELEGRAM_AVAILABLE = True
except ImportError:
TELEGRAM_AVAILABLE = False
class TelegramRuntime:
"""Owns Telegram SDK lifecycle and inbound event handoff."""
name = "telegram"
def __init__(
self,
bot_token: str | None = None,
allowed_user_id: str | None = None,
*,
telegram_proxy_url: str | None = None,
limiter: MessagingRateLimiter,
transcriber: Transcriber | None,
log_raw_messaging_content: bool = False,
log_api_error_tracebacks: bool = False,
) -> None:
if not TELEGRAM_AVAILABLE:
raise ImportError(
"python-telegram-bot is required. Install with: pip install python-telegram-bot"
)
self.bot_token = bot_token
self.allowed_user_id = allowed_user_id
self.telegram_proxy_url = telegram_proxy_url
if not self.bot_token:
logger.warning("TELEGRAM_BOT_TOKEN not set")
self._application: Application | None = None
self._message_handler: InboundMessageHandler | None = None
self._connected = False
self._limiter = limiter
self.outbound = TelegramMessenger(
get_application=lambda: self._application,
limiter=limiter,
)
self._voice_flow = VoiceNoteFlow(
transcriber=transcriber,
log_raw_messaging_content=log_raw_messaging_content,
log_api_error_tracebacks=log_api_error_tracebacks,
)
self._log_raw_messaging_content = log_raw_messaging_content
self._log_api_error_tracebacks = log_api_error_tracebacks
async def cancel_pending_voice(
self, scope: MessageScope, reply_id: str
) -> VoiceCancellationResult | None:
"""Cancel a pending voice transcription."""
return await self._voice_flow.cancel_pending_voice(scope, reply_id)
async def cancel_all_pending_voices(
self,
) -> tuple[VoiceCancellationResult, ...]:
"""Cancel every pending voice transcription and handoff."""
return await self._voice_flow.cancel_all_pending_voices()
async def cancel_pending_voices_in_scope(
self,
scope: MessageScope,
) -> tuple[VoiceCancellationResult, ...]:
"""Cancel pending voice transcriptions belonging to one chat."""
return await self._voice_flow.cancel_pending_voices_in_scope(scope)
async def start(self) -> None:
"""Initialize and connect to Telegram."""
if not self.bot_token:
raise ValueError("TELEGRAM_BOT_TOKEN is required")
if self.telegram_proxy_url:
request = HTTPXRequest(
connection_pool_size=8,
connect_timeout=30.0,
read_timeout=30.0,
proxy=self.telegram_proxy_url,
)
update_request = HTTPXRequest(
connection_pool_size=8,
connect_timeout=30.0,
read_timeout=30.0,
proxy=self.telegram_proxy_url,
)
builder = (
Application.builder()
.token(self.bot_token)
.request(request)
.get_updates_request(update_request)
)
else:
request = HTTPXRequest(
connection_pool_size=8, connect_timeout=30.0, read_timeout=30.0
)
builder = Application.builder().token(self.bot_token).request(request)
application = builder.build()
self._application = application
application.add_handler(
MessageHandler(filters.TEXT & (~filters.COMMAND), self._on_telegram_message)
)
application.add_handler(CommandHandler("start", self._on_start_command))
application.add_handler(
MessageHandler(filters.COMMAND, self._on_telegram_message)
)
application.add_handler(MessageHandler(filters.VOICE, self._on_telegram_voice))
await self._retry_connection_step(
application.initialize,
step="initialization",
)
await application.start()
self._limiter.start()
updater = application.updater
if updater is not None:
await self._retry_connection_step(
lambda: updater.start_polling(drop_pending_updates=False),
step="polling",
)
self._connected = True
logger.info("Telegram platform started (Bot API)")
async def _retry_connection_step(
self,
operation: Callable[[], Awaitable[object]],
*,
step: str,
) -> None:
"""Retry one independently repeatable Telegram connection step."""
max_attempts = 3
for attempt in range(1, max_attempts + 1):
try:
await operation()
return
except Exception as exc:
if attempt == max_attempts:
logger.error(
"Telegram {} failed after {} attempts",
step,
max_attempts,
)
raise
wait_time = 2 * attempt
if self._log_api_error_tracebacks:
logger.warning(
"Telegram {} failed (attempt {}/{}): {}. Retrying in {}s...",
step,
attempt,
max_attempts,
exc,
wait_time,
)
else:
logger.warning(
"Telegram {} failed (attempt {}/{}): exc_type={}. Retrying in {}s...",
step,
attempt,
max_attempts,
type(exc).__name__,
wait_time,
)
await asyncio.sleep(wait_time)
async def quiesce(self) -> None:
"""Stop Telegram ingress after draining active SDK handlers."""
application = self._application
updater = application.updater if application is not None else None
try:
if updater is not None and updater.running:
await updater.stop()
finally:
try:
if application is not None and application.running:
await application.stop()
finally:
self._connected = False
async def close(self) -> None:
"""Close Telegram delivery and initialized SDK resources."""
application = self._application
try:
await self.outbound.close()
finally:
try:
await self._limiter.shutdown()
finally:
try:
if application is not None:
await application.shutdown()
finally:
logger.info("Telegram platform closed")
def on_message(self, handler: Callable[[IncomingMessage], Awaitable[None]]) -> None:
"""Register the workflow callback for inbound messages."""
self._message_handler = handler
@property
def is_connected(self) -> bool:
"""Return whether Telegram startup completed."""
return self._connected
async def _on_start_command(
self, update: Update, context: ContextTypes.DEFAULT_TYPE
) -> None:
if update.message:
await update.message.reply_text("👋 Hello! I am the Claude Code Proxy Bot.")
await self._on_telegram_message(update, context)
async def _on_telegram_message(
self, update: Update, context: ContextTypes.DEFAULT_TYPE
) -> None:
incoming = telegram_text_message_from_update(
update,
allowed_user_id=self.allowed_user_id,
log_raw_messaging_content=self._log_raw_messaging_content,
)
if incoming is None or self._message_handler is None:
return
try:
await self._message_handler(incoming)
except Exception as e:
if self._log_api_error_tracebacks:
logger.error("Error handling message: {}", e)
else:
logger.error("Error handling message: exc_type={}", type(e).__name__)
with contextlib.suppress(Exception):
await self.outbound.send_message(
incoming.chat_id,
f"❌ *{escape_md_v2('Error:')}* {escape_md_v2(format_user_error_preview(e))}",
reply_to=incoming.message_id,
message_thread_id=incoming.message_thread_id,
parse_mode="MarkdownV2",
)
async def _on_telegram_voice(
self, update: Update, context: ContextTypes.DEFAULT_TYPE
) -> None:
message = update.message
async def _reply_text(text: str) -> None:
if message is not None:
await message.reply_text(text)
if await self._voice_flow.reply_if_disabled(_reply_text):
return
request = telegram_voice_request_from_update(
update,
context,
allowed_user_id=self.allowed_user_id,
)
if request is None:
return
await self._voice_flow.handle(
request,
message_handler=self._message_handler,
queue_send_message=self.outbound.queue_send_message,
queue_delete_messages=self.outbound.queue_delete_messages,
)