Skip to content

Latest commit

 

History

History
39 lines (29 loc) · 3.42 KB

File metadata and controls

39 lines (29 loc) · 3.42 KB

Walkthrough: Telegram Premium Sync Fix (PRO)

This document explains the final, production-ready fix for the Telegram Premium Sync between the HollowScan Mobile App and the @HollowScanBot.

1. Technical Changes in app.py

Naive UTC Timestamps (Bot Compatibility)

  • What: I replaced isoformat() (which often includes +00:00 or Z) with datetime.now(timezone.utc).replace(tzinfo=None).isoformat().
  • Why: The Telegram bot's database (bot_users.json) uses "naive" ISO strings without timezone offsets. If the offset is present, the bot's comparison expiry_dt > now fails, causing the bot to think you are "Not Subscribed."
  • Benefit: Absolute compatibility with the bot's existing logic.

Metadata Preservation (**bot_users.get)

  • What: I updated the dictionary update logic from bot_users[tid] = { "expiry": ... } to bot_users[tid] = { **bot_users.get(tid, {}), "expiry": ... }.
  • Why: The old code would have wiped out the user's Telegram username or their alert filter categories when updating their premium status.
  • Benefit: No data loss. The user's bot settings remain perfectly intact.

Source-Aware Analytics

  • What: The backend now passes the actual payment source ("google", "apple", or "mobile_app") to the bot's data file.
  • Why: You mentioned that a user could pay via Apple Pay or Google Play. Using a hardcoded "source": "google_play" for everyone was misleading.
  • Benefit: Better analytics in the bot's dashboard and status messages.

Proactive Sync on Link

  • What: I added logic to the /v1/user/telegram/link endpoint to check if the app user is already premium.
  • Why: Previously, syncing only happened from the Bot -> App. Now, if a subscriber links their Telegram, the App -> Bot sync triggers immediately.
  • Benefit: 100% bi-directional sync. It works perfectly whether you pay in the app or the bot.

2. Safety Assurance (100% Reliable)

  • Background Tasks: The bot-syncing logic runs via background_tasks.add_task(...). This means even if the Supabase write fails or is slow, it will not slow down the app or interfere with the user's purchase verification.
  • Isolation: These changes are strictly within the update_bot_user_premium and sync_google_premium_to_telegram functions. They do not modify your main SQL tables (users, messages, etc.) or the authentication flow.
  • Error Handling: Every bot-sync operation is wrapped in a try...except block. If anything goes wrong, it logs a clear error to your Railway dashboard but never crashes the app.

3. How to Deploy (USER ACTION)

  1. Push the current app.py to Railway.
  2. In the Mobile App, go to Settings > Telegram Account.
  3. Tap Link Account (or "Sync Premium Status" if already linked).
  4. Verify your status in the Telegram bot with the /status command.