This document explains the final, production-ready fix for the Telegram Premium Sync between the HollowScan Mobile App and the @HollowScanBot.
- What: I replaced
isoformat()(which often includes+00:00orZ) withdatetime.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 comparisonexpiry_dt > nowfails, causing the bot to think you are "Not Subscribed." - Benefit: Absolute compatibility with the bot's existing logic.
- What: I updated the dictionary update logic from
bot_users[tid] = { "expiry": ... }tobot_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.
- 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.
- What: I added logic to the
/v1/user/telegram/linkendpoint 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.
- 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...exceptblock. If anything goes wrong, it logs a clear error to your Railway dashboard but never crashes the app.
- Push the current app.py to Railway.
- In the Mobile App, go to Settings > Telegram Account.
- Tap Link Account (or "Sync Premium Status" if already linked).
- Verify your status in the Telegram bot with the
/statuscommand.