Brad Butner
All posts

Adaptive Telegram Polling with Perplexity Computer

Building an always-on AI assistant that speeds up when you need it and conserves resources when you don't.

The Problem

I wanted an AI assistant I could talk to on Telegram at any time of day — one that would check for my messages, think about them, and reply thoughtfully. Perplexity Computer has all the capabilities to make this work: it can call APIs, manage files, and run scheduled tasks. But there's a catch — its minimum scheduling frequency is one hour.

Waiting up to 60 minutes for a reply during an active conversation? That's not a conversation. That's email. I needed something smarter.

The Solution: Adaptive Acceleration

The system I built starts with a simple hourly polling schedule, then dynamically accelerates when it detects I'm actively chatting. The key insight is a technique I call the minute-shift trick: instead of changing the polling frequency (which isn't possible), you change which minute of the hour the task fires on.

A scheduled task set to fire at :00 every hour can be shifted to :05, then :10, then :15 — creating a chain of checks that are only 5 minutes apart, all while technically maintaining an hourly schedule.

How It Works

The Tier System

The system uses a 5-tier acceleration model tracked in a simple state file:

Tier State Behavior Next Check
0IdleChecks every hour on :0060 min
1ActiveNew message found — accelerate5 min
2CoolingNo new message — still checking5 min
3CoolingStill nothing — keep checking5 min
4CoolingFinal cooldown check15 min
ResetIdleNo activity — back to baseline60 min

If I send a new message at any tier during cooldown, the system immediately jumps back to Tier 1 and resumes 5-minute checks. It's like a conversation partner who stays alert while you're talking and gradually relaxes when you go quiet.

The Minute-Shift Trick

Here's a walkthrough of the system in action. Say the baseline fires at :00 each hour, and I send a message at 2:03 PM:

Time What Happens Cron After
2:00 PMScheduled check finds my 2:03 PM message. Replies. Sets tier 1.:05
2:05 PMCheck fires. I replied again. Tier stays at 1.:10
2:10 PMNo new message. Tier 1 → 2.:15
2:15 PMNo new message. Tier 2 → 3.:20
2:20 PMNo new message. Tier 3 → 4.:35
2:35 PMNo new message. Tier 4 → reset to 0.:00
3:00 PMBack to normal hourly baseline.:00

The cron expression is always hourly. The only value that changes is the minute number. Each escalation calculates the current UTC time, adds 5 (or 15) minutes, and updates the cron to fire at that new minute mark.

The Escalation Handoff

There's one important architectural detail: background scheduled tasks in Perplexity Computer can't modify their own schedule. So the background task escalates to the main conversation with a message like:

"TELEGRAM SCHEDULE UPDATE: New message detected and replied. acceleration_tier=1. Please update cron to run 5 minutes from now."

The main conversation receives this, calculates the correct UTC minute, and updates the scheduled task. This adds a small delay (usually under a minute) but effectively achieves 5-minute polling during active conversations.

The Architecture

The complete message lifecycle flows through 9 steps:

  1. You send a message — your Telegram app delivers it to your bot
  2. Scheduled task fires — Computer's cron triggers at the scheduled minute
  3. Read state file — the task reads telegram_state.json for tracking data
  4. Fetch new updates — calls getUpdates with the last processed offset
  5. Reply on Telegram — composes and sends replies through the bot
  6. Update state file — writes new tracking data and sets acceleration tier
  7. Escalate for schedule change — sends escalation with new timing request
  8. Main conversation updates cron — calculates UTC minute and updates the schedule
  9. Next check at new minute mark — system loops back, now at :05 instead of :00

Key Gotchas

  • Send /start first — you must message /start to your bot before Computer can reach you. It's a Telegram requirement for all bots.
  • Usernames vs. numeric chat IDs — the @username format often doesn't work for sending messages. Computer discovers your numeric chat ID automatically.
  • Duplicate prevention — the state file tracks last_processed_update_id as the single source of truth. No duplicates, ever.
  • Edited messages — Telegram sends these as separate edited_message updates. The system ignores them to avoid re-processing.
  • Timing nuance — if the calculated minute has already passed for the current hour, the cron fires at that minute in the next hour. Computer handles this math automatically.

The Result

What started as a one-hour polling loop became a responsive AI assistant that adapts to my behavior in real time. During active conversations, I get replies within 5-7 minutes. When I go quiet, the system gracefully winds down to conserve tokens. It's the best of both worlds — always available, never wasteful.

The full guide with step-by-step setup instructions, exact prompts to give Computer, and all the technical details is available as a PDF below.

Download the Full Guide

Adaptive Telegram Polling with Perplexity Computer — 13-page PDF with step-by-step instructions, prompts, architecture diagrams, and troubleshooting tips.

Download PDF