AWS Builder Center

Building TeleUserBot: a small Telegram userbot with Telethon and SQLite

I built TeleUserBot to automate small Telegram tasks for myself and keep its state in a simple SQLite database, and this is what I learned along the way.

I started TeleUserBot for a pretty boring reason. I kept doing the same small jobs on Telegram by hand, checking chats, saving messages I wanted to come back to, and sending the same kind of replies again and again. It was not a big problem, but it added up, and I wanted a tool that worked the way I actually use Telegram. So I built a userbot, which runs as my own account and automates things I would normally do myself. That choice shaped the rest of the project, because it meant I had to be careful about what it touched and how often it acted.
I picked Telethon because it is a Python library for the Telegram API and it felt close to how Telegram actually works. I already knew Python from my other projects, so I did not want to learn a new language just to get started. Telethon lets me listen for new messages, reply to them, and run a small handler when something matches a pattern I care about. The first version was just one script and a few handlers, and that was enough to prove the idea. I could send a command from my own account and watch the bot pick it up and do the job.
SQLite came in because I needed somewhere to keep state without setting up a server. The bot needs to remember things between restarts, like which chats it has seen, which messages it already handled, and a few settings I change as I go. A small SQLite file does that job, and I can open it and look at the rows when something looks wrong. I kept the tables simple on purpose, with one table for the state the bot needs to resume cleanly and a few columns that say what happened and when.
The pieces fit together in a simple way. Telethon watches for events and calls the right handler, the handler does its work, and then it writes what it did to SQLite before it moves on. That order matters. If the bot crashes or my internet drops, it can start again and check the database instead of doing the same job twice or losing track of where it was. I also keep the handlers short. Each one does one job, like saving a message or sending a reply, and then it gets out of the way.
The biggest lesson was about flaky connections. My internet is not perfect, and Telegram will drop a session at the worst time if I let it. At first I tried to handle every error in the same place, and it just made the code harder to read. What worked better was keeping the retry logic small and clear, logging what happened, and letting the bot reconnect and pick up from the database. I also learned to slow down. Telegram has limits on how fast an account can act, and a userbot that fires off messages too quickly can get throttled. Small pauses helped more than any clever code did.
Keeping state simple was the other lesson that stuck. Early on I wanted to store everything, just in case I needed it later. That made the database messy and made it harder to tell what the bot actually depended on. Now I only store what the bot needs to resume or to answer a real question. It is easier to debug, easier to back up, and easier to explain to someone else. I also keep secrets out of the database and the code, and use environment variables for keys and session details instead.
If I rebuilt it today, I would split the code into smaller modules earlier, add better logging from the start, and write a few small tests for the SQLite parts, because a bad query there can quietly break the whole flow. I would also add a simple queue for jobs that do not need to happen right away. It is still a small project, but it taught me more about async code and storing state than a tutorial did. If you are a student thinking about building something like this, start with one small job you do every day. What would you automate first?
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article