Spaces:
Runtime error

File size: 35,226 Bytes
6e8e31e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfebfec
442ce56
6e8e31e
048ae4b
6e8e31e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
442ce56
 
 
6e8e31e
d4da14e
6e8e31e
 
dda71dc
6e8e31e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5d5a84
d398533
6e8e31e
 
dda71dc
 
 
6e8e31e
 
 
 
e0a8808
dda71dc
e0a8808
6e8e31e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0a8808
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4da14e
e0a8808
 
 
 
 
 
 
 
 
6e8e31e
e0a8808
 
 
 
 
 
 
 
6e8e31e
 
 
 
 
 
 
 
bfebfec
 
 
 
 
 
 
 
 
 
6e8e31e
bfebfec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e8e31e
 
 
 
 
55d508d
048ae4b
55d508d
048ae4b
 
 
 
 
 
55d508d
048ae4b
55d508d
 
 
 
6e8e31e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0a8808
6e8e31e
 
 
e0a8808
 
6e8e31e
 
 
e0a8808
6e8e31e
 
e0a8808
 
 
 
 
 
6e8e31e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0a8808
 
 
 
 
 
 
6e8e31e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0a8808
 
 
 
 
 
 
 
6e8e31e
e0a8808
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e8e31e
e0a8808
 
 
 
 
 
6e8e31e
 
 
 
 
 
fa7bcf9
 
 
 
c9b0948
fa7bcf9
6e8e31e
 
 
 
 
442ce56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9b0948
442ce56
 
6e8e31e
 
 
 
 
 
 
 
 
bfebfec
 
 
6e8e31e
 
 
bfebfec
 
 
55d508d
048ae4b
55d508d
 
 
6e8e31e
bfebfec
 
 
 
 
55d508d
bfebfec
55d508d
 
bfebfec
 
 
6e8e31e
55d508d
bfebfec
55d508d
 
bfebfec
6e8e31e
bfebfec
 
 
 
6e8e31e
bfebfec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
442ce56
 
 
 
c9b0948
442ce56
 
bfebfec
 
 
 
 
 
048ae4b
bfebfec
 
dd6e933
d4da14e
 
bfebfec
 
 
 
 
 
 
 
55d508d
 
 
 
bfebfec
e0a8808
 
 
 
 
 
 
 
 
 
 
 
bfebfec
6e8e31e
 
 
 
 
 
 
 
 
dd6e933
 
 
 
 
 
 
6e8e31e
 
 
 
 
dd6e933
 
 
6e8e31e
 
 
 
 
 
 
 
 
 
 
 
 
77184f1
 
 
 
6e8e31e
 
77184f1
6e8e31e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77184f1
6e8e31e
 
 
 
 
dd6e933
6e8e31e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
"""
ARMS Slot Monitor  –  Multi-User Telegram Bot Service
======================================================
Two threads run side-by-side:
  1. Bot Thread   – handles /start, contact sharing, admin /user commands
  2. Monitor Thread – polls ARMS API every 15 s; broadcasts to all subscribers

Setup:
    py -m pip install requests
    py api_monitor.py

Admin commands (only YOUR chat ID can use these):
    /user <phone>    – approve a subscriber (sends them congrats)
    /users           – list all approved subscribers
    /remove <phone>  – remove a subscriber
"""

import os
import requests
import json
import time
import threading
import logging
import sqlite3
import html
from datetime import datetime
import pytz
from pathlib import Path
import signal
import sys
import http.server
import socketserver

# ─────────────────────────────────────────────────────
#  CONFIGURATION
# ─────────────────────────────────────────────────────

BASE_URL = (
    "https://arms.sse.saveetha.com/Handler/Student.ashx"
    "?Page=StudentInfobyId&Mode=GetCourseBySlot&Id={slot_id}"
)

# ARMS credentials for auto-login (hardcoded)
ARMS_USERNAME = "P192512045"   # ARMS username / roll number
ARMS_PASSWORD = "welcome"      # ARMS password

ARMS_LOGIN_URL = "https://arms.sse.saveetha.com/Login.aspx?s=exp"

# ARMS session cookie β€” auto-refreshed via login; also settable via /setcookie
COOKIES = {"ASP.NET_SessionId": ""}

# Error alert rate-limiting: only alert admin once per error type per hour
_last_alert: dict[str, float] = {}
ALERT_COOLDOWN = 3600   # seconds

HEADERS = {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "accept-language": "en-US,en;q=0.5",
    "cache-control": "no-cache, no-store",
    "pragma": "no-cache",
    "user-agent": (
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
        "AppleWebKit/537.36 (KHTML, like Gecko) "
        "Chrome/145.0.0.0 Safari/537.36"
    ),
}

# Seconds between slot polls
POLL_INTERVAL = 20

# ── Telegram ─────────────────────
TELEGRAM_BOT_TOKEN = "8340772186:AAGpn5IZx6Af_3oQaomJir2itlfiCmQJI3s"
TELEGRAM_API       = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}"

ADMIN_CHAT_ID      = "8467592443"        # only this ID can run admin commands
ADMIN_PHONE        = "9360406137"         # your phone number (for reference)
CHANNEL_CHAT_ID    = "-1003845063774"    # private channel β€” all slot alerts go here

# File that stores subscribers across restarts
SUBSCRIBERS_FILE   = Path("subscribers.json")

# Dashboard URL for notifications
DASHBOARD_URL      = "https://your-site.alwaysdata.net"

# ─────────────────────────────────────────────────────
#  LOGGING
# ─────────────────────────────────────────────────────
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s  %(message)s",
    datefmt="%H:%M:%S",
    handlers=[
        logging.StreamHandler(),
        logging.FileHandler("slot_monitor.log", encoding="utf-8"),
    ],
)
log = logging.getLogger(__name__)


# ──────────────────────────────────────────────────────
#  AUTO-LOGIN
# ──────────────────────────────────────────────────────

def auto_login() -> bool:
    """
    Log into ARMS with ARMS_USERNAME / ARMS_PASSWORD and update
    COOKIES with the fresh ASP.NET_SessionId.
    Returns True on success, False on failure.
    """
    if not ARMS_USERNAME or not ARMS_PASSWORD:
        log.warning("  [Login] No credentials set β€” skipping auto-login.")
        return False

    max_retries = 3
    for attempt in range(1, max_retries + 1):
        try:
            log.info(f"  [Login] Attempting auto-login to ARMS (Attempt {attempt})…")
            s = requests.Session()

            # Step 1: GET login page to grab hidden ASP.NET fields
            r = s.get(ARMS_LOGIN_URL, timeout=30)
            r.raise_for_status()

            import re
            def _field(name):
                m = re.search(rf'id="{name}"[^>]*value="([^"]*)"|name="{name}"[^>]*value="([^"]*)"|value="([^"]*)"[^>]*name="{name}"', r.text)
                return (m.group(1) or m.group(2) or m.group(3) or "") if m else ""

            viewstate       = _field("__VIEWSTATE")
            eventvalidation = _field("__EVENTVALIDATION")
            vsgenerator     = _field("__VIEWSTATEGENERATOR")

            # Step 2: POST credentials
            payload = {
                "__VIEWSTATE":          viewstate,
                "__VIEWSTATEGENERATOR": vsgenerator,
                "__EVENTVALIDATION":    eventvalidation,
                "txtusername":          ARMS_USERNAME,
                "txtpassword":          ARMS_PASSWORD,
                "btnlogin":             "Login",
            }
            resp = s.post(ARMS_LOGIN_URL, data=payload, timeout=30, allow_redirects=True)

            # Step 3: Extract session cookie
            session_id = s.cookies.get("ASP.NET_SessionId") or resp.cookies.get("ASP.NET_SessionId")

            if session_id:
                COOKIES["ASP.NET_SessionId"] = session_id
                _last_alert.clear()   # reset cooldowns β€” fresh session
                log.info(f"  [Login] βœ… Auto-login successful! Session: {session_id[:12]}…")
                send_message(
                    ADMIN_CHAT_ID,
                    f"πŸ”‘ <b>Auto-login successful!</b>\n"
                    f"New session: <code>{session_id[:16]}…</code>"
                )
                return True
            else:
                log.warning(f"  [Login] ⚠ Attempt {attempt} failed: No session ID in response.")
                if attempt == max_retries:
                    log.error("  [Login] ❌ All login attempts failed.")
                    send_message(ADMIN_CHAT_ID, "❌ <b>Auto-login failed!</b>\nCheck credentials or use /setcookie manually.")
                    return False
                time.sleep(2)

        except Exception as e:
            log.warning(f"  [Login] ⚠ Attempt {attempt} exception: {e}")
            if attempt == max_retries:
                log.error(f"  [Login] ❌ Exception during auto-login after {max_retries} tries: {e}")
                send_message(ADMIN_CHAT_ID, f"❌ <b>Auto-login error after retries:</b> {e}")
                return False
            time.sleep(2)
    return False



# ─────────────────────────────────────────────────────
#  SUBSCRIBER STORAGE
# ─────────────────────────────────────────────────────

def load_db() -> dict:
    default_db = {
        "approved": [], 
        "pending": {},
        "slots": [
            {"id": 4, "label": "A"},
            {"id": 5, "label": "B"},
            {"id": 2, "label": "C"},
            {"id": 7, "label": "D"}
        ]
    }
    if SUBSCRIBERS_FILE.exists():
        try:
            data = json.loads(SUBSCRIBERS_FILE.read_text(encoding="utf-8"))
            # Ensure new schema fields exist
            if "slots" not in data:
                data["slots"] = default_db["slots"]
            return data
        except json.JSONDecodeError:
            pass
    return default_db
    
# ─────────────────────────────────────────────────────
#  ANALYTICS STORAGE (SQLite)
# ─────────────────────────────────────────────────────

def init_history_db():
    conn = sqlite3.connect("history.db")
    c = conn.cursor()
    c.execute('''
        CREATE TABLE IF NOT EXISTS history (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
            slot_id INTEGER,
            course_count INTEGER
        )
    ''')
    conn.commit()
    conn.close()

def log_history(slot_id: int, course_count: int):
    try:
        conn = sqlite3.connect("history.db")
        c = conn.cursor()
        c.execute("INSERT INTO history (slot_id, course_count) VALUES (?, ?)", (slot_id, course_count))
        conn.commit()
        conn.close()
    except Exception as e:
        log.error(f"  [DB] SQLite history logging failed: {e}")

init_history_db()


def save_db(db: dict) -> None:
    SUBSCRIBERS_FILE.write_text(json.dumps(db, indent=2, ensure_ascii=False), encoding="utf-8")

# ─────────────────────────────────────────────────────
#  GLOBAL METRICS & TIMEZONE
# ─────────────────────────────────────────────────────

IST = pytz.timezone('Asia/Kolkata')

def get_ist_now():
    return datetime.now(IST)

GLOBAL_METRICS = {
    "start_time": get_ist_now(),
    "polls": 0,
    "latency": "0.00s",
    "total_courses": 0
}

# ─────────────────────────────────────────────────────
#  TELEGRAM HELPERS
# ─────────────────────────────────────────────────────

def tg_post(method: str, **kwargs) -> dict:
    """POST to any Telegram Bot API method."""
    try:
        r = requests.post(f"{TELEGRAM_API}/{method}", json=kwargs, timeout=10)
        return r.json()
    except Exception as e:
        log.warning(f"[Telegram] {method} failed: {e}")
        return {}


def send_message(chat_id: str | int, text: str, reply_markup=None, inline_keyboard=None) -> None:
    payload = {"chat_id": str(chat_id), "text": text, "parse_mode": "HTML"}
    if reply_markup:
        payload["reply_markup"] = reply_markup
    elif inline_keyboard:
        payload["reply_markup"] = {"inline_keyboard": inline_keyboard}
    tg_post("sendMessage", **payload)


def broadcast(text: str, include_dashboard: bool = False) -> None:
    """Send a slot alert to the private channel."""
    log.info(f"  [Bot] Sending alert to channel {CHANNEL_CHAT_ID}…")
    
    inline_kb = None
    if include_dashboard and DASHBOARD_URL:
        inline_kb = [[{"text": "πŸ”— Open Dashboard", "url": DASHBOARD_URL}]]
        
    send_message(CHANNEL_CHAT_ID, text, inline_keyboard=inline_kb)


def set_bot_profile() -> None:
    """Update the bot's Bio (short description) and About (description)."""
    bio = "πŸš€ Instant real-time alerts for ARMS course slots. Never miss an opening! πŸŽ“"
    about = (
        "ARMS Slot Monitor β€” The most reliable way to track course slot availability in real-time. ⚑\n\n"
        "βœ… Instant Telegram notifications\n"
        "βœ… 24/7 Slot Monitoring\n"
        "βœ… Secure & Multi-user\n\n"
        "Monitoring slots so you don't have to! πŸš€"
    )

    log.info("  [Bot] Updating bot profile (Bio/About)…")
    
    # 1. Set Short Description (Bio)
    r_bio = tg_post("setMyShortDescription", short_description=bio)
    if r_bio.get("ok"):
        log.info("  [Bot] βœ… Bio updated successfully.")
    else:
        log.warning(f"  [Bot] ⚠ Bio update failed: {r_bio.get('description')}")

    # 2. Set Description (About)
    r_about = tg_post("setMyDescription", description=about)
    if r_about.get("ok"):
        log.info("  [Bot] βœ… About description updated successfully.")
    else:
        log.warning(f"  [Bot] ⚠ About description update failed: {r_about.get('description')}")


# ─────────────────────────────────────────────────────
#  BOT COMMAND HANDLERS
# ─────────────────────────────────────────────────────

def handle_start(chat_id: str, first_name: str) -> None:
    """Send the 'Share Phone' button to a new user."""
    keyboard = {
        "keyboard": [[{"text": "πŸ“± Share My Phone Number", "request_contact": True}]],
        "resize_keyboard": True,
        "one_time_keyboard": True,
    }
    send_message(
        chat_id,
        f"πŸ‘‹ Hello <b>{first_name}</b>!\n\n"
        "Welcome to <b>ARMS Slot Notifier</b>.\n"
        "Tap the button below to share your phone number so the admin can activate your subscription.",
        reply_markup=keyboard,
    )


def handle_contact(chat_id: str, phone: str, first_name: str) -> None:
    """Store the pending user and notify admin."""
    phone = phone.lstrip("+").strip()
    db = load_db()

    # Already approved?
    for sub in db["approved"]:
        if sub.get("phone") == phone:
            send_message(chat_id, "βœ… You are already an approved subscriber!")
            return

    db["pending"][phone] = {"chat_id": str(chat_id), "name": first_name}
    save_db(db)

    send_message(
        chat_id,
        "βœ… <b>Phone number received!</b>\n\n"
        "The admin has been notified. You'll get a confirmation message once approved.\n"
        "Hang tight! πŸŽ‰",
    )

    # Notify admin
    send_message(
        ADMIN_CHAT_ID,
        f"πŸ“¬ <b>New subscriber request</b>\n"
        f"Name : {first_name}\n"
        f"Phone: <code>{phone}</code>\n\n"
        f"Approve with:\n<code>/user {phone}</code>",
    )
    log.info(f"  [Bot] New pending user: {first_name} ({phone})")


def handle_add_user(chat_id: str, phone: str) -> None:
    """Admin command: /user <phone> β€” approve a subscriber."""
    if str(chat_id) != ADMIN_CHAT_ID:
        send_message(chat_id, "β›” You are not authorised to use this command.")
        return

    phone = phone.lstrip("+").strip()
    db = load_db()

    # Already approved?
    for sub in db["approved"]:
        if sub.get("phone") == phone:
            send_message(chat_id, f"ℹ️ <code>{phone}</code> is already approved.")
            return

    # Look up in pending
    pending_info = db["pending"].get(phone)

    if pending_info:
        subscriber_chat_id = pending_info["chat_id"]
        subscriber_name    = pending_info.get("name", "Subscriber")
        del db["pending"][phone]
    else:
        # Admin is adding someone manually (user hasn't started the bot yet)
        subscriber_chat_id = None
        subscriber_name    = "User"

    db["approved"].append({
        "chat_id": subscriber_chat_id,
        "phone":   phone,
        "name":    subscriber_name,
        "added":   datetime.now().isoformat(),
    })
    save_db(db)

    admin_msg = (
        f"βœ… <b>{subscriber_name}</b> (<code>{phone}</code>) approved!\n"
        f"Total subscribers: {len(db['approved'])}"
    )
    send_message(ADMIN_CHAT_ID, admin_msg)

    # Send congrats to new subscriber (if we have their chat ID)
    if subscriber_chat_id:
        send_message(
            subscriber_chat_id,
            "πŸŽ‰ <b>Congratulations! You're now subscribed!</b>\n\n"
            "You will receive instant Telegram notifications whenever\n"
            "course slots change in ARMS.\n\n"
            "Stay tuned β€” we'll alert you the moment a slot opens! πŸš€",
        )
        log.info(f"  [Bot] βœ… Approved & notified: {subscriber_name} ({phone})")
    else:
        log.info(f"  [Bot] βœ… Approved (offline): {phone}")
        send_message(
            ADMIN_CHAT_ID,
            f"⚠️ {phone} has not started the bot yet β€” they won't receive messages until they do.",
        )


def handle_list_users(chat_id: str) -> None:
    """Admin command: /users β€” list all approved subscribers."""
    if str(chat_id) != ADMIN_CHAT_ID:
        send_message(chat_id, "β›” Not authorised.")
        return

    db = load_db()
    approved = db.get("approved", [])
    pending  = db.get("pending", {})

    if not approved and not pending:
        send_message(chat_id, "πŸ“‹ No subscribers yet.")
        return

    lines = [f"<b>πŸ“‹ Subscribers ({len(approved)})</b>"]
    for i, sub in enumerate(approved, 1):
        lines.append(f"{i}. {sub.get('name','?')} β€” <code>{sub.get('phone','?')}</code>")

    if pending:
        lines.append(f"\n<b>⏳ Pending ({len(pending)})</b>")
        for phone, info in pending.items():
            lines.append(f"β€’ {info.get('name','?')} β€” <code>{phone}</code>")

    send_message(chat_id, "\n".join(lines))


def handle_remove_user(chat_id: str, phone: str) -> None:
    """Admin command: /remove <phone> β€” remove a subscriber."""
    if str(chat_id) != ADMIN_CHAT_ID:
        send_message(chat_id, "β›” Not authorised.")
        return

    phone = phone.lstrip("+").strip()
    db = load_db()
    before = len(db["approved"])
    db["approved"] = [s for s in db["approved"] if s.get("phone") != phone]

    if len(db["approved"]) < before:
        save_db(db)
        send_message(chat_id, f"βœ… <code>{phone}</code> removed.")
        log.info(f"  [Bot] Removed subscriber: {phone}")
    else:
        send_message(chat_id, f"❓ <code>{phone}</code> not found in approved list.")


def handle_set_cookie(chat_id: str, value: str) -> None:
    """Admin command: /setcookie <value> β€” update session cookie live."""
    if str(chat_id) != ADMIN_CHAT_ID:
        send_message(chat_id, "β›” Not authorised.")
        return

    value = value.strip()
    if not value:
        send_message(chat_id, "Usage: /setcookie &lt;ASP.NET_SessionId value&gt;")
        return

    COOKIES["ASP.NET_SessionId"] = value
    # Clear all error alert cooldowns so next poll will re-verify
    _last_alert.clear()
    log.info(f"  [Bot] πŸ”‘ Session cookie updated by admin.")
    send_message(
        chat_id,
        f"βœ… <b>Session cookie updated!</b>\n"
        f"<code>{value[:20]}…</code>\n\n"
        "Monitor will use the new cookie on the next poll (within 15 s).",
    )


# ─────────────────────────────────────────────────────
#  BOT POLLING THREAD
# ─────────────────────────────────────────────────────

def bot_thread():
    """Long-poll the Telegram Bot API for incoming messages."""
    log.info("  [Bot] Starting Telegram bot polling…")
    offset = 0

    while True:
        try:
            resp = requests.get(
                f"{TELEGRAM_API}/getUpdates",
                params={"offset": offset, "timeout": 30},
                timeout=35,
            ).json()

            for update in resp.get("result", []):
                offset = update["update_id"] + 1
                msg = update.get("message", {})
                if not msg:
                    continue

                chat_id    = str(msg["chat"]["id"])
                first_name = msg["chat"].get("first_name", "there")
                text       = msg.get("text", "").strip()
                contact    = msg.get("contact")

                # Contact shared
                if contact:
                    handle_contact(chat_id, contact.get("phone_number", ""), first_name)
                    continue

                # Commands
                if text.startswith("/start"):
                    handle_start(chat_id, first_name)

                elif text.startswith("/user "):
                    phone = text[6:].strip()
                    handle_add_user(chat_id, phone)

                elif text == "/users":
                    handle_list_users(chat_id)

                elif text.startswith("/remove "):
                    phone = text[8:].strip()
                    handle_remove_user(chat_id, phone)

                elif text.startswith("/setcookie "):
                    value = text[11:].strip()
                    handle_set_cookie(chat_id, value)

                elif text == "/setcookie":
                    send_message(chat_id, "Usage: /setcookie &lt;ASP.NET_SessionId value&gt;\n\nGet it from browser DevTools β†’ Application β†’ Cookies β†’ arms.sse.saveetha.com")

                elif text == "/dashboard":
                    if DASHBOARD_URL:
                        send_message(chat_id, f"πŸ“Š <b>ARMS Monitor Dashboard</b>\n\nClick the button below to view live analytics and configuration.", 
                                     inline_keyboard=[[{"text": "πŸ”— Open Dashboard", "url": DASHBOARD_URL}]])
                    else:
                        send_message(chat_id, "⚠️ Dashboard URL not configured. Contact admin.")

        except Exception as e:
            log.warning(f"  [Bot] Poll error: {e}")
            time.sleep(5)


# ─────────────────────────────────────────────────────
#  ERROR ALERT HELPER
# ─────────────────────────────────────────────────────

def alert_admin(key: str, message: str) -> None:
    """Send an error alert to admin, rate-limited to once per hour per key."""
    now = time.time()
    if now - _last_alert.get(key, 0) < ALERT_COOLDOWN:
        return   # already alerted recently
    _last_alert[key] = now
    log.error(f"  [ALERT] {message}")
    try:
        send_message(
            ADMIN_CHAT_ID,
            f"⚠️ <b>ARMS Monitor Alert</b>\n\n{message}\n\n"
            f"<i>πŸ• {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</i>\n"
            "Use /setcookie &lt;value&gt; to update session cookie.",
        )
    except Exception:
        pass


# ─────────────────────────────────────────────────────
#  SLOT MONITOR HELPERS
# ─────────────────────────────────────────────────────

def fetch_courses(slot_id: int) -> list[dict] | None:
    url = BASE_URL.format(slot_id=slot_id)
    max_retries = 3
    
    for attempt in range(1, max_retries + 1):
        try:
            resp = requests.get(url, headers=HEADERS, cookies=COOKIES, timeout=30)
            resp.raise_for_status()
            body = resp.text.strip()
            
            if not body:
                # Try auto-login first before alerting admin
                log.warning(f"  [Slot {slot_id}] Empty response (Attempt {attempt}) β€” attempting auto-login…")
                if auto_login():
                    # Retry the request with the new cookie
                    try:
                        resp2 = requests.get(url, headers=HEADERS, cookies=COOKIES, timeout=30)
                        body = resp2.text.strip()
                    except Exception:
                        body = ""
                
                if not body:
                    if attempt == max_retries:
                        alert_admin(
                            f"slot{slot_id}_empty",
                            f"❌ Slot {slot_id}: Empty response β€” session cookie has likely <b>expired</b>.\n"
                            "Auto-login also failed. Update with /setcookie &lt;new_value&gt;"
                        )
                        return None
                    time.sleep(2)
                    continue

            data = json.loads(body).get("Table", [])
            # Clear any previous empty-response alert for this slot
            _last_alert.pop(f"slot{slot_id}_empty", None)
            return data

        except requests.exceptions.RequestException as e:
            log.warning(f"  [Slot {slot_id}] ⚠ Attempt {attempt} failed: {e}")
            if attempt == max_retries:
                if isinstance(e, requests.exceptions.ConnectionError):
                    alert_admin(f"slot{slot_id}_conn", f"🌐 Slot {slot_id}: <b>Connection error</b> β€” no internet or ARMS is down.")
                elif isinstance(e, requests.exceptions.Timeout):
                    alert_admin(f"slot{slot_id}_timeout", f"⏱ Slot {slot_id}: <b>Request timed out</b> (30s) β€” ARMS may be slow.")
                elif isinstance(e, requests.exceptions.HTTPError):
                    alert_admin(f"slot{slot_id}_http", f"🚫 Slot {slot_id}: <b>HTTP error</b> β€” {e}")
                else:
                    alert_admin(f"slot{slot_id}_err", f"πŸ’₯ Slot {slot_id}: Unexpected error β€” {e}")
                return None
            time.sleep(2)
            
        except Exception as e:
            log.error(f"  [Slot {slot_id}] πŸ’₯ Unexpected exception: {e}")
            return None
    return None


def summarise(courses: list[dict]) -> str:
    available = [c for c in courses if c.get("AvailableCount", 0) > 0]
    if not available:
        return "No open slots."
    
    lines = []
    for c in available[:5]:
        faculty_name = get_faculty_name(c)
        lines.append(f"β€’ {c['SubjectCode']} – {c['AvailableCount']} slots | {faculty_name}")

    if len(available) > 5:
        lines.append(f"  … and {len(available) - 5} more")
    return "\n".join(lines)


def get_faculty_name(course: dict) -> str:
    """Return the best faculty name available from the ARMS payload."""
    faculty_keys = (
        "FacultyName",
        "Faculty",
        "StaffName",
        "Staff",
        "TeacherName",
        "Teacher",
        "EmployeeName",
        "Employee",
        "ProfessorName",
        "Professor",
    )
    for key in faculty_keys:
        value = course.get(key)
        if value is None:
            continue
        faculty_name = str(value).strip()
        if faculty_name and faculty_name.lower() not in {"null", "none", "nan"}:
            return faculty_name
    return "NTA"





# ─────────────────────────────────────────────────────
#  SLOT MONITOR THREAD
# ─────────────────────────────────────────────────────

def monitor_thread():
    log.info("  [Monitor] Starting slot monitor…")
    global _last_alert
    
    # Store known counts per slot
    baselines: dict[int, dict] = {}

    while True:
        db = load_db()
        active_slots = db.get("slots", [])
        
        GLOBAL_METRICS["polls"] += 1
        log.info(f"\n[Poll #{GLOBAL_METRICS['polls']:04d}]  {get_ist_now().strftime('%Y-%m-%d %I:%M:%S %p %Z')}")

        cycle_courses_count = 0
        cycle_start_t = time.time()

        for slot_data in active_slots:
            try:
                slot_id = slot_data["id"]
                slot_label = slot_data["label"]
                
                t0 = time.time()
                courses = fetch_courses(slot_id)
                t1 = time.time()
                
                if courses is None:
                    log.warning(f"  [Slot {slot_label}] ⚠  No response, skipping.")
                    continue

                log.info(f"  [API] Slot {slot_label} fetched in {(t1-t0):.2f}s")
                current_count = len(courses)
                cycle_courses_count += current_count
                
                log_history(slot_id, current_count)

                if slot_id not in baselines:
                    log.info(f"  [Slot {slot_label}] βœ… Baseline: {current_count} courses.")
                    baselines[slot_id] = {"count": current_count, "courses": courses}
                    continue

                prev_count   = baselines[slot_id]["count"]
                prev_courses = baselines[slot_id]["courses"]

                if current_count != prev_count:
                    # Update baseline and file only when data actually changes
                    baselines[slot_id] = {"count": current_count, "courses": courses}
                    with open(f"latest_slot{slot_id}.json", "w", encoding="utf-8") as f:
                        json.dump(courses, f, indent=2, ensure_ascii=False)

                    if current_count > prev_count:
                        # Only notify on INCREASE
                        delta = current_count - prev_count

                        log.info(f"  [Slot {slot_label}] πŸ”” COUNT INCREASED: {prev_count} β†’ {current_count} (+{delta})")

                        prev_ids = {c["SubjectId"]: c for c in prev_courses}
                        curr_ids = {c["SubjectId"]: c for c in courses}

                        added_lines = []
                        for sid, c in curr_ids.items():
                            if sid not in prev_ids:
                                faculty_name = get_faculty_name(c)
                                course_line = (
                                    f"  βž• {html.escape(str(c['SubjectCode']))} – "
                                    f"{html.escape(str(c['SubjectName']))} "
                                    f"({c['AvailableCount']} slots) | Faculty: <b>{html.escape(faculty_name)}</b>"
                                )
                                added_lines.append(course_line)

                        # Build Telegram message
                        tg = [f"<b>πŸ”” ARMS Slot {slot_label}: New Course Added! β–²</b>",
                              f"Courses: <b>{prev_count} β†’ {current_count}</b>  (+{delta})"]
                        if added_lines:
                            tg.append("\n<b>Added:</b>\n" + "\n".join(added_lines))
                        tg.append(f"\nπŸ• <i>{get_ist_now().strftime('%Y-%m-%d %I:%M:%S %p IST')}</i>")
                        tg_text = "\n".join(tg)

                        # Send to Admin and Channel only
                        send_message(ADMIN_CHAT_ID, tg_text)
                        broadcast(tg_text)

                    elif current_count < prev_count:
                        log.info(f"  [Slot {slot_id}] πŸ“‰ Count decreased {prev_count}β†’{current_count} (no notification sent)")
                    else:
                        pass  # Reduced logging: only log on changes

            except Exception as e:
                log.error(f"  [Slot {slot_label}] ❌ Error processing courses: {e}")
        
        cycle_end_t = time.time()
        GLOBAL_METRICS["latency"] = f"{(cycle_end_t - cycle_start_t):.2f}s"
        GLOBAL_METRICS["total_courses"] = cycle_courses_count

        try:
            import json
            with open("metrics.json", "w") as mf:
                json.dump({
                    "start_time": GLOBAL_METRICS["start_time"].isoformat(),
                    "polls": GLOBAL_METRICS["polls"],
                    "latency": GLOBAL_METRICS["latency"],
                    "total_courses": GLOBAL_METRICS["total_courses"]
                }, mf)
        except Exception as e:
            log.error(f"  [Metrics] Failed to save metrics: {e}")

        # Sleep before next poll
        time.sleep(POLL_INTERVAL)


# ─────────────────────────────────────────────────────
#  SHUTDOWN HANDLER
# ─────────────────────────────────────────────────────

def handle_shutdown(signum=None, frame=None):
    """Notify admin and exit gracefully on shutdown signals."""
    sig_name = signal.Signals(signum).name if signum else "Manual shutdown"
    reason_map = {
        "SIGINT":  "Ctrl+C pressed / manual stop",
        "SIGTERM": "Server terminated (platform restart or deploy)",
        "Manual shutdown": "Unhandled exception in monitor thread",
    }
    reason = reason_map.get(sig_name, f"Signal: {sig_name}")
    log.info(f"\n[System] πŸ›‘ Shutdown signal ({sig_name}) received. Notifying admin…")
    try:
        send_message(
            ADMIN_CHAT_ID,
            "πŸ›‘ <b>ARMS Monitor β€” Server Powering Down</b>\n\n"
            f"πŸ“Œ <b>Reason:</b> {reason}\n\n"
            "Monitoring will be paused until the service is back online.\n"
            f"πŸ• <i>{datetime.now().strftime('%Y-%m-%d %H:%M:%S IST')}</i>"
        )
    except Exception as e:
        log.error(f"  [System] Failed to send shutdown message: {e}")
    
    log.info("Goodbye!")
    os._exit(0)  # Kill all threads and exit immediately


# ─────────────────────────────────────────────────────
#  ENTRY POINT
# ─────────────────────────────────────────────────────

if __name__ == "__main__":
    db_init = load_db()
    active_slots_init = db_init.get("slots", [])
    slot_ids_init = [s["id"] for s in active_slots_init]
    
    log.info("=" * 60)
    log.info("  ARMS Slot Monitor  –  Multi-User Bot Service")
    log.info(f"  Admin : {ADMIN_CHAT_ID}  |  Slots: {slot_ids_init}")
    log.info("=" * 60)

    # Ensure subscribers file exists
    if not SUBSCRIBERS_FILE.exists():
        save_db({"approved": [], "pending": {}})
        log.info("  Created subscribers.json")

    # Auto-login to get a fresh session cookie
    if ARMS_USERNAME and ARMS_PASSWORD:
        auto_login()
    else:
        log.info("  [Login] Running with hardcoded session cookie (no credentials set).")

    # Update bot profile (Bio/About)
    set_bot_profile()

    # Startup message to admin
    slot_labels_str = ", ".join(s["label"] for s in active_slots_init) if active_slots_init else "None"
    send_message(
        ADMIN_CHAT_ID,
        "πŸš€ <b>ARMS Slot Monitor is running!</b>\n\n"
        f"πŸ‘ Watching Slots: <b>{slot_labels_str}</b>\n"
        f"⏱ Poll Interval: every <b>{POLL_INTERVAL}s</b>\n"
        "/setcookie &lt;value&gt; – update session cookie live"
    )

    # Start bot in background thread
    t_bot = threading.Thread(target=bot_thread, daemon=True, name="BotThread")
    t_bot.start()

    # Register shutdown signals (SIGINT for Ctrl+C, SIGTERM for cloud restarts)
    signal.signal(signal.SIGINT, handle_shutdown)
    signal.signal(signal.SIGTERM, handle_shutdown)

    # Run monitor on main thread
    try:
        monitor_thread()
    except Exception as e:
        log.error(f"CRITICAL ERROR in Monitor Thread: {e}")
        handle_shutdown()