maryjscout commited on
Commit
849a142
·
verified ·
1 Parent(s): 7386884

Add reminders + memory to Telegram bot

Browse files
Files changed (1) hide show
  1. telegram_bot.py +54 -0
telegram_bot.py CHANGED
@@ -21,6 +21,26 @@ for _p in (".env",):
21
  load_env(_p)
22
 
23
  _poll_status = {"last_poll": "never", "last_err": "", "offset": 0, "polls": 0}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  # HF health check: Docker Spaces must listen on 7860
25
  def _health():
26
  try:
@@ -52,6 +72,12 @@ ZEN_URL="https://opencode.ai/zen/v1/responses"
52
  MODEL=os.environ.get("OPENCODE_MODEL","muse-spark-1.2-contributor-free")
53
  KEY=(os.environ.get("OPENCODE_API_KEY") or "").strip()
54
 
 
 
 
 
 
 
55
  def _pc_status():
56
  try:
57
  import firebase_admin
@@ -74,6 +100,32 @@ def _pc_status():
74
  def brain(text):
75
  if not KEY: return "Missing OPENCODE_API_KEY, sir."
76
  low=text.lower()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  # PC status queries
78
  if "is my pc on" in low or "is pc on" in low or "pc status" in low:
79
  on, d, age = _pc_status()
@@ -103,6 +155,7 @@ def brain(text):
103
  return "".join(out).strip() or "(no reply)"
104
 
105
  def main():
 
106
  offset=0
107
  print(f"JARVIS Telegram poller live (model {MODEL}) - polling @AyomideJarvis_bot")
108
  while True:
@@ -117,6 +170,7 @@ def main():
117
  chat=msg.get("chat") or {}; text=msg.get("text") or ""
118
  cid=chat.get("id")
119
  if not text or not cid: continue
 
120
  print(f"📩 {text} from {cid}")
121
  reply=brain(text)
122
  requests.post(f"https://api.telegram.org/bot{BOT}/sendMessage", json={"chat_id": cid, "text": reply}, timeout=10)
 
21
  load_env(_p)
22
 
23
  _poll_status = {"last_poll": "never", "last_err": "", "offset": 0, "polls": 0}
24
+ _last_chat_id = None
25
+ # Reminder checker — runs every 15s, sends due reminders via Telegram
26
+ def _reminder_loop():
27
+ while True:
28
+ try:
29
+ if _rem:
30
+ due = _rem.check_due()
31
+ for task in due:
32
+ cid = _last_chat_id
33
+ if cid:
34
+ try:
35
+ requests.post(f"https://api.telegram.org/bot{BOT}/sendMessage", json={"chat_id": cid, "text": f"⏰ Reminder, sir: {task}"}, timeout=10)
36
+ except Exception as e:
37
+ print("reminder send err", e)
38
+ print(f"⏰ Reminder due: {task}")
39
+ except Exception as e:
40
+ print("reminder loop err", e)
41
+ time.sleep(15)
42
+ import threading as _th
43
+ _th.Thread(target=_reminder_loop, daemon=True).start()
44
  # HF health check: Docker Spaces must listen on 7860
45
  def _health():
46
  try:
 
72
  MODEL=os.environ.get("OPENCODE_MODEL","muse-spark-1.2-contributor-free")
73
  KEY=(os.environ.get("OPENCODE_API_KEY") or "").strip()
74
 
75
+ # Reminders support (Firebase or local fallback)
76
+ try:
77
+ import reminders as _rem
78
+ except Exception:
79
+ _rem = None
80
+
81
  def _pc_status():
82
  try:
83
  import firebase_admin
 
100
  def brain(text):
101
  if not KEY: return "Missing OPENCODE_API_KEY, sir."
102
  low=text.lower()
103
+ # Reminders (Telegram bot is the JARVIS on HF — must handle them)
104
+ if _rem and ("remind me" in low or "wake me up in" in low):
105
+ try:
106
+ confirm, _task = _rem.parse_and_add(text)
107
+ if confirm: return confirm
108
+ except Exception as e:
109
+ return f"Couldn't set reminder, sir: {e}"
110
+ if _rem and ("pending reminders" in low or "list reminders" in low or "what are my reminders" in low):
111
+ try: return _rem.list_pending()
112
+ except Exception as e: return f"Couldn't list reminders: {e}"
113
+ # Memory / save to database
114
+ if "save this to" in low and "database" in low:
115
+ try:
116
+ import memory_cloud
117
+ # Try to save last user message as memory; if phrase is generic, save it
118
+ ok = memory_cloud.remember(text)
119
+ return "Saved to your database, sir." if ok else "I couldn't save that, sir — database offline."
120
+ except Exception as e:
121
+ return f"Save failed: {e}"
122
+ if "remember that" in low:
123
+ try:
124
+ import memory_cloud
125
+ fact = text.lower().split("remember that",1)[1].strip(" .")
126
+ ok = memory_cloud.remember(fact)
127
+ return "Right away, I'll remember that, sir." if ok else "Couldn't save, sir."
128
+ except Exception: pass
129
  # PC status queries
130
  if "is my pc on" in low or "is pc on" in low or "pc status" in low:
131
  on, d, age = _pc_status()
 
155
  return "".join(out).strip() or "(no reply)"
156
 
157
  def main():
158
+ global _last_chat_id
159
  offset=0
160
  print(f"JARVIS Telegram poller live (model {MODEL}) - polling @AyomideJarvis_bot")
161
  while True:
 
170
  chat=msg.get("chat") or {}; text=msg.get("text") or ""
171
  cid=chat.get("id")
172
  if not text or not cid: continue
173
+ _last_chat_id = cid
174
  print(f"📩 {text} from {cid}")
175
  reply=brain(text)
176
  requests.post(f"https://api.telegram.org/bot{BOT}/sendMessage", json={"chat_id": cid, "text": reply}, timeout=10)