<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ANIS</title>
    <description>The latest articles on DEV Community by ANIS (@assinscreedfc).</description>
    <link>https://dev.to/assinscreedfc</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2011629%2Fe9ff6c74-6a67-4fdf-8cb3-6e334f8df6f7.png</url>
      <title>DEV Community: ANIS</title>
      <link>https://dev.to/assinscreedfc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/assinscreedfc"/>
    <language>en</language>
    <item>
      <title>How I monitor my Telegram userbots with zero infra</title>
      <dc:creator>ANIS</dc:creator>
      <pubDate>Tue, 09 Jun 2026 16:58:16 +0000</pubDate>
      <link>https://dev.to/assinscreedfc/how-i-monitor-my-telegram-userbots-with-zero-infra-34i9</link>
      <guid>https://dev.to/assinscreedfc/how-i-monitor-my-telegram-userbots-with-zero-infra-34i9</guid>
      <description>&lt;p&gt;I run a few Telegram bots and userbots in production. A couple of aiogram bots, a couple of Telethon userbots doing scraping work. For months I kept hitting the same two failures.&lt;br&gt;
First: a bot dies and I don't notice. The process crashes or hangs, and I find out hours later when someone tells me it stopped answering.&lt;br&gt;
Second, and worse: a userbot account gets restricted or banned. Telegram throttles it, or kills the session, and by the time I look the damage is done.&lt;br&gt;
The obvious answer is monitoring. But every option felt wrong for the size of the problem. Prometheus plus Grafana for two bots is a lot of moving parts to babysit. Hosted observability wants a server, an agent, a dashboard. I just wanted a Telegram message when something broke.&lt;br&gt;
So I built tgwatch.&lt;/p&gt;

&lt;p&gt;What it does&lt;br&gt;
You attach it to your bot or userbot in two lines. After that, capture is automatic:&lt;/p&gt;

&lt;p&gt;alive or dead state, based on a heartbeat&lt;br&gt;
message, error, and FloodWait counters&lt;br&gt;
userbot account health: restricted, banned, or dead session&lt;/p&gt;

&lt;p&gt;When something degrades, a separate watchdog process sends you a Telegram alert.&lt;br&gt;
from tgwatch import Watch&lt;/p&gt;

&lt;p&gt;watch = Watch(&lt;br&gt;
    storage="tgwatch.db",&lt;br&gt;
    alert_bot_token="123456:abc...",   # a Telegram bot dedicated to alerts&lt;br&gt;
    alert_chat_id="@my_alerts",&lt;br&gt;
)&lt;/p&gt;

&lt;h1&gt;
  
  
  Telethon userbot
&lt;/h1&gt;

&lt;p&gt;watch.attach(telethon_client, kind="userbot", name="scraper")&lt;/p&gt;

&lt;h1&gt;
  
  
  aiogram bot
&lt;/h1&gt;

&lt;p&gt;watch.attach(dispatcher, kind="bot", name="support_bot")&lt;br&gt;
After attach, you write no more monitoring code. Your bot behaves exactly as before. Exceptions are recorded, then re-raised, so nothing changes about how your code runs.&lt;/p&gt;

&lt;p&gt;The part I couldn't find elsewhere&lt;br&gt;
Knowing a process is up is easy. Knowing a userbot account is healthy is not.&lt;br&gt;
A Telethon userbot can be technically running while its account is restricted or already banned. The process looks fine. The account is dead.&lt;br&gt;
tgwatch classifies account state from two sources.&lt;br&gt;
Passive layer: it reads the Telethon exceptions your code already throws.&lt;/p&gt;

&lt;p&gt;ExceptionState&lt;br&gt;
PeerFloodErrorrestricted&lt;br&gt;
PhoneNumberBannedError, UserDeactivatedBanErrorbanned&lt;br&gt;
AuthKeyUnregisteredErrordead session&lt;br&gt;
FloodWaitErrorfloodwait event (slowed down, not a degraded state by itself)&lt;/p&gt;

&lt;p&gt;Active layer: a periodic get_me() probe that reads the account's restricted flag.&lt;br&gt;
The two signals merge with a worst-state-wins rule: banned beats dead session beats restricted beats healthy. A "banned" signal is never overwritten by a later unconfirmed "healthy". So once tgwatch sees a ban, it stays loud.&lt;/p&gt;

&lt;p&gt;Why a separate process&lt;br&gt;
A dead process can't alert you about itself. That sounds obvious, and it's the reason the watchdog runs on its own.&lt;br&gt;
tgwatch watch --storage tgwatch.db --token 123456:abc... --chat-id @my_alerts&lt;br&gt;
The watchdog reads the shared SQLite file, detects degraded states and missed heartbeats, and sends the alert. Your bot writes state into SQLite. The watchdog reads it. If the bot dies, the watchdog is still there to notice.&lt;br&gt;
You can also check state by hand:&lt;br&gt;
tgwatch status --storage tgwatch.db&lt;br&gt;
NAME          KIND     STATUS  HEALTH      MSG   ERROR  FLOODWAIT&lt;br&gt;
scraper       userbot  up      restricted  842   3      1&lt;br&gt;
support_bot   bot      down    n/a         1503  12     0&lt;/p&gt;

&lt;p&gt;Design choices I stand by&lt;br&gt;
Zero runtime dependencies in the base install. The core uses only the standard library: sqlite3, urllib, argparse. aiogram and telethon are optional extras, imported lazily. If you only run bots you never install Telethon, and the reverse.&lt;br&gt;
One direction of dependencies. There's a core layer that knows nothing about Telegram. Adapters depend on core, never the other way. SQLite runs in WAL mode, so the bot process and the watchdog process share the file safely.&lt;br&gt;
Secrets never touch the database or the logs. Bot token, session string, phone number: all masked at the recorder, with sensitive keys redacted and a regex for bot-token shapes. I didn't want a monitoring tool to become the thing that leaks the credentials it's supposed to protect.&lt;br&gt;
Alerting fails soft. Sending an alert retries with backoff, then logs and gives up. It never raises into the bot you're monitoring. A monitoring tool that crashes your bot is worse than no monitoring.&lt;br&gt;
The whole thing has 241 tests at 96% coverage and runs fully offline.&lt;/p&gt;

&lt;p&gt;Where it's at&lt;br&gt;
tgwatch is at 0.1.0. It does what I needed, and I'm running it on my own bots now.&lt;br&gt;
pip install "tgwatch[telethon,aiogram]"&lt;br&gt;
Repo: &lt;a href="https://github.com/assinscreedFC/tgwatch" rel="noopener noreferrer"&gt;https://github.com/assinscreedFC/tgwatch&lt;/a&gt;&lt;br&gt;
What I'd like feedback on: the account-health signals. Telethon throws a lot of exceptions, and I mapped the ones that bit me. If you've seen account states I'm not catching, tell me.&lt;/p&gt;

</description>
      <category>python</category>
      <category>telegram</category>
      <category>opensource</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
