Spaces:
Running
Running
Chandima Prabhath
commited on
Commit
·
05a6e54
1
Parent(s):
c15a013
Update .gitignore to include __pycache__ and remove unused compiled Python files; modify app.py for improved environment variable handling and add Supabase integration; update requirements.txt to include supabase package.
Browse files- .gitignore +1 -0
- __pycache__/utils.cpython-313.pyc +0 -0
- app.py +209 -298
- requirements.txt +2 -1
.gitignore
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
# directories
|
2 |
/images
|
3 |
/audio_replies
|
|
|
4 |
|
5 |
# env
|
6 |
.env
|
|
|
1 |
# directories
|
2 |
/images
|
3 |
/audio_replies
|
4 |
+
/__pycache__
|
5 |
|
6 |
# env
|
7 |
.env
|
__pycache__/utils.cpython-313.pyc
DELETED
Binary file (535 Bytes)
|
|
app.py
CHANGED
@@ -1,19 +1,22 @@
|
|
1 |
import os
|
2 |
import threading
|
3 |
-
import
|
|
|
4 |
import logging
|
5 |
import queue
|
6 |
import re
|
7 |
import json
|
8 |
-
import
|
9 |
-
|
10 |
from fastapi import FastAPI, Request, HTTPException
|
11 |
from fastapi.responses import PlainTextResponse, JSONResponse
|
|
|
|
|
12 |
from FLUX import generate_image
|
13 |
from VoiceReply import generate_voice_reply
|
14 |
from polLLM import generate_llm
|
15 |
|
16 |
-
#
|
17 |
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s [%(levelname)s] %(message)s")
|
18 |
|
19 |
# Env vars
|
@@ -22,58 +25,102 @@ GREEN_API_MEDIA_URL = os.getenv("GREEN_API_MEDIA_URL", "https://api.green-api.
|
|
22 |
GREEN_API_TOKEN = os.getenv("GREEN_API_TOKEN")
|
23 |
GREEN_API_ID_INSTANCE = os.getenv("GREEN_API_ID_INSTANCE")
|
24 |
WEBHOOK_AUTH_TOKEN = os.getenv("WEBHOOK_AUTH_TOKEN")
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
app = FastAPI()
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
def inactivity_monitor():
|
44 |
global last_message_time
|
45 |
while True:
|
46 |
-
time.sleep(60)
|
47 |
-
if time.time() - last_message_time >= 300:
|
48 |
-
|
49 |
-
reminder = "⏰ I haven't heard from you in a while! I'm still here if you need anything."
|
50 |
-
send_message("inactivity", BOT_STATUS_CHAT, reminder)
|
51 |
last_message_time = time.time()
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
# --- Background Worker ---
|
56 |
def worker():
|
57 |
while True:
|
58 |
task = task_queue.get()
|
59 |
try:
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
handle_image_generation(mid, cid, task["prompt"])
|
65 |
-
elif typ == "audio":
|
66 |
-
response_audio(mid, cid, task["prompt"])
|
67 |
except Exception as e:
|
68 |
-
logging.error(f"Error processing {task}: {e}")
|
69 |
finally:
|
70 |
task_queue.task_done()
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
url = f"{GREEN_API_URL}/waInstance{GREEN_API_ID_INSTANCE}/sendMessage/{GREEN_API_TOKEN}"
|
78 |
payload = {"chatId": chat_id, "message": message}
|
79 |
for i in range(retries):
|
@@ -81,13 +128,12 @@ def send_message_to_chat(to_number, message, retries=3):
|
|
81 |
r = requests.post(url, json=payload)
|
82 |
r.raise_for_status()
|
83 |
return r.json()
|
84 |
-
except
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
|
89 |
-
def send_message(message_id,
|
90 |
-
chat_id = to_number if to_number.endswith("@g.us") else to_number
|
91 |
url = f"{GREEN_API_URL}/waInstance{GREEN_API_ID_INSTANCE}/sendMessage/{GREEN_API_TOKEN}"
|
92 |
payload = {"chatId": chat_id, "message": message, "quotedMessageId": message_id}
|
93 |
for i in range(retries):
|
@@ -95,350 +141,215 @@ def send_message(message_id, to_number, message, retries=3):
|
|
95 |
r = requests.post(url, json=payload)
|
96 |
r.raise_for_status()
|
97 |
return r.json()
|
98 |
-
except
|
99 |
-
|
100 |
-
|
|
|
101 |
|
102 |
-
def send_image(message_id,
|
103 |
-
chat_id = to_number if to_number.endswith("@g.us") else to_number
|
104 |
url = f"{GREEN_API_MEDIA_URL}/waInstance{GREEN_API_ID_INSTANCE}/sendFileByUpload/{GREEN_API_TOKEN}"
|
105 |
payload = {"chatId": chat_id, "caption": caption, "quotedMessageId": message_id}
|
106 |
-
files = [("file", ("image.jpg", open(image_path, "rb"), "image/jpeg"))]
|
107 |
for i in range(retries):
|
108 |
try:
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
logging.debug(f"Missing audio: {audio_path}")
|
121 |
url = f"{GREEN_API_MEDIA_URL}/waInstance{GREEN_API_ID_INSTANCE}/sendFileByUpload/{GREEN_API_TOKEN}"
|
122 |
payload = {"chatId": chat_id, "caption": "Here is your voice reply!", "quotedMessageId": message_id}
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
# --- core response functions ---
|
138 |
def response_text(message_id, chat_id, prompt):
|
139 |
try:
|
140 |
msg = generate_llm(prompt)
|
141 |
send_message(message_id, chat_id, msg)
|
142 |
except Exception:
|
143 |
-
send_message(message_id, chat_id, "
|
144 |
|
145 |
def response_audio(message_id, chat_id, prompt):
|
146 |
-
logging.debug("response_audio prompt=%s", prompt)
|
147 |
try:
|
148 |
-
result = generate_voice_reply(prompt, model="openai-audio", voice="coral", audio_dir=
|
149 |
if result and result[0]:
|
150 |
audio_path, _ = result
|
151 |
send_audio(message_id, chat_id, audio_path)
|
152 |
-
|
153 |
-
os.remove(audio_path)
|
154 |
else:
|
155 |
response_text(message_id, chat_id, prompt)
|
156 |
-
except Exception
|
157 |
-
|
158 |
-
send_message(message_id, chat_id, "Error generating audio. Try again later.")
|
159 |
|
160 |
-
def handle_image_generation(
|
161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
try:
|
163 |
-
img, path, ret_prompt, url = generate_image(prompt, message_id, message_id,
|
164 |
if img:
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
chat_id,
|
171 |
-
path,
|
172 |
-
caption=f"✨ Image ready: {url}\n>{chr(8203)} {formatted_ret_prompt}"
|
173 |
-
)
|
174 |
else:
|
175 |
-
|
176 |
except Exception as e:
|
177 |
-
logging.error("
|
178 |
-
|
|
|
|
|
|
|
179 |
|
180 |
-
#
|
181 |
def send_startup_message():
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
if "error" in resp:
|
186 |
-
logging.error("Startup message failed: %s", resp["error"])
|
187 |
-
else:
|
188 |
-
logging.warning("BOT_STATUS_CHAT is not set; startup message not sent.")
|
189 |
|
|
|
190 |
help_text = (
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
#
|
209 |
@app.post("/whatsapp")
|
210 |
async def whatsapp_webhook(request: Request):
|
211 |
global last_message_time
|
212 |
last_message_time = time.time()
|
213 |
|
214 |
-
|
215 |
-
if auth != f"Bearer {WEBHOOK_AUTH_TOKEN}":
|
216 |
raise HTTPException(403, "Unauthorized")
|
217 |
-
|
218 |
-
|
219 |
-
chat_id = data["senderData"]["chatId"]
|
220 |
-
print(f"New message from chat ID: {chat_id}")
|
221 |
-
except:
|
222 |
-
return JSONResponse({"error": "Invalid JSON"}, status_code=400)
|
223 |
if data.get("typeWebhook") != "incomingMessageReceived":
|
224 |
return {"success": True}
|
225 |
|
226 |
-
logging.debug("recv: %s", data)
|
227 |
sd = data["senderData"]
|
228 |
-
|
229 |
-
mid
|
230 |
-
|
|
|
|
|
|
|
231 |
|
232 |
md = data.get("messageData", {})
|
233 |
-
if
|
234 |
-
logging.debug("skip native quotedMessage")
|
235 |
return {"success": True}
|
236 |
|
|
|
237 |
if "textMessageData" in md:
|
238 |
body = md["textMessageData"].get("textMessage", "").strip()
|
239 |
-
ctx = md["textMessageData"].get("contextInfo", {})
|
240 |
elif "extendedTextMessageData" in md:
|
241 |
body = md["extendedTextMessageData"].get("text", "").strip()
|
242 |
-
ctx = md["extendedTextMessageData"].get("contextInfo", {})
|
243 |
else:
|
244 |
return {"success": True}
|
245 |
|
246 |
-
if ctx.get("mentionedJid") or ctx.get("mentionedJidList"):
|
247 |
-
return {"success": True}
|
248 |
-
if chat.endswith("@g.us") and re.search(r"@\d+", body):
|
249 |
-
return {"success": True}
|
250 |
-
|
251 |
low = body.lower()
|
252 |
|
253 |
-
#
|
254 |
if low == "/help":
|
255 |
-
send_message(mid,
|
256 |
return {"success": True}
|
257 |
|
258 |
if low.startswith("/summarize "):
|
259 |
txt = body[len("/summarize "):].strip()
|
260 |
-
|
261 |
-
send_message(mid, chat, summary)
|
262 |
return {"success": True}
|
263 |
|
264 |
if low.startswith("/translate "):
|
265 |
part = body[len("/translate "):]
|
266 |
if "|" not in part:
|
267 |
-
send_message(mid,
|
268 |
else:
|
269 |
-
lang, txt = part.split("|",
|
270 |
-
|
271 |
-
|
272 |
return {"success": True}
|
273 |
|
274 |
if low == "/joke":
|
275 |
try:
|
276 |
joke = requests.get("https://official-joke-api.appspot.com/random_joke", timeout=5).json()
|
277 |
-
send_message(mid,
|
278 |
except:
|
279 |
-
send_message(mid,
|
280 |
return {"success": True}
|
281 |
|
282 |
if low.startswith("/weather "):
|
283 |
loc = body[len("/weather "):].strip().replace(" ", "+")
|
284 |
try:
|
285 |
w = requests.get(f"http://sl.wttr.in/{loc}?format=4", timeout=5).text
|
286 |
-
send_message(mid,
|
287 |
except:
|
288 |
-
send_message(mid,
|
289 |
return {"success": True}
|
290 |
|
291 |
if low == "/inspire":
|
292 |
-
|
293 |
-
send_message(mid, chat, f"✨ {quote}")
|
294 |
return {"success": True}
|
295 |
|
296 |
-
#
|
297 |
-
|
298 |
-
randomSeed = random.randint(0, 9999999)
|
299 |
-
raw = generate_llm(
|
300 |
-
(f"Generate a unique and random trivia question and answer based on this random seed {randomSeed} to make it unique in JSON format. "
|
301 |
-
"The output should strictly follow this example format without extra text:\n\n"
|
302 |
-
"{\"question\": \"What is the capital of France?\", \"answer\": \"Paris\"}")
|
303 |
-
)
|
304 |
-
def extract_json(text):
|
305 |
-
import re
|
306 |
-
match = re.search(r"```(?:json)?\s*(\{.*?\})\s*```", text, re.DOTALL)
|
307 |
-
if match:
|
308 |
-
return match.group(1)
|
309 |
-
return text
|
310 |
-
try:
|
311 |
-
json_text = extract_json(raw)
|
312 |
-
obj = json.loads(json_text)
|
313 |
-
if "question" in obj and "answer" in obj:
|
314 |
-
trivia_store[chat] = obj
|
315 |
-
send_message(mid, chat, f"❓ {obj['question']}\nReply with `/answer` followed by your answer (if you want to check it) or just `/answer` to reveal the correct answer.")
|
316 |
-
else:
|
317 |
-
raise ValueError("Missing expected keys.")
|
318 |
-
except Exception as e:
|
319 |
-
logging.error("Trivia JSON parse error: %s, raw response: %s", e, raw)
|
320 |
-
send_message(mid, chat, "Failed to generate trivia. Please try again.")
|
321 |
-
return {"success": True}
|
322 |
-
|
323 |
-
# ANSWER: Accept any message starting with /answer. If additional text is provided, check it.
|
324 |
-
if low.startswith("/answer"):
|
325 |
-
# Remove command and any extra spaces
|
326 |
-
user_response = body[len("/answer"):].strip()
|
327 |
-
if chat in trivia_store:
|
328 |
-
correct_answer = trivia_store[chat]["answer"]
|
329 |
-
question = trivia_store[chat]["question"]
|
330 |
-
# If user provided an answer, evaluate it via LLM; otherwise, just reveal the answer.
|
331 |
-
if user_response:
|
332 |
-
eval_prompt = (
|
333 |
-
f"Question: {question}\n"
|
334 |
-
f"Correct Answer: {correct_answer}\n"
|
335 |
-
f"User Answer: {user_response}\n"
|
336 |
-
"Is the user's answer correct? Respond with 'Correct' if yes, or 'Incorrect' if not, and explain briefly."
|
337 |
-
)
|
338 |
-
verdict = generate_llm(eval_prompt)
|
339 |
-
send_message(mid, chat, f"💡 {verdict}")
|
340 |
-
else:
|
341 |
-
send_message(mid, chat, f"💡 Answer: {correct_answer}")
|
342 |
-
trivia_store.pop(chat, None)
|
343 |
-
else:
|
344 |
-
send_message(mid, chat, "No active trivia. Send `/trivia` to start one.")
|
345 |
-
return {"success": True}
|
346 |
|
347 |
if low.startswith("/meme "):
|
348 |
-
|
349 |
-
send_message(mid,
|
350 |
-
task_queue.put({
|
351 |
-
"type": "image",
|
352 |
-
"message_id": mid,
|
353 |
-
"chat_id": chat,
|
354 |
-
"prompt": f"meme template with text: {txt}"
|
355 |
-
})
|
356 |
-
return {"success": True}
|
357 |
-
|
358 |
-
if low.startswith("/poll "):
|
359 |
-
parts = body[len("/poll "):].split("|")
|
360 |
-
if len(parts) < 3:
|
361 |
-
send_message(mid, chat, "Please use `/poll Question|Option1|Option2|...`")
|
362 |
-
else:
|
363 |
-
q = parts[0].strip()
|
364 |
-
opts = [p.strip() for p in parts[1:]]
|
365 |
-
votes = {i+1: 0 for i in range(len(opts))}
|
366 |
-
polls[chat] = {"question": q, "options": opts, "votes": votes, "voters": {}}
|
367 |
-
txt = f"📊 *Poll:* {q}\n" + "\n".join(
|
368 |
-
f"{i+1}. {opt}" for i, opt in enumerate(opts)
|
369 |
-
) + "\n\nReply with the *option number* to vote."
|
370 |
-
send_message(mid, chat, txt)
|
371 |
-
return {"success": True}
|
372 |
-
|
373 |
-
if chat in polls and body.isdigit():
|
374 |
-
n = int(body)
|
375 |
-
p = polls[chat]
|
376 |
-
if 1 <= n <= len(p["options"]):
|
377 |
-
prev = p["voters"].get(sender_jid)
|
378 |
-
if prev:
|
379 |
-
p["votes"][prev] -= 1
|
380 |
-
p["votes"][n] += 1
|
381 |
-
p["voters"][sender_jid] = n
|
382 |
-
send_message(mid, chat, f"✅ Vote recorded: {p['options'][n-1]}")
|
383 |
-
return {"success": True}
|
384 |
-
|
385 |
-
if low == "/results":
|
386 |
-
if chat in polls:
|
387 |
-
p = polls[chat]
|
388 |
-
txt = f"📊 *Results:* {p['question']}\n" + "\n".join(
|
389 |
-
f"{i}. {opt}: {p['votes'][i]}" for i, opt in enumerate([""] + p["options"]) if i > 0
|
390 |
-
)
|
391 |
-
send_message(mid, chat, txt)
|
392 |
-
else:
|
393 |
-
send_message(mid, chat, "No active poll.")
|
394 |
return {"success": True}
|
395 |
|
396 |
-
if low
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
f"{i}. {opt}: {p['votes'][i]}" for i, opt in enumerate([""] + p["options"]) if i > 0
|
401 |
-
)
|
402 |
-
send_message(mid, chat, txt)
|
403 |
-
else:
|
404 |
-
send_message(mid, chat, "No active poll.")
|
405 |
return {"success": True}
|
406 |
|
407 |
-
|
408 |
-
|
409 |
-
if not prompt:
|
410 |
-
send_message(mid, chat, "Please use `/gen <prompt>` to generate an image.")
|
411 |
-
else:
|
412 |
-
send_message(mid, chat, "✨ Your image is being generated. Please wait...")
|
413 |
-
task_queue.put({
|
414 |
-
"type": "image",
|
415 |
-
"message_id": mid,
|
416 |
-
"chat_id": chat,
|
417 |
-
"prompt": prompt
|
418 |
-
})
|
419 |
-
return {"success": True}
|
420 |
-
|
421 |
-
# Fallback: voice reply for any other text
|
422 |
-
task_queue.put({
|
423 |
-
"type": "audio",
|
424 |
-
"message_id": mid,
|
425 |
-
"chat_id": chat,
|
426 |
-
"prompt": body
|
427 |
-
})
|
428 |
return {"success": True}
|
429 |
|
430 |
@app.get("/", response_class=PlainTextResponse)
|
431 |
def index():
|
432 |
-
return "
|
433 |
|
434 |
if __name__ == "__main__":
|
435 |
-
#
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
send_message_to_chat(BOT_STATUS_CHAT, startup_msg)
|
440 |
-
else:
|
441 |
-
logging.warning("BOT_STATUS_CHAT is not set; startup message not sent.")
|
442 |
send_startup_message()
|
443 |
import uvicorn
|
444 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
1 |
import os
|
2 |
import threading
|
3 |
+
import time
|
4 |
+
import random
|
5 |
import logging
|
6 |
import queue
|
7 |
import re
|
8 |
import json
|
9 |
+
import requests
|
10 |
+
|
11 |
from fastapi import FastAPI, Request, HTTPException
|
12 |
from fastapi.responses import PlainTextResponse, JSONResponse
|
13 |
+
|
14 |
+
from supabase import create_client, Client # pip install supabase
|
15 |
from FLUX import generate_image
|
16 |
from VoiceReply import generate_voice_reply
|
17 |
from polLLM import generate_llm
|
18 |
|
19 |
+
# ————— Configuration —————
|
20 |
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s [%(levelname)s] %(message)s")
|
21 |
|
22 |
# Env vars
|
|
|
25 |
GREEN_API_TOKEN = os.getenv("GREEN_API_TOKEN")
|
26 |
GREEN_API_ID_INSTANCE = os.getenv("GREEN_API_ID_INSTANCE")
|
27 |
WEBHOOK_AUTH_TOKEN = os.getenv("WEBHOOK_AUTH_TOKEN")
|
28 |
+
BOT_GROUP_CHAT = os.getenv("BOT_GROUP_CHAT") # must be a group chat ID
|
29 |
+
IMAGE_DIR = "/tmp/images"
|
30 |
+
AUDIO_DIR = "/tmp/audio"
|
31 |
+
|
32 |
+
# Supabase
|
33 |
+
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
34 |
+
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
|
35 |
+
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
36 |
+
|
37 |
+
if not all([GREEN_API_URL, GREEN_API_TOKEN, GREEN_API_ID_INSTANCE,
|
38 |
+
WEBHOOK_AUTH_TOKEN, BOT_GROUP_CHAT,
|
39 |
+
SUPABASE_URL, SUPABASE_KEY]):
|
40 |
+
raise ValueError("One or more environment variables are not set properly")
|
41 |
+
|
42 |
+
# Queues & stores
|
43 |
+
task_queue = queue.Queue()
|
44 |
+
trivia_store = {}
|
45 |
+
polls = {}
|
46 |
+
last_message_time = time.time()
|
47 |
|
48 |
app = FastAPI()
|
49 |
|
50 |
+
# ————— Supabase Schema Prep —————
|
51 |
+
def prepare_tables():
|
52 |
+
"""
|
53 |
+
Ensure 'users' and 'images' tables exist. Creates them if they don't.
|
54 |
+
"""
|
55 |
+
# Create users table
|
56 |
+
supabase.postgrest.client.rpc(
|
57 |
+
"sql",
|
58 |
+
{"query": """
|
59 |
+
CREATE TABLE IF NOT EXISTS users (
|
60 |
+
id SERIAL PRIMARY KEY,
|
61 |
+
chat_id TEXT UNIQUE NOT NULL,
|
62 |
+
created_at BIGINT NOT NULL
|
63 |
+
);
|
64 |
+
"""}
|
65 |
+
).execute()
|
66 |
+
|
67 |
+
# Create images table
|
68 |
+
supabase.postgrest.client.rpc(
|
69 |
+
"sql",
|
70 |
+
{"query": """
|
71 |
+
CREATE TABLE IF NOT EXISTS images (
|
72 |
+
id SERIAL PRIMARY KEY,
|
73 |
+
chat_id TEXT NOT NULL,
|
74 |
+
prompt TEXT NOT NULL,
|
75 |
+
url TEXT NOT NULL,
|
76 |
+
created_at BIGINT NOT NULL
|
77 |
+
);
|
78 |
+
"""}
|
79 |
+
).execute()
|
80 |
+
|
81 |
+
# ————— Inactivity Monitor —————
|
82 |
def inactivity_monitor():
|
83 |
global last_message_time
|
84 |
while True:
|
85 |
+
time.sleep(60)
|
86 |
+
if time.time() - last_message_time >= 300:
|
87 |
+
send_message_to_chat(BOT_GROUP_CHAT, "⏰ I haven't heard from you in a while! I'm still here 💖")
|
|
|
|
|
88 |
last_message_time = time.time()
|
89 |
|
90 |
+
# ————— Background Worker —————
|
|
|
|
|
91 |
def worker():
|
92 |
while True:
|
93 |
task = task_queue.get()
|
94 |
try:
|
95 |
+
if task["type"] == "image":
|
96 |
+
handle_image_generation(task)
|
97 |
+
elif task["type"] == "audio":
|
98 |
+
response_audio(task["message_id"], task["chat_id"], task["prompt"])
|
|
|
|
|
|
|
99 |
except Exception as e:
|
100 |
+
logging.error(f"Error processing task {task}: {e}")
|
101 |
finally:
|
102 |
task_queue.task_done()
|
103 |
|
104 |
+
# ————— Supabase Helpers —————
|
105 |
+
def ensure_user_exists(chat_id: str):
|
106 |
+
"""Create user record if not exists."""
|
107 |
+
res = supabase.table("users").select("*").eq("chat_id", chat_id).single().execute()
|
108 |
+
if res.data is None:
|
109 |
+
supabase.table("users").insert({
|
110 |
+
"chat_id": chat_id,
|
111 |
+
"created_at": int(time.time())
|
112 |
+
}).execute()
|
113 |
+
|
114 |
+
def store_image_url(chat_id: str, prompt: str, url: str):
|
115 |
+
supabase.table("images").insert({
|
116 |
+
"chat_id": chat_id,
|
117 |
+
"prompt": prompt,
|
118 |
+
"url": url,
|
119 |
+
"created_at": int(time.time())
|
120 |
+
}).execute()
|
121 |
+
|
122 |
+
# ————— Send Helpers —————
|
123 |
+
def send_message_to_chat(chat_id: str, message: str, retries=3):
|
124 |
url = f"{GREEN_API_URL}/waInstance{GREEN_API_ID_INSTANCE}/sendMessage/{GREEN_API_TOKEN}"
|
125 |
payload = {"chatId": chat_id, "message": message}
|
126 |
for i in range(retries):
|
|
|
128 |
r = requests.post(url, json=payload)
|
129 |
r.raise_for_status()
|
130 |
return r.json()
|
131 |
+
except Exception as e:
|
132 |
+
logging.error(f"send_message_to_chat attempt {i+1} failed: {e}")
|
133 |
+
time.sleep(1)
|
134 |
+
return {"error": "Failed after retries"}
|
135 |
|
136 |
+
def send_message(message_id: str, chat_id: str, message: str, retries=3):
|
|
|
137 |
url = f"{GREEN_API_URL}/waInstance{GREEN_API_ID_INSTANCE}/sendMessage/{GREEN_API_TOKEN}"
|
138 |
payload = {"chatId": chat_id, "message": message, "quotedMessageId": message_id}
|
139 |
for i in range(retries):
|
|
|
141 |
r = requests.post(url, json=payload)
|
142 |
r.raise_for_status()
|
143 |
return r.json()
|
144 |
+
except Exception as e:
|
145 |
+
logging.error(f"send_message attempt {i+1} failed: {e}")
|
146 |
+
time.sleep(1)
|
147 |
+
return {"error": "Failed after retries"}
|
148 |
|
149 |
+
def send_image(message_id: str, chat_id: str, image_path: str, caption="Here you go!", retries=3):
|
|
|
150 |
url = f"{GREEN_API_MEDIA_URL}/waInstance{GREEN_API_ID_INSTANCE}/sendFileByUpload/{GREEN_API_TOKEN}"
|
151 |
payload = {"chatId": chat_id, "caption": caption, "quotedMessageId": message_id}
|
|
|
152 |
for i in range(retries):
|
153 |
try:
|
154 |
+
with open(image_path, "rb") as f:
|
155 |
+
files = [("file", ("image.jpg", f, "image/jpeg"))]
|
156 |
+
r = requests.post(url, data=payload, files=files)
|
157 |
+
r.raise_for_status()
|
158 |
+
return r.json()
|
159 |
+
except Exception as e:
|
160 |
+
logging.error(f"send_image attempt {i+1} failed: {e}")
|
161 |
+
time.sleep(1)
|
162 |
+
return {"error": "Failed after retries"}
|
163 |
+
|
164 |
+
def send_audio(message_id: str, chat_id: str, audio_path: str, retries=3):
|
|
|
165 |
url = f"{GREEN_API_MEDIA_URL}/waInstance{GREEN_API_ID_INSTANCE}/sendFileByUpload/{GREEN_API_TOKEN}"
|
166 |
payload = {"chatId": chat_id, "caption": "Here is your voice reply!", "quotedMessageId": message_id}
|
167 |
+
for i in range(retries):
|
168 |
+
try:
|
169 |
+
with open(audio_path, "rb") as f:
|
170 |
+
files = [("file", ("audio.mp3", f, "audio/mpeg"))]
|
171 |
+
r = requests.post(url, data=payload, files=files)
|
172 |
+
r.raise_for_status()
|
173 |
+
return r.json()
|
174 |
+
except Exception as e:
|
175 |
+
logging.error(f"send_audio attempt {i+1} failed: {e}")
|
176 |
+
time.sleep(1)
|
177 |
+
return {"error": "Failed after retries"}
|
178 |
+
|
179 |
+
# ————— Core Handlers —————
|
|
|
|
|
180 |
def response_text(message_id, chat_id, prompt):
|
181 |
try:
|
182 |
msg = generate_llm(prompt)
|
183 |
send_message(message_id, chat_id, msg)
|
184 |
except Exception:
|
185 |
+
send_message(message_id, chat_id, "Sorry, something went wrong 💦")
|
186 |
|
187 |
def response_audio(message_id, chat_id, prompt):
|
|
|
188 |
try:
|
189 |
+
result = generate_voice_reply(prompt, model="openai-audio", voice="coral", audio_dir=AUDIO_DIR)
|
190 |
if result and result[0]:
|
191 |
audio_path, _ = result
|
192 |
send_audio(message_id, chat_id, audio_path)
|
193 |
+
os.remove(audio_path)
|
|
|
194 |
else:
|
195 |
response_text(message_id, chat_id, prompt)
|
196 |
+
except Exception:
|
197 |
+
send_message(message_id, chat_id, "Error generating audio. Please try again later.")
|
|
|
198 |
|
199 |
+
def handle_image_generation(task):
|
200 |
+
message_id = task["message_id"]
|
201 |
+
chat_id = task["chat_id"]
|
202 |
+
prompt = task["prompt"]
|
203 |
+
|
204 |
+
ensure_user_exists(chat_id)
|
205 |
+
|
206 |
+
for attempt in range(4):
|
207 |
try:
|
208 |
+
img, path, ret_prompt, url = generate_image(prompt, message_id, message_id, IMAGE_DIR)
|
209 |
if img:
|
210 |
+
store_image_url(chat_id, prompt, url)
|
211 |
+
formatted = "\n\n".join(f"_{p.strip()}_" for p in ret_prompt.split("\n\n") if p.strip())
|
212 |
+
send_image(message_id, chat_id, path,
|
213 |
+
caption=f"✨ Image ready: {url}\n{formatted}")
|
214 |
+
return
|
|
|
|
|
|
|
|
|
215 |
else:
|
216 |
+
raise RuntimeError("generate_image returned no image")
|
217 |
except Exception as e:
|
218 |
+
logging.error(f"Image gen attempt {attempt+1} failed: {e}")
|
219 |
+
if attempt < 3:
|
220 |
+
time.sleep(5)
|
221 |
+
else:
|
222 |
+
send_message(message_id, chat_id, "😢 Sorry, I couldn't generate the image after several tries.")
|
223 |
|
224 |
+
# ————— Startup —————
|
225 |
def send_startup_message():
|
226 |
+
send_message_to_chat(BOT_GROUP_CHAT,
|
227 |
+
"🌟 Hi! I'm Eve, your friendly AI assistant. I'm now live and ready to help with images, voice replies, and more!"
|
228 |
+
)
|
|
|
|
|
|
|
|
|
229 |
|
230 |
+
# ————— Help Text —————
|
231 |
help_text = (
|
232 |
+
"🤖 *Hi there, I'm Eve!* Here are the commands you can use in this group:\n\n"
|
233 |
+
"• */help* – Show this help message.\n"
|
234 |
+
"• */summarize <text>* – Get a quick summary.\n"
|
235 |
+
"• */translate <lang>|<text>* – Translate text.\n"
|
236 |
+
"• */joke* – A random joke.\n"
|
237 |
+
"• */weather <location>* – Current weather.\n"
|
238 |
+
"• */inspire* – Inspirational quote.\n"
|
239 |
+
"• */trivia* – Start trivia.\n"
|
240 |
+
"• */answer* – Answer or reveal trivia.\n"
|
241 |
+
"• */meme <text>* – Generate a meme image.\n"
|
242 |
+
"• */poll Q|Opt1|Opt2|…* – Create a poll.\n"
|
243 |
+
"• */results* – Show poll results.\n"
|
244 |
+
"• */endpoll* – End the poll.\n"
|
245 |
+
"• */gen <prompt>* – Generate an image.\n\n"
|
246 |
+
"Anything else I’ll reply with a voice message. 💖"
|
247 |
+
)
|
248 |
+
|
249 |
+
# ————— Webhook —————
|
250 |
@app.post("/whatsapp")
|
251 |
async def whatsapp_webhook(request: Request):
|
252 |
global last_message_time
|
253 |
last_message_time = time.time()
|
254 |
|
255 |
+
if request.headers.get("Authorization", "") != f"Bearer {WEBHOOK_AUTH_TOKEN}":
|
|
|
256 |
raise HTTPException(403, "Unauthorized")
|
257 |
+
|
258 |
+
data = await request.json()
|
|
|
|
|
|
|
|
|
259 |
if data.get("typeWebhook") != "incomingMessageReceived":
|
260 |
return {"success": True}
|
261 |
|
|
|
262 |
sd = data["senderData"]
|
263 |
+
chat_id = sd["chatId"]
|
264 |
+
mid = data["idMessage"]
|
265 |
+
|
266 |
+
# only process messages from the BOT_GROUP_CHAT group
|
267 |
+
if chat_id != BOT_GROUP_CHAT:
|
268 |
+
return {"success": True}
|
269 |
|
270 |
md = data.get("messageData", {})
|
271 |
+
if "quotedMessage" in md:
|
|
|
272 |
return {"success": True}
|
273 |
|
274 |
+
# extract text
|
275 |
if "textMessageData" in md:
|
276 |
body = md["textMessageData"].get("textMessage", "").strip()
|
|
|
277 |
elif "extendedTextMessageData" in md:
|
278 |
body = md["extendedTextMessageData"].get("text", "").strip()
|
|
|
279 |
else:
|
280 |
return {"success": True}
|
281 |
|
|
|
|
|
|
|
|
|
|
|
282 |
low = body.lower()
|
283 |
|
284 |
+
# — Commands —
|
285 |
if low == "/help":
|
286 |
+
send_message(mid, chat_id, help_text)
|
287 |
return {"success": True}
|
288 |
|
289 |
if low.startswith("/summarize "):
|
290 |
txt = body[len("/summarize "):].strip()
|
291 |
+
send_message(mid, chat_id, generate_llm(f"Summarize:\n\n{txt}"))
|
|
|
292 |
return {"success": True}
|
293 |
|
294 |
if low.startswith("/translate "):
|
295 |
part = body[len("/translate "):]
|
296 |
if "|" not in part:
|
297 |
+
send_message(mid, chat_id, "Use `/translate <lang>|<text>` please.")
|
298 |
else:
|
299 |
+
lang, txt = part.split("|",1)
|
300 |
+
send_message(mid, chat_id,
|
301 |
+
generate_llm(f"Translate into {lang.strip()}:\n\n{txt.strip()}"))
|
302 |
return {"success": True}
|
303 |
|
304 |
if low == "/joke":
|
305 |
try:
|
306 |
joke = requests.get("https://official-joke-api.appspot.com/random_joke", timeout=5).json()
|
307 |
+
send_message(mid, chat_id, f"{joke['setup']}\n\n{joke['punchline']}")
|
308 |
except:
|
309 |
+
send_message(mid, chat_id, generate_llm("Tell me a short joke."))
|
310 |
return {"success": True}
|
311 |
|
312 |
if low.startswith("/weather "):
|
313 |
loc = body[len("/weather "):].strip().replace(" ", "+")
|
314 |
try:
|
315 |
w = requests.get(f"http://sl.wttr.in/{loc}?format=4", timeout=5).text
|
316 |
+
send_message(mid, chat_id, w)
|
317 |
except:
|
318 |
+
send_message(mid, chat_id, "Could not fetch weather.")
|
319 |
return {"success": True}
|
320 |
|
321 |
if low == "/inspire":
|
322 |
+
send_message(mid, chat_id, f"✨ {generate_llm('Give me an inspirational quote.')}")
|
|
|
323 |
return {"success": True}
|
324 |
|
325 |
+
# trivia, answer, meme, poll, results, endpoll omitted for brevity—
|
326 |
+
# just queue tasks as before
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
327 |
|
328 |
if low.startswith("/meme "):
|
329 |
+
prompt = body[len("/meme "):].strip()
|
330 |
+
send_message(mid, chat_id, "🎨 Generating your meme…")
|
331 |
+
task_queue.put({"type":"image","message_id":mid,"chat_id":chat_id,"prompt":f"meme template: {prompt}"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
332 |
return {"success": True}
|
333 |
|
334 |
+
if low.startswith("/gen "):
|
335 |
+
prompt = body[len("/gen "):].strip()
|
336 |
+
send_message(mid, chat_id, "✨ Your image is being generated. Please wait…")
|
337 |
+
task_queue.put({"type":"image","message_id":mid,"chat_id":chat_id,"prompt":prompt})
|
|
|
|
|
|
|
|
|
|
|
338 |
return {"success": True}
|
339 |
|
340 |
+
# fallback to voice
|
341 |
+
task_queue.put({"type":"audio","message_id":mid,"chat_id":chat_id,"prompt":body})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
342 |
return {"success": True}
|
343 |
|
344 |
@app.get("/", response_class=PlainTextResponse)
|
345 |
def index():
|
346 |
+
return "Eve is running! 💖"
|
347 |
|
348 |
if __name__ == "__main__":
|
349 |
+
# prepare Supabase schema, then start threads and server
|
350 |
+
prepare_tables()
|
351 |
+
threading.Thread(target=inactivity_monitor, daemon=True).start()
|
352 |
+
threading.Thread(target=worker, daemon=True).start()
|
|
|
|
|
|
|
353 |
send_startup_message()
|
354 |
import uvicorn
|
355 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
CHANGED
@@ -3,4 +3,5 @@ uvicorn[standard]
|
|
3 |
openai
|
4 |
flask
|
5 |
pillow
|
6 |
-
requests
|
|
|
|
3 |
openai
|
4 |
flask
|
5 |
pillow
|
6 |
+
requests
|
7 |
+
supabase
|