<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Jan.l Dev Notes]]></title><description><![CDATA[Jan.l Dev Notes]]></description><link>https://adonisleung.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 29 Nov 2026 01:59:51 GMT</lastBuildDate><atom:link href="https://adonisleung.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[A production-shaped Telegram bot in ~500 lines of Python — no frameworks]]></title><description><![CDATA[Long polling, SQLite logging, commands and inline callbacks - a complete Telegram bot in plain Python, no frameworks, plus deploy patterns.]]></description><link>https://adonisleung.hashnode.dev/a-production-shaped-telegram-bot-in-500-lines-of-python-no-frameworks</link><guid isPermaLink="true">https://adonisleung.hashnode.dev/a-production-shaped-telegram-bot-in-500-lines-of-python-no-frameworks</guid><category><![CDATA[telegram]]></category><dc:creator><![CDATA[Jan.l]]></dc:creator><pubDate>Sat, 14 Nov 2026 02:13:06 GMT</pubDate><content:encoded><![CDATA[<p>Most Telegram bot tutorials hand you 40 lines that echo messages back. Most bot frameworks hand you 200 dependencies and an upgrade treadmill.</p>
<p>I wanted the middle path: a bot small enough to read in one sitting, but structured like something you could actually deploy. Here's the architecture that fit in ~500 lines using nothing but <code>requests</code> and SQLite.</p>
<h3>The 4-module layout</h3>
<pre><code class="language-plaintext">bot.py            long-polling loop + routing
telegram_api.py   minimal HTTP client
handlers.py       command registry + callback registry
storage.py        SQLite (WAL) users + message log
</code></pre>
<p><strong>The key decision is the registries.</strong> Commands are declared with a decorator and aliases come free:</p>
<pre><code class="language-python">@command("start", aliases=("hello", "hi"))
def cmd_start(api, store, chat_id, first_name=None, **_):
    store.upsert_user(chat_id, first_name=first_name)
    api.send_message(chat_id, f"Hi {first_name or 'there'}!")
</code></pre>
<p>Adding <code>/help</code> costs zero extra code because help text is generated by iterating the registry. Same pattern for inline-button callbacks:</p>
<pre><code class="language-python">@callback("confirm:")
def cb_confirm(api, store, chat_id, callback_query_id=None, data=None, **_):
    api.answer_callback_query(callback_query_id, text="Confirmed")
</code></pre>
<h3>Long polling details that bite in production</h3>
<ol>
<li><strong>Offset tracking</strong> — every update you process, remember <code>update_id + 1</code> and send it back as <code>offset</code>. Forget this and Telegram replays history.</li>
<li><strong>409 Conflict</strong> — two processes polling with one token. Detect it and exit with a clear message; users always think the bot is "broken".</li>
<li><strong>Timeouts</strong> — <code>getUpdates</code> with <code>timeout=25</code> is fine, but your HTTP client timeout must be <em>longer</em> than the poll timeout, or you'll race your own long polls.</li>
<li><strong>Per-chat echo state</strong> — a plain dict on the handler function is fine for one process; anything more goes into SQLite.</li>
</ol>
<h3>SQLite specifics</h3>
<ul>
<li>Enable WAL: <code>PRAGMA journal_mode=WAL</code> — you can inspect the DB while the bot runs.</li>
<li><code>INSERT ... ON CONFLICT(chat_id) DO UPDATE</code> for the users table: one round trip per message, no read-then-write race.</li>
<li><code>check_same_thread=False</code> is safe here <em>because</em> the bot is single-threaded.</li>
</ul>
<h3>Deploy in 10 minutes</h3>
<p>Long polling needs no public endpoint — any $4 VPS works:</p>
<pre><code class="language-ini"># /etc/systemd/system/bot.service
[Service]
ExecStart=/opt/bot/kit/.venv/bin/python bot.py
Restart=on-failure
</code></pre>
<p><code>systemctl enable --now bot</code>, done. Docker-compose file for the same layout is 6 lines (<code>build: .</code>, <code>env_file: .env</code>, a volume for the database).</p>
<h3>Wrapping up</h3>
<p>The full kit (commands, reply+inline keyboards, confirm/cancel callback pattern, per-chat echo mode, Dockerfile, systemd unit, and a <code>--check</code> self-test mode) is packaged here: <a href="https://adonisflare56.gumroad.com/l/nrzbdk"><strong>Telegram Bot Starter Kit</strong></a> — $12, single-user license, extend it freely in your own projects.</p>
<p>If you want to see the scraping side of the same "small but production-shaped" philosophy, that's here: <a href="https://adonisflare56.gumroad.com/l/sucvp"><strong>Scraper Recipe Book</strong></a>.</p>
<p>Happy to answer questions about the architecture in the comments.</p>
]]></content:encoded></item></channel></rss>