-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtelegram-bot.py
More file actions
320 lines (235 loc) · 9.47 KB
/
telegram-bot.py
File metadata and controls
320 lines (235 loc) · 9.47 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python3
"""
Telegram bot that connects to Claude Code CLI.
Setup:
1. pip install -r requirements.txt
2. Get bot token from @BotFather on Telegram
3. Get your user ID from @userinfobot on Telegram
4. Copy .env.example to .env and fill in values
5. Run: python telegram-bot.py
"""
import subprocess
import json
import os
import html
import tempfile
from pathlib import Path
import mistune
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
# Load .env file if it exists
ENV_FILE = Path(__file__).parent / ".env"
if ENV_FILE.exists():
for line in ENV_FILE.read_text().splitlines():
line = line.strip()
if line and not line.startswith("#") and "=" in line:
key, value = line.split("=", 1)
os.environ.setdefault(key.strip(), value.strip())
# Configuration
BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN")
ALLOWED_USERS = [int(x) for x in os.environ.get("ALLOWED_USERS", "").split(",") if x]
WORKSPACE = os.environ.get("CLAUDE_WORKSPACE", str(Path.home()))
CLAUDE_PATH = os.environ.get("CLAUDE_PATH", "claude")
SESSION_FILE = Path.home() / ".telegram-claude-sessions.json"
# Optional: Voice transcription (requires mlx-whisper on Apple Silicon)
try:
import mlx_whisper
VOICE_ENABLED = True
except ImportError:
VOICE_ENABLED = False
def load_sessions() -> dict:
if SESSION_FILE.exists():
return json.loads(SESSION_FILE.read_text())
return {}
def save_sessions(sessions: dict):
SESSION_FILE.write_text(json.dumps(sessions, indent=2))
def transcribe_audio(audio_path: str) -> str:
"""Transcribe audio file using mlx-whisper (Apple Silicon only)."""
if not VOICE_ENABLED:
return None
result = mlx_whisper.transcribe(
audio_path,
path_or_hf_repo="mlx-community/whisper-small-mlx",
verbose=False
)
return result.get("text", "").strip()
class TelegramRenderer(mistune.HTMLRenderer):
"""Custom renderer for Telegram-compatible HTML."""
def heading(self, text, level, **attrs):
return f"<b>{text}</b>\n\n"
def paragraph(self, text):
return f"{text}\n\n"
def list(self, text, ordered, **attrs):
return text + "\n"
def list_item(self, text, **attrs):
return f"• {text}\n"
def block_code(self, code, info=None):
escaped = html.escape(code.strip())
return f"<pre>{escaped}</pre>\n\n"
def codespan(self, text):
return f"<code>{html.escape(text)}</code>"
def emphasis(self, text):
return f"<i>{text}</i>"
def strong(self, text):
return f"<b>{text}</b>"
def strikethrough(self, text):
return f"<s>{text}</s>"
def link(self, text, url, title=None):
return f'<a href="{html.escape(url)}">{text}</a>'
def image(self, text, url, title=None):
return f'[Image: {text}]'
def block_quote(self, text):
return f"<blockquote>{text}</blockquote>\n"
def thematic_break(self):
return "\n---\n\n"
def linebreak(self):
return "\n"
def table(self, text):
return f"<pre>{text}</pre>\n\n"
def table_head(self, text):
return text + "─" * 20 + "\n"
def table_body(self, text):
return text
def table_row(self, text):
return text + "\n"
def table_cell(self, text, align=None, head=False):
if head:
return f"<b>{text}</b> │ "
return f"{text} │ "
# Create markdown parser with GFM support
md = mistune.create_markdown(
renderer=TelegramRenderer(escape=False),
plugins=['strikethrough', 'table', 'task_lists', 'url']
)
def markdown_to_telegram_html(text: str) -> str:
"""Convert GitHub-flavored markdown to Telegram-compatible HTML."""
result = md(text)
return result.strip()
CONTEXT_PROMPT = """First, silently read CLAUDE.md for context.
Then respond to: """
def run_claude(message: str, session_id: str = None) -> tuple[str, str]:
"""Run Claude and return (response, new_session_id). Handles expired sessions."""
cmd = [
CLAUDE_PATH, "-p", message,
"--output-format", "json",
"--allowedTools", "Read,Write,Edit,Bash,Glob,Grep,WebFetch,WebSearch,Task,Skill"
]
if session_id:
cmd.extend(["--resume", session_id])
result = subprocess.run(cmd, capture_output=True, text=True, cwd=WORKSPACE)
try:
data = json.loads(result.stdout)
response = data.get("result", "No response")
new_session_id = data.get("session_id")
return response, new_session_id
except json.JSONDecodeError:
error_text = result.stdout or result.stderr or ""
if "No conversation found" in error_text or "session" in error_text.lower():
return None, None # Signal to retry without session
return error_text or "Error running Claude", None
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update.effective_user.id
if ALLOWED_USERS and user_id not in ALLOWED_USERS:
await update.message.reply_text("Not authorized.")
return
message = update.message.text
sessions = load_sessions()
session_id = sessions.get(str(user_id))
await update.message.chat.send_action("typing")
if session_id:
response, new_session_id = run_claude(message, session_id)
if response is None:
del sessions[str(user_id)]
save_sessions(sessions)
session_id = None
if not session_id:
full_message = CONTEXT_PROMPT + message
response, new_session_id = run_claude(full_message)
if new_session_id:
sessions[str(user_id)] = new_session_id
save_sessions(sessions)
response = markdown_to_telegram_html(response)
if not response or not response.strip():
response = "(No response from Claude)"
# Telegram has 4096 char limit
if len(response) > 4000:
response = response[:4000] + "\n\n... (truncated)"
await update.message.reply_text(response, parse_mode="HTML")
async def handle_voice(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Handle voice messages by transcribing and sending to Claude."""
if not VOICE_ENABLED:
await update.message.reply_text("Voice messages not supported (requires mlx-whisper on Apple Silicon)")
return
user_id = update.effective_user.id
if ALLOWED_USERS and user_id not in ALLOWED_USERS:
await update.message.reply_text("Not authorized.")
return
await update.message.reply_text("🎤 Transcribing...")
await update.message.chat.send_action("typing")
voice = update.message.voice
file = await context.bot.get_file(voice.file_id)
with tempfile.NamedTemporaryFile(suffix=".ogg", delete=False) as tmp:
tmp_path = tmp.name
await file.download_to_drive(tmp_path)
try:
transcript = transcribe_audio(tmp_path)
if not transcript:
await update.message.reply_text("Could not transcribe voice message.")
return
await update.message.reply_text(f"📝 <i>{transcript}</i>", parse_mode="HTML")
sessions = load_sessions()
session_id = sessions.get(str(user_id))
await update.message.chat.send_action("typing")
if session_id:
response, new_session_id = run_claude(transcript, session_id)
if response is None:
del sessions[str(user_id)]
save_sessions(sessions)
session_id = None
if not session_id:
response, new_session_id = run_claude(CONTEXT_PROMPT + transcript)
if new_session_id:
sessions[str(user_id)] = new_session_id
save_sessions(sessions)
response = markdown_to_telegram_html(response)
if not response or not response.strip():
response = "(No response from Claude)"
if len(response) > 4000:
response = response[:4000] + "\n\n... (truncated)"
await update.message.reply_text(response, parse_mode="HTML")
finally:
if os.path.exists(tmp_path):
os.remove(tmp_path)
async def new_session(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update.effective_user.id
sessions = load_sessions()
if str(user_id) in sessions:
del sessions[str(user_id)]
save_sessions(sessions)
await update.message.reply_text("Session cleared. Next message starts fresh.")
async def status(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update.effective_user.id
sessions = load_sessions()
has_session = str(user_id) in sessions
await update.message.reply_text(
f"User ID: {user_id}\n"
f"Active session: {'Yes' if has_session else 'No'}\n"
f"Voice enabled: {'Yes' if VOICE_ENABLED else 'No'}"
)
def main():
if not BOT_TOKEN:
print("Set TELEGRAM_BOT_TOKEN in .env file")
return
print(f"Starting bot...")
print(f"Workspace: {WORKSPACE}")
print(f"Allowed users: {ALLOWED_USERS or 'Everyone (set ALLOWED_USERS to restrict)'}")
print(f"Voice enabled: {VOICE_ENABLED}")
app = Application.builder().token(BOT_TOKEN).build()
app.add_handler(CommandHandler("new", new_session))
app.add_handler(CommandHandler("status", status))
app.add_handler(MessageHandler(filters.VOICE, handle_voice))
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
print("Bot running. Send messages on Telegram.")
app.run_polling()
if __name__ == "__main__":
main()