Spaces:
Sleeping
Sleeping
Deploy JARVIS Telegram bot (muse-spark)
Browse files- Dockerfile +6 -0
- README.md +17 -0
- requirements.txt +1 -0
- telegram_bot.py +62 -0
Dockerfile
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
WORKDIR /app
|
| 3 |
+
COPY requirements.txt .
|
| 4 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 5 |
+
COPY telegram_bot.py .
|
| 6 |
+
CMD ["python", "telegram_bot.py"]
|
README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: JARVIS Telegram Bot
|
| 3 |
+
emoji: 🤖
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# JARVIS Telegram Bot — @AyomideJarvis_bot
|
| 11 |
+
|
| 12 |
+
Polls Telegram and replies via Muse Spark.
|
| 13 |
+
|
| 14 |
+
## Secrets (Space → Settings → Variables and secrets)
|
| 15 |
+
- `OPENCODE_API_KEY` — your Muse Spark key
|
| 16 |
+
- `TELEGRAM_BOT_TOKEN` — `8825546647:AAH...`
|
| 17 |
+
- `OPENCODE_MODEL` — `muse-spark-1.2-contributor-free`
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
requests>=2.31
|
telegram_bot.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
telegram_bot.py — Local JARVIS Telegram poller (runs beside Jarvis_Local.bat).
|
| 3 |
+
Polls @AyomideJarvis_bot via getUpdates and replies via Muse Spark brain.
|
| 4 |
+
Use this when HF n8n -> Telegram is slow (30s timeout). Works when PC is on.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import os, time, json, requests
|
| 8 |
+
|
| 9 |
+
def load_env(p=".env"):
|
| 10 |
+
for line in open(p,encoding="utf-8"):
|
| 11 |
+
line=line.strip()
|
| 12 |
+
if line and not line.startswith("#") and "=" in line:
|
| 13 |
+
k,v=line.split("=",1); k=k.strip(); v=v.strip().strip('"').strip("'")
|
| 14 |
+
if k and v and k not in os.environ: os.environ[k]=v
|
| 15 |
+
for _p in (".env",):
|
| 16 |
+
load_env(_p)
|
| 17 |
+
|
| 18 |
+
BOT="8825546647:AAGxv77FD5xEEk-yQOFZg7_OXBPRwAGv3gs"
|
| 19 |
+
ZEN_URL="https://opencode.ai/zen/v1/responses"
|
| 20 |
+
MODEL=os.environ.get("OPENCODE_MODEL","muse-spark-1.2-contributor-free")
|
| 21 |
+
KEY=(os.environ.get("OPENCODE_API_KEY") or "").strip()
|
| 22 |
+
|
| 23 |
+
def brain(text):
|
| 24 |
+
if not KEY: return "Missing OPENCODE_API_KEY, sir."
|
| 25 |
+
low=text.lower()
|
| 26 |
+
if "what time" in low or "time is it" in low:
|
| 27 |
+
return f"It is {time.strftime('%I:%M %p on %A, %B %d, %Y').lstrip('0')}, sir."
|
| 28 |
+
if "hello" in low and len(low)<20: return "At your service, sir. What do you need?"
|
| 29 |
+
payload={"model": MODEL, "input": [{"role":"user","content": text}], "instructions": "You are J.A.R.V.I.S., witty loyal assistant. Under 2 sentences."}
|
| 30 |
+
headers={"Authorization": f"Bearer {KEY}", "Content-Type":"application/json"}
|
| 31 |
+
r=requests.post(ZEN_URL, json=payload, headers=headers, timeout=45)
|
| 32 |
+
data=r.json()
|
| 33 |
+
out=[]
|
| 34 |
+
for item in data.get("output") or []:
|
| 35 |
+
if item.get("type")=="message":
|
| 36 |
+
for c in item.get("content") or []:
|
| 37 |
+
if c.get("type")=="output_text": out.append(c.get("text",""))
|
| 38 |
+
return "".join(out).strip() or "(no reply)"
|
| 39 |
+
|
| 40 |
+
def main():
|
| 41 |
+
offset=0
|
| 42 |
+
print(f"🤖 JARVIS Telegram poller live (model {MODEL}) — polling @AyomideJarvis_bot")
|
| 43 |
+
while True:
|
| 44 |
+
try:
|
| 45 |
+
r=requests.get(f"https://api.telegram.org/bot{BOT}/getUpdates", params={"offset": offset, "timeout": 25}, timeout=30)
|
| 46 |
+
data=r.json()
|
| 47 |
+
for upd in data.get("result") or []:
|
| 48 |
+
offset=upd["update_id"]+1
|
| 49 |
+
msg=upd.get("message") or {}
|
| 50 |
+
chat=msg.get("chat") or {}; text=msg.get("text") or ""
|
| 51 |
+
cid=chat.get("id")
|
| 52 |
+
if not text or not cid: continue
|
| 53 |
+
print(f"📩 {text} from {cid}")
|
| 54 |
+
reply=brain(text)
|
| 55 |
+
requests.post(f"https://api.telegram.org/bot{BOT}/sendMessage", json={"chat_id": cid, "text": reply}, timeout=10)
|
| 56 |
+
print(f"📤 {reply[:80]}")
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print("poll err", e)
|
| 59 |
+
time.sleep(3)
|
| 60 |
+
|
| 61 |
+
if __name__=="__main__":
|
| 62 |
+
main()
|