Telegram is my favourite messenger application. It is feature rich, customizable, and has bots. Bots can be added to groups or can be used with one-to-one messages depending on the bot’s functionality. Telegram bots have certain commands, which can be sent over text messages, triggered by a
/. The bot will then respond to your command. There are quite a few Telegram bots available, some very useful, some not so much, but very interesting nonetheless.
A few months back I started exploring Telegram bots and how to build one. Since my primary language for coding is Python, I was looking to build a bot using Python. Luckily, I found a library to ease the process of building a Telegram bot.
A Telegram bot is basically just an account which runs independently. To create a bot, you first need to register a bot using the @BotFather bot on Telegram. You’ll get a tocken. You can also manage the bot settings using BotFather. Once registered, you essentially need to listen for new messages sent to the bot, then respond according to the commands.
While I have experimented with a few bots, deploying a bot with a proper project structure is not as straightforward as running the bot in Jupyter Notebook. So the first bot I have deployed is a very simple bot called Random GRE Word Bot. This bot helps you learn a new word. To use this bot, visit @random_gre_word_bot in Telegram to chat with the bot. Send /word to get a random word with definition.
On fetching a word, you’ll also get two buttons to fetch its pronunciation and synonyms. You can click these buttons to get the pronunciation and synonyms as new messages.
The source code for the bot is available at https://git.gvkr.in/gourav/telegram-wordbot, which is my self-hosted git server.
The bot is deployed using a Docker container. Docker eases the process of deployment. It ensures that the container is always running. The Python script starts automatically with the Docker container, which keeps the bot available at all times.