-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram_webhook.py
More file actions
221 lines (170 loc) · 6.48 KB
/
telegram_webhook.py
File metadata and controls
221 lines (170 loc) · 6.48 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
# tap_ai/telegram_webhook.py
# Telegram Channel Adapter for Frappe AI Backend
import os
import time
import uuid
import requests
from flask import Flask, request, jsonify
from dotenv import load_dotenv
from openai import OpenAI
# -------------------------------------------------------------------
# Environment
# -------------------------------------------------------------------
load_dotenv()
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
FRAPPE_API_URL = os.getenv("FRAPPE_API_URL")
FRAPPE_API_RESULT_URL = os.getenv("FRAPPE_API_RESULT_URL")
FRAPPE_API_KEY = os.getenv("FRAPPE_API_KEY")
FRAPPE_API_SECRET = os.getenv("FRAPPE_API_SECRET")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
client = OpenAI(api_key=OPENAI_API_KEY)
app = Flask(__name__)
TELEGRAM_API_BASE = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}"
HEADERS = {
"Authorization": f"token {FRAPPE_API_KEY}:{FRAPPE_API_SECRET}",
"Content-Type": "application/json"
}
# -------------------------------------------------------------------
# Helpers
# -------------------------------------------------------------------
def telegram_get_file(file_id: str) -> str:
"""Get downloadable Telegram file URL."""
r = requests.get(f"{TELEGRAM_API_BASE}/getFile", params={"file_id": file_id})
r.raise_for_status()
file_path = r.json()["result"]["file_path"]
return f"https://api.telegram.org/file/bot{TELEGRAM_BOT_TOKEN}/{file_path}"
def whisper_transcribe(file_url: str) -> tuple[str, str]:
"""Download audio and transcribe using Whisper."""
audio = requests.get(file_url).content
# Generate unique temp filename to avoid race conditions
temp_filename = f"/tmp/input_{uuid.uuid4().hex}.ogg"
try:
with open(temp_filename, "wb") as f:
f.write(audio)
with open(temp_filename, "rb") as audio_file:
transcript = client.audio.transcriptions.create(
model="gpt-4o-transcribe",
file=audio_file
)
finally:
# Cleanup temp file
if os.path.exists(temp_filename):
os.remove(temp_filename)
text = transcript.text
language = getattr(transcript, "language", "unknown")
return text, language
def call_query_api(text: str, user_id: str) -> str:
params = {
"q": text,
"user_id": user_id
}
r = requests.post(
FRAPPE_API_URL,
params=params,
headers=HEADERS,
timeout=30
)
r.raise_for_status()
return r.json()["message"]["request_id"]
def poll_result(request_id: str, timeout_sec: int = 60) -> dict:
start = time.time()
while time.time() - start < timeout_sec:
r = requests.get(
FRAPPE_API_RESULT_URL,
params={"request_id": request_id},
headers=HEADERS,
timeout=15
)
r.raise_for_status()
data = r.json()["message"]
if data["status"] in ("success", "failed"):
return data
time.sleep(2)
return {
"status": "failed",
"answer": "Request timed out."
}
def tts_generate(text: str) -> str:
"""Generate MP3 audio from text using OpenAI TTS."""
# Generate unique temp filename
temp_filename = f"/tmp/output_{uuid.uuid4().hex}.mp3"
try:
with client.audio.speech.with_streaming_response.create(
model="gpt-4o-mini-tts",
voice="alloy",
input=text
) as response:
response.stream_to_file(temp_filename)
return temp_filename
except Exception:
# Cleanup on error
if os.path.exists(temp_filename):
os.remove(temp_filename)
raise
def send_text(chat_id: int, text: str):
payload = {"chat_id": chat_id, "text": text}
requests.post(f"{TELEGRAM_API_BASE}/sendMessage", json=payload, timeout=20)
def send_voice(chat_id: int, audio_path: str):
with open(audio_path, "rb") as audio:
files = {"voice": audio}
data = {"chat_id": chat_id}
requests.post(
f"{TELEGRAM_API_BASE}/sendVoice",
data=data,
files=files,
timeout=30
)
# -------------------------------------------------------------------
# Webhook
# -------------------------------------------------------------------
@app.route("/webhook", methods=["POST"])
def telegram_webhook():
update = request.get_json()
message = update.get("message", {})
chat_id = message.get("chat", {}).get("id")
if not chat_id:
return jsonify(success=True)
user_id = f"telegram:{chat_id}"
is_voice_input = False
try:
# ---------------------------
# TEXT MESSAGE
# ---------------------------
if "text" in message:
user_text = message["text"]
# ---------------------------
# VOICE MESSAGE
# ---------------------------
elif "voice" in message:
is_voice_input = True
file_id = message["voice"]["file_id"]
file_url = telegram_get_file(file_id)
user_text, language = whisper_transcribe(file_url)
else:
send_text(chat_id, "Unsupported message type.")
return jsonify(success=True)
# ---------------------------
# Call Frappe
# ---------------------------
request_id = call_query_api(user_text, user_id)
result = poll_result(request_id)
if result["status"] == "success":
answer_text = result["answer"]
# Voice input → voice output
if is_voice_input:
try:
audio_path = tts_generate(answer_text)
send_voice(chat_id, audio_path)
except Exception as tts_err:
print("TTS failed:", tts_err)
send_text(chat_id, answer_text)
else:
send_text(chat_id, answer_text)
else:
send_text(chat_id, "Sorry, something went wrong while processing your request.")
except Exception as e:
print("Webhook error:", e)
send_text(chat_id, "Internal error. Please try again later.")
return jsonify(success=True)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=False)