A few days back, I came across the Combot Telegram bot for group chats. My close friends chat in a Telegram group and I thought why not add this bot for laughs and giggles. What I did not know was that the bot had a feature called Entertainment
which allows the group chat users to gain XP
and Reputation
based on their activity (sending chat messages) in the group. The bot had a fully functional XP
leveling up system kinda like the character leveling system found in RPG games.
What I did not realize was that Combot was a freemium bot and as part of its promotion it gives newly added chat groups a free trial of 7 days to test out its premium features.
1 week in and our group was enjoying the new XP
and Reputation
system but since the trial finished it stopped working. Disappointed my friends were discussing if someone cough cough (me) to implement a similar Telegram bot.
Previously, Kumar and I have implemented a Rise of Nations Taunt bot which has been blog posted already here
So, ensure a wild 4-hour marathon where I hack together a somewhat functioning, somewhat similar to Combot, Telegram bot.
Codename Niscoin is a Python 3 based Telegram bot using the python-telegram-bot
package.
The bot when added starts checking each text message sent by a user in that group and assigns XP between 1 - 12. To prevent XP farming I’ve added a cooling period of anywhere between 1 - 4 minutes between each successive XP granted.
Next, I implemented a feature that allows group chat users to see their XP ranking table. Turns out Telegram forbids Bot API to get the list of users in a group chat. This means my bot will not be able to know who are the users till they message at least once in the group.
With the XP system implemented, I went to implement an XP Leveling Up system. I had to figure out an XP level distribution which is simple and easy to implement. Reverse engineering the Combot XP level distribution I figured out how they implemented the default version of it.
A mathematical representation of the XP distribution.
\[f(x) = \begin{cases} x \mod 2 \neq 0 &, 2f(x-1) - f(x-2) + 35 + 100 \nonumber \\\ x \mod 2 = 0 &, 2f(x-1) - f(x-2) + 35 \end{cases} \]
After implementing it in pure Python.
|
|
I used least recently used (LRU) caching method, as the function is a recursive function, and caching it will speed up computation.
This would result in a sequence like this for the first 20 levels (including 0).
|
|
The final output was something like this.

With the XP system implemented and complete I can now move on to the Reputation System. In Combot, when uses reply to a chat message with either a +
or a -
that corresponding user would have their reputation increased or decreased.
Implementing it was not too difficult and I borrowed the code for granting XP. I also added a conditional check making sure a user cannot increase their own reputation (else we’ll have users mass replying to their own messages).
The final output was something like this.

Finally, I deployed the bot on my dedicated cloud VPS server and published the changes to GitHub. You can find my repo here. I like to thank my good friend Suchit Kar for helping me out in debugging the bot and suggesting changes.