-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchronos_bot.py
More file actions
723 lines (587 loc) · 27.7 KB
/
chronos_bot.py
File metadata and controls
723 lines (587 loc) · 27.7 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
#!/usr/bin/env python3
"""
Chronos Telegram Bot
────────────────────
Personal schedule assistant powered by your Chronos save code.
Sends reminders before events, lets you query today/week/next event.
Setup:
1. pip install "python-telegram-bot[job-queue]" apscheduler httpx
2. Talk to @BotFather on Telegram → /newbot → copy the token
3. Set environment variables: BOT_TOKEN, TIMEZONE, ADMIN_CHAT_ID
4. python chronos_bot.py
"""
import asyncio
import base64
import json
import logging
import os
import threading
from datetime import datetime, timedelta
from http.server import BaseHTTPRequestHandler, HTTPServer
from zoneinfo import ZoneInfo # Python 3.9+
import httpx
from telegram import Update
from telegram.ext import (
Application, CommandHandler, MessageHandler,
filters, ContextTypes,
)
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.date import DateTrigger
# ── CONFIG — all via environment variables ─────────────────────────────────
BOT_TOKEN = os.environ.get("BOT_TOKEN", "")
TIMEZONE = os.environ.get("TIMEZONE", "Asia/Makassar") # default = Bali WITA
ADMIN_CHAT_ID = int(os.environ.get("ADMIN_CHAT_ID", "0"))
RENDER_EXTERNAL_URL = os.environ.get("RENDER_EXTERNAL_URL", "")
# ──────────────────────────────────────────────────────────────────────────
DEFAULT_REMIND_MINS = 15
logging.basicConfig(
format="%(asctime)s [%(levelname)s] %(message)s",
level=logging.INFO,
)
log = logging.getLogger(__name__)
# user_state[chat_id] = { schedule, remind_minutes, job_ids[] }
user_state: dict = {}
# Buffer for multi-part save codes (Telegram splits long messages)
code_buffer: dict = {} # chat_id -> {"parts": [], "job": None}
stats = {"total_users": set(), "schedules_loaded": 0}
scheduler = AsyncIOScheduler(timezone=TIMEZONE)
# ═══════════════════════════════════════════════════════════════════════════
# HEALTH SERVER + SELF-PING (keeps Render free tier alive)
# ═══════════════════════════════════════════════════════════════════════════
class _Health(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(b"ok")
def log_message(self, *args):
pass # silence access logs
def start_health_server():
port = int(os.environ.get("PORT", 8080))
HTTPServer(("0.0.0.0", port), _Health).serve_forever()
async def self_ping():
if not RENDER_EXTERNAL_URL:
log.info("RENDER_EXTERNAL_URL not set — self-ping disabled")
return
try:
async with httpx.AsyncClient() as client:
while True:
try:
await client.get(RENDER_EXTERNAL_URL, timeout=10)
log.info("Self-ping OK")
except Exception as ex:
log.warning("Self-ping failed: %s", ex)
await asyncio.sleep(600)
except asyncio.CancelledError:
pass # clean shutdown, nothing to worry about
# ═══════════════════════════════════════════════════════════════════════════
# CHRONOS PARSING
# ═══════════════════════════════════════════════════════════════════════════
def parse_code(code: str) -> dict:
"""Decode a Chronos base64 save code → dict."""
padded = code.strip() + "=" * (-len(code.strip()) % 4)
raw = base64.b64decode(padded).decode("utf-8")
return json.loads(raw)
def week_start_dt(ws_iso: str, tz: ZoneInfo) -> datetime:
dt = datetime.fromisoformat(ws_iso.replace("Z", "+00:00"))
return dt.astimezone(tz).replace(hour=0, minute=0, second=0, microsecond=0)
def ev_start_min(ev: dict, day_idx: int) -> int:
rp = ev.get("recPos") or {}
return rp.get(str(day_idx), rp.get(day_idx, ev.get("startMin", 0)))
def occurrences(ev: dict, ws_dt: datetime, days_ahead: int = 30) -> list[tuple]:
tz = ws_dt.tzinfo
now = datetime.now(tz)
today_midnight = now.replace(hour=0, minute=0, second=0, microsecond=0)
cutoff = today_midnight + timedelta(days=days_ahead)
dur = ev.get("dur", 60)
rec = ev.get("recurring", "")
days = ev.get("recDays") or [ev.get("day", 0)]
bw_grp = ev.get("bwGroup", 1)
out = []
if not rec:
d = ev.get("day", 0)
sm = ev_start_min(ev, d)
edt = ws_dt + timedelta(days=d, minutes=sm)
if today_midnight <= edt < cutoff:
out.append((edt, edt + timedelta(minutes=dur), ev))
elif rec == "weekly":
wk = ws_dt
while wk < cutoff:
for d in days:
sm = ev_start_min(ev, d)
edt = wk + timedelta(days=d, minutes=sm)
if today_midnight <= edt < cutoff:
out.append((edt, edt + timedelta(minutes=dur), ev))
wk += timedelta(weeks=1)
elif rec == "biweekly":
wk, wk_num = ws_dt, 0
while wk < cutoff:
grp = 1 if wk_num % 2 == 0 else 2
if grp == bw_grp:
for d in days:
sm = ev_start_min(ev, d)
edt = wk + timedelta(days=d, minutes=sm)
if today_midnight <= edt < cutoff:
out.append((edt, edt + timedelta(minutes=dur), ev))
wk += timedelta(weeks=1)
wk_num += 1
return sorted(out, key=lambda x: x[0])
def all_upcoming(schedule: dict, days_ahead: int = 30) -> list[tuple]:
tz = ZoneInfo(TIMEZONE)
ws = week_start_dt(schedule.get("ws", datetime.now(tz).isoformat()), tz)
result = []
for ev in schedule.get("events", []):
if ev.get("done"):
continue
result.extend(occurrences(ev, ws, days_ahead))
return sorted(result, key=lambda x: x[0])
# ═══════════════════════════════════════════════════════════════════════════
# FORMATTING HELPERS
# ═══════════════════════════════════════════════════════════════════════════
def fmt_dur(mins: int) -> str:
h, m = divmod(mins, 60)
if h and m: return f"{h}h {m}m"
if h: return f"{h}h"
return f"{m}m"
def fmt_ev(ev: dict, s: datetime, e: datetime) -> str:
rec_icon = " 🔄" if ev.get("recurring") else ""
lock_icon = " 🔒" if ev.get("locked") else ""
return (
f"• {s.strftime('%H:%M')}–{e.strftime('%H:%M')} "
f"*{ev.get('name','?')}*{rec_icon}{lock_icon} "
f"_{ev.get('cat','')} · {fmt_dur(ev.get('dur',0))}_"
)
# ═══════════════════════════════════════════════════════════════════════════
# REMINDER JOBS
# ═══════════════════════════════════════════════════════════════════════════
def clear_jobs(chat_id: int):
for jid in user_state.get(chat_id, {}).get("job_ids", []):
try:
scheduler.remove_job(jid)
except Exception:
pass
if chat_id in user_state:
user_state[chat_id]["job_ids"] = []
async def _send(bot, chat_id: int, text: str):
try:
await bot.send_message(chat_id=chat_id, text=text, parse_mode="Markdown")
except Exception as ex:
log.warning("send failed: %s", ex)
def reschedule(app: Application, chat_id: int) -> int:
ud = user_state.get(chat_id)
if not ud or not ud.get("schedule"):
return 0
clear_jobs(chat_id)
remind_mins = ud.get("remind_minutes", DEFAULT_REMIND_MINS)
now = datetime.now(ZoneInfo(TIMEZONE))
job_ids = []
remind_count = 0
for s, e, ev in all_upcoming(ud["schedule"], days_ahead=30):
name = ev.get("name", "Event")
cat = ev.get("cat", "")
dur = ev.get("dur", 60)
tag = f"{chat_id}_{s.isoformat()}_{name[:8]}"
if remind_mins > 0:
rdt = s - timedelta(minutes=remind_mins)
if rdt > now:
jid = f"pre_{tag}"
txt = (
f"⏰ *In {remind_mins} min* — *{name}*\n"
f"🕐 {s.strftime('%H:%M')}–{e.strftime('%H:%M')} · {fmt_dur(dur)}\n"
f"📂 {cat}"
)
scheduler.add_job(
_send, DateTrigger(run_date=rdt),
args=[app.bot, chat_id, txt],
id=jid, replace_existing=True,
)
job_ids.append(jid)
remind_count += 1
if s > now:
jid = f"now_{tag}"
txt = (
f"🚀 *Starting now — {name}*\n"
f"⏱ Until {e.strftime('%H:%M')} ({fmt_dur(dur)})\n"
f"📂 {cat}"
)
scheduler.add_job(
_send, DateTrigger(run_date=s),
args=[app.bot, chat_id, txt],
id=jid, replace_existing=True,
)
job_ids.append(jid)
ud["job_ids"] = job_ids
log.info("Scheduled %d jobs for chat %d", len(job_ids), chat_id)
return remind_count
# ═══════════════════════════════════════════════════════════════════════════
# BOT COMMAND HANDLERS
# ═══════════════════════════════════════════════════════════════════════════
HELP_TEXT = """
👋 *Chronos Assistant*
I read your Chronos schedule and send reminders before events.
*Load / update schedule*
Just paste your Chronos save code (from the 💾 Save button).
*Commands*
/today — Events today
/tomorrow — Events tomorrow
/week — Next 7 days
/next — Your next upcoming event
/remind 15 — Set reminder lead-time in minutes (0 = disable)
/status — Schedule summary
/timezone — Show or change timezone
/help — This message
To change and create the schedule visit:
https://chronos_v1.oneapp.dev or https://alekseiavgustin.github.io/ChronOs/
""".strip()
async def cmd_start(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
cid = update.effective_chat.id
user = update.effective_user
user_state.setdefault(cid, {"schedule": None, "remind_minutes": DEFAULT_REMIND_MINS, "job_ids": []})
stats["total_users"].add(cid)
if ADMIN_CHAT_ID and cid != ADMIN_CHAT_ID:
try:
name = user.username or user.first_name or str(cid)
await ctx.bot.send_message(
ADMIN_CHAT_ID,
f"👤 New user: @{name} (`{cid}`)",
parse_mode="Markdown",
)
except Exception:
pass
await update.message.reply_text(HELP_TEXT, parse_mode="Markdown")
async def cmd_help(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text(HELP_TEXT, parse_mode="Markdown")
async def cmd_today(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
cid = update.effective_chat.id
ud = user_state.get(cid, {})
if not ud.get("schedule"):
await update.message.reply_text("No schedule loaded yet — paste your Chronos save code!")
return
tz = ZoneInfo(TIMEZONE)
today = datetime.now(tz).date()
evts = [(s, e, ev) for s, e, ev in all_upcoming(ud["schedule"], 1) if s.date() == today]
if not evts:
await update.message.reply_text(f"📭 Nothing scheduled for today ({today.strftime('%a %d %b')}).")
return
lines = [f"📅 *Today — {today.strftime('%A, %d %b')}*\n"]
lines += [fmt_ev(ev, s, e) for s, e, ev in evts]
lines.append(f"\n⏱ Total: {fmt_dur(sum(ev.get('dur',0) for _,_,ev in evts))}")
await update.message.reply_text("\n".join(lines), parse_mode="Markdown")
async def cmd_tomorrow(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
cid = update.effective_chat.id
ud = user_state.get(cid, {})
if not ud.get("schedule"):
await update.message.reply_text("No schedule loaded yet — paste your Chronos save code!")
return
tz = ZoneInfo(TIMEZONE)
tmrw = (datetime.now(tz) + timedelta(days=1)).date()
evts = [(s, e, ev) for s, e, ev in all_upcoming(ud["schedule"], 2) if s.date() == tmrw]
if not evts:
await update.message.reply_text(f"📭 Nothing scheduled for tomorrow ({tmrw.strftime('%a %d %b')}).")
return
lines = [f"📅 *Tomorrow — {tmrw.strftime('%A, %d %b')}*\n"]
lines += [fmt_ev(ev, s, e) for s, e, ev in evts]
lines.append(f"\n⏱ Total: {fmt_dur(sum(ev.get('dur',0) for _,_,ev in evts))}")
await update.message.reply_text("\n".join(lines), parse_mode="Markdown")
async def cmd_week(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
cid = update.effective_chat.id
ud = user_state.get(cid, {})
if not ud.get("schedule"):
await update.message.reply_text("No schedule loaded yet — paste your Chronos save code!")
return
tz = ZoneInfo(TIMEZONE)
today = datetime.now(tz).date()
evts = [(s, e, ev) for s, e, ev in all_upcoming(ud["schedule"], 7)]
if not evts:
await update.message.reply_text("📭 Nothing in the next 7 days.")
return
lines = ["📆 *Next 7 days*\n"]
cur_day = None
for s, e, ev in evts:
d = s.date()
if d != cur_day:
cur_day = d
if d == today:
label = f"Today, {d.strftime('%d %b')}"
elif d == today + timedelta(days=1):
label = f"Tomorrow, {d.strftime('%d %b')}"
else:
label = d.strftime("%A, %d %b")
lines.append(f"\n*{label}*")
lines.append(fmt_ev(ev, s, e))
total = sum(ev.get("dur", 0) for _, _, ev in evts)
lines.append(f"\n⏱ Week total: {fmt_dur(total)}")
await update.message.reply_text("\n".join(lines), parse_mode="Markdown")
async def cmd_next(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
cid = update.effective_chat.id
ud = user_state.get(cid, {})
if not ud.get("schedule"):
await update.message.reply_text("No schedule loaded yet — paste your Chronos save code!")
return
tz = ZoneInfo(TIMEZONE)
now = datetime.now(tz)
upcoming = [(s, e, ev) for s, e, ev in all_upcoming(ud["schedule"], 7) if s > now]
if not upcoming:
await update.message.reply_text("📭 No upcoming events in the next 7 days.")
return
s, e, ev = upcoming[0]
delta = s - now
hrs, rem = divmod(int(delta.total_seconds()), 3600)
mins_left = rem // 60
if delta.days >= 1:
when = f"in {delta.days}d {hrs % 24}h"
elif hrs:
when = f"in {hrs}h {mins_left}m"
else:
when = f"in {mins_left}m"
text = (
f"⏭ *Next event — {when}*\n\n"
f"{fmt_ev(ev, s, e)}\n"
f"📅 {s.strftime('%A, %d %b')}"
)
await update.message.reply_text(text, parse_mode="Markdown")
async def cmd_remind(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
cid = update.effective_chat.id
user_state.setdefault(cid, {"schedule": None, "remind_minutes": DEFAULT_REMIND_MINS, "job_ids": []})
if not ctx.args:
curr = user_state[cid].get("remind_minutes", DEFAULT_REMIND_MINS)
await update.message.reply_text(
f"⏰ Reminder lead-time: *{curr} min* before each event.\n"
"Change with `/remind 10` (use 0 to disable early reminder).",
parse_mode="Markdown",
)
return
try:
mins = int(ctx.args[0])
assert 0 <= mins <= 120
except (ValueError, AssertionError):
await update.message.reply_text("Usage: `/remind 15` (0–120 minutes)", parse_mode="Markdown")
return
user_state[cid]["remind_minutes"] = mins
count = reschedule(ctx.application, cid) if user_state[cid].get("schedule") else 0
if mins == 0:
await update.message.reply_text(
"✅ Early reminders *disabled* — you'll still get a ping exactly at start time.",
parse_mode="Markdown",
)
else:
await update.message.reply_text(
f"✅ Reminder lead-time set to *{mins} min*. {count} upcoming reminders rescheduled.",
parse_mode="Markdown",
)
async def cmd_status(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
cid = update.effective_chat.id
ud = user_state.get(cid, {})
if not ud.get("schedule"):
await update.message.reply_text("No schedule loaded. Paste your Chronos save code!")
return
sch = ud["schedule"]
tz = ZoneInfo(TIMEZONE)
ws_str = week_start_dt(sch.get("ws", datetime.now(tz).isoformat()), tz).strftime("%d %b %Y")
text = (
f"📋 *Schedule status*\n\n"
f"*Project:* {sch.get('proj','—')}\n"
f"*Base week:* {ws_str}\n"
f"*Events:* {len(sch.get('events',[]))}\n"
f"*Unscheduled tasks:* {len(sch.get('tasks',[]))}\n"
f"*Reminder lead-time:* {ud.get('remind_minutes', DEFAULT_REMIND_MINS)} min\n"
f"*Active jobs:* {len(ud.get('job_ids',[]))}\n"
f"*Timezone:* {TIMEZONE}\n\n"
"Paste a new Chronos code any time to update."
)
await update.message.reply_text(text, parse_mode="Markdown")
async def cmd_admin(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
cid = update.effective_chat.id
if cid != ADMIN_CHAT_ID:
return
total_jobs = sum(len(u.get("job_ids", [])) for u in user_state.values())
loaded = sum(1 for u in user_state.values() if u.get("schedule"))
text = (
f"📊 *Chronos Bot — Admin Stats*\n\n"
f"👥 Unique users: {len(stats['total_users'])}\n"
f"📥 Schedules loaded: {stats['schedules_loaded']}\n"
f"🟢 Active sessions: {len(user_state)}\n"
f"📅 Sessions with schedule: {loaded}\n"
f"🔔 Scheduled reminder jobs: {total_jobs}\n"
f"🌐 Timezone: {TIMEZONE}\n\n"
"Use /broadcast <message> to notify all users."
)
await update.message.reply_text(text, parse_mode="Markdown")
async def cmd_broadcast(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
cid = update.effective_chat.id
if cid != ADMIN_CHAT_ID:
return
if not ctx.args:
await update.message.reply_text(
"Usage: `/broadcast Your message here`\n"
"Sends to all users who have loaded a schedule.",
parse_mode="Markdown",
)
return
msg = " ".join(ctx.args)
targets = list(stats["total_users"])
ok, fail = 0, 0
for uid in targets:
try:
await ctx.bot.send_message(
uid,
f"📢 *Message from Chronos*\n\n{msg}",
parse_mode="Markdown",
)
ok += 1
except Exception:
fail += 1
await update.message.reply_text(
f"📢 Broadcast sent\n✅ {ok} delivered · ❌ {fail} failed",
parse_mode="Markdown",
)
async def cmd_timezone(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
global TIMEZONE
cid = update.effective_chat.id
if not ctx.args:
now = datetime.now(ZoneInfo(TIMEZONE))
await update.message.reply_text(
f"🌐 Current timezone: *{TIMEZONE}*\n"
f"🕐 Local time now: *{now.strftime('%H:%M, %a %d %b')}*\n\n"
"Change with `/timezone Asia/Singapore`\n"
"Other examples:\n"
"`/timezone Asia/Makassar` — Bali\n"
"`/timezone Asia/Singapore` — SGT\n"
"`/timezone Europe/Moscow` — Moscow\n"
"`/timezone Europe/London` — London\n"
"`/timezone America/New_York` — New York",
parse_mode="Markdown",
)
return
tz_input = ctx.args[0].strip()
try:
ZoneInfo(tz_input)
except Exception:
await update.message.reply_text(
f"❌ Unknown timezone `{tz_input}`.\n"
"Use a standard tz name like `Asia/Singapore` or `Europe/London`.\n"
"Full list: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones",
parse_mode="Markdown",
)
return
TIMEZONE = tz_input
scheduler.configure(timezone=TIMEZONE)
count = 0
for uid in user_state:
if user_state[uid].get("schedule"):
reschedule(ctx.application, uid)
count += 1
now = datetime.now(ZoneInfo(TIMEZONE))
await update.message.reply_text(
f"✅ Timezone set to *{TIMEZONE}*\n"
f"🕐 Local time now: *{now.strftime('%H:%M, %a %d %b')}*\n"
f"🔔 Rescheduled reminders for {count} active user(s).",
parse_mode="Markdown",
)
# ═══════════════════════════════════════════════════════════════════════════
# MULTI-PART CODE BUFFER
# ═══════════════════════════════════════════════════════════════════════════
async def try_parse_buffer(ctx: ContextTypes.DEFAULT_TYPE):
cid = ctx.job.data["cid"]
app = ctx.job.data["app"]
buf = code_buffer.pop(cid, None)
if not buf:
return
full_code = "".join(buf["parts"])
try:
sch = parse_code(full_code)
if "events" not in sch and "tasks" not in sch:
raise ValueError("missing keys")
user_state.setdefault(cid, {"schedule": None, "remind_minutes": DEFAULT_REMIND_MINS, "job_ids": []})
user_state[cid]["schedule"] = sch
stats["total_users"].add(cid)
stats["schedules_loaded"] += 1
count = reschedule(app, cid)
tz = ZoneInfo(TIMEZONE)
today = datetime.now(tz).date()
today_evts = sum(1 for s, _, _ in all_upcoming(sch, 1) if s.date() == today)
week_evts = len(all_upcoming(sch, 7))
reply = (
f"✅ *Schedule loaded!*\n\n"
f"📁 *{sch.get('proj','My Schedule')}*\n"
f"📅 {len(sch.get('events',[]))} events · "
f"{len(sch.get('tasks',[]))} unscheduled tasks\n"
f"🔔 {count} reminders set "
f"({user_state[cid]['remind_minutes']}min before + at start)\n\n"
)
if today_evts:
reply += f"You have *{today_evts}* event(s) today → /today\n"
reply += f"*{week_evts}* event(s) in the next 7 days → /week"
await ctx.bot.send_message(cid, reply, parse_mode="Markdown")
except Exception as ex:
log.debug("Buffer parse failed (%d chars): %s", len(full_code), ex)
await ctx.bot.send_message(
cid,
"❌ Couldn't read that code — make sure you copy the *entire* save code from Chronos.",
parse_mode="Markdown",
)
# ═══════════════════════════════════════════════════════════════════════════
# GENERIC MESSAGE HANDLER
# ═══════════════════════════════════════════════════════════════════════════
async def handle_message(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
cid = update.effective_chat.id
text = update.message.text.strip()
looks_like_code = len(text) > 10 and " " not in text and "\n" not in text
if looks_like_code:
if cid not in code_buffer:
code_buffer[cid] = {"parts": [], "job": None}
code_buffer[cid]["parts"].append(text)
old_job = code_buffer[cid].get("job")
if old_job:
try:
old_job.schedule_removal()
except Exception:
pass
job = ctx.application.job_queue.run_once(
try_parse_buffer,
when=3,
data={"cid": cid, "app": ctx.application},
name=f"codeparse_{cid}",
)
code_buffer[cid]["job"] = job
return
ud = user_state.get(cid, {})
if not ud.get("schedule"):
await update.message.reply_text("Paste your Chronos save code to get started, or use /help.")
else:
await update.message.reply_text(
"Try /today, /tomorrow, /week, /next — or paste a new Chronos code to update your schedule."
)
# ═══════════════════════════════════════════════════════════════════════════
# POST INIT
# ═══════════════════════════════════════════════════════════════════════════
async def post_init(app: Application):
scheduler.start()
asyncio.create_task(self_ping())
log.info("Scheduler started")
# ═══════════════════════════════════════════════════════════════════════════
# ENTRY POINT
# ═══════════════════════════════════════════════════════════════════════════
def main():
if not BOT_TOKEN:
print("❌ BOT_TOKEN environment variable not set!")
return
# Start health server in background thread (keeps Render happy)
threading.Thread(target=start_health_server, daemon=True).start()
log.info("Health server started on port %s", os.environ.get("PORT", 8080))
app = Application.builder().token(BOT_TOKEN).post_init(post_init).build()
app.add_handler(CommandHandler("start", cmd_start))
app.add_handler(CommandHandler("help", cmd_help))
app.add_handler(CommandHandler("today", cmd_today))
app.add_handler(CommandHandler("tomorrow", cmd_tomorrow))
app.add_handler(CommandHandler("week", cmd_week))
app.add_handler(CommandHandler("next", cmd_next))
app.add_handler(CommandHandler("remind", cmd_remind))
app.add_handler(CommandHandler("status", cmd_status))
app.add_handler(CommandHandler("admin", cmd_admin))
app.add_handler(CommandHandler("broadcast", cmd_broadcast))
app.add_handler(CommandHandler("timezone", cmd_timezone))
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
log.info("Chronos Bot running — timezone: %s", TIMEZONE)
app.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
main()