Thinking in Python part 3 The bot is in production 

December 7, 2023

My previous chatbot version was quite OK for learning Python or for demonstrating possibilities of Telegram bot-building, but not satisfactory for everyday use.

Particularly, I moved all the configuration into the TOML files, but only partly the logic, so it crashed on all pieces of the non-reviewed source code. I dislike Python for this amazing possibility of making things in the worst manner ever when you have a line of code, the Linter may not give any error or even warning there, and only at the execution time an exception suddenly happens there. Thus, I had to spend in total about a week of my efforts to walk through all cases and conditions and check how it worked after refactoring.

Another moment to implement was to add a scheduling, so the bot can fully work with no human interventions, open games for registration, close when the number of participants is over, and re-open again when someone decides to leave.

Finally, the totally new one was protection from other bots, usually spamming, so new users in the chat were asked to follow a quick self-registration routine, containing several steps, fully customizable via TOML file. Until the registration is passed, the user cannot get a right to write anything into the chat. Moreover, if the registration is not finished during the specified amount of time, the user is kicked off from the chat, and the next attempt to enter is allowed only after a specified threshold. It may sound too rude, but let’s see; anyway, the auto kick-off feature is fully controllable via the TOML file as well.

Now I have a final version of the bot in my hands, and I would like to sum up what it is and how it works.

First, it reads the configuration from the chat at the launch and distinguishes Administrators and other Registered Users. For Administrators, it gives the following top-level features:

  1. Game Management
  2. Game Join

Users have:

  1. Game Join
  2. See/Update their Personal Information
  3. View Chat Rules

To make life easy, users who are in the chat by the moment of the start of the bot and have written at least one message, are treated as registered and agreed with the rules of the chat, so they are allowed to join games. New users, joining after, or those who haven’t left any messages in the chat (or possibly wrote something that later was removed), are asked to pass mandatory registration steps.

All registration and de-registration events are saved into the SQLite database. Probably I will add a “User report” feature to check user activities or registration history one day.

Several weird issues were occurring while I was working with it; I could not recognize the exact root cause, so, thinking as a beginner Python developer, I treated it as a fault caused by probably the incorrect sequence of calls, but I could not identify what exactly was wrong. The first, the weirdest, was callback actions firing by Regex match, or frankly speaking, not firing, so finally I left just one callback entry and dispatched everything within. The second was an annoying error "telegram.error.Conflict: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running", which suddenly appeared one day. Before that, I added a forced language switch, so I spent a few hours trying to find something that broke everything. But can you imagine, that the error happened exactly because the two bot instances were trying to pull updates from the chat, and the second one was my old good Raspberry with older code, that I forgot to turn off!! Finally, when I wrote automation tests around my first version of the bot, I faced another issue, raised by Telegram API, when too frequent requests were not accepted. Those were not quite comfortable aspects of bot building when you expected your code to work, but it wasn’t.

So, here is what the boot looks like now. You need to have a Telegram chat where you are admin, add the bot account there, and grant the rights to it to ban users and delete messages:

The chat should have one pinned message to be shown as a result of the /help command:

Then, when the administrator or user starts the session, the bot replies in a private chat and removes the initial “/start” command from the chat.

Let’s start with the administrative UI:

Here is how the entry of custom date-time for the competition looks like:

The registration can be opened automatically by the scheduler or by the button:

UI is multi-languaged and auto-translated, so here is how it looks in Ukrainian:

Or in original English:

Registration events are translated to the chat. Older notification messages about the same event are auto-cleaned.

Now about the user registration. They can use polls for every game open:

Or type “+X” or “-X” messages from the keyboard, but only there is just one game open for registration.

Or they can use /start command of the bot:

If they are registered already, they will be offered the unregister option:

Users see the summary about all confirmed or open for registration competitions:

..and they can join open for registration ones:

The most interesting moment is about chat registration. Users can see and change their registration information, and have to enter it at the beginning to be allowed to write anything into chat and join the games.

Here are table tennis registration steps:

..etc. You can have whatever you want, in any number of steps.

At the end you have to confirm the rules, and are allowed to join the club games.

There are probably a few more screens that you can see in the bot, but hope you got the idea of configuring everything via config files, having initial messages on English, so you are flexible in translations to any language you want, and rely the translation duties to AI-powered services on a free tier.

You are welcome to use this bot in your everyday live, and ask me to help to customize it for your needs. Or even probably write another one. Or even not a Telegram Bot. Or even not on Python 🙂

https://github.com/XCoReD/SportCompetitionBot