Learn more about the telegram bots
What are bots on telegram?
We are extremely happy you’ve asked us about this. The topic of telegram bots can be really complicated for the non-technical audience but we are here to help and answer all your questions. Bots are defined by Telegram team as third-party applications running within telegram. Users can interact with them by either sending messages directly to them ( for example by sending commands ) or by allowing them to do what they were designed to do - like in our case - fighting the spam in messages on the groups. Bots connect to Telegram Bot API using a secure SSL protocol, therefore nobody observing the traffic can snoop on it and any conversation in your group stays between the participants and the bot itself.
How do bots work, exactly?
We will try to explain it in our bot’s example, which is way more advanced than most of the bots you can see on telegram daily. Our bot listens on a special endpoint which is known only to the owner and Telegram Bot API team. Whenever there’s a message sent on one of the groups bot is on - the Telegram itself sends the message to the mentioned endpoint and that’s where the whole magic starts.
Main bot microservice checks if the message was sent in one of the known groups and by the known user - if not - it will fill up a new entry in the database to store your group settings and user profile. Then message itself is converted into our internal format with those IDs attached and passed to the logging microservice which stores the message temporarily ( up to 24 hours ) in our database. After this simple task, the internal structure is extended with an “internal message ID” identifier, allowing the rest of the microservices to refer to it quickly and without expensive database queries.
Logging microservice then sends the message into the Queue system which distributes it to all the microservices for further checks. For example, the language microservice will check if your group settings have the “language guard” set on and which language you have picked for your group, then it will verify that message we have just received is in this ( or English ) language. If not - there’s a bit of punishment waiting for the user. All the microservices report their results into the “decision microservice” which gathers them as they arrive and does all the calculations four times every second. If any message was identified as inappropriate - the “decision microservice” will issue a punishment through “action microservice”, which then tells the “notifications microservice” to send the message to the group telling everyone about the punishment.
That sounds so complicated!
You are absolutely right - it is a really complicated procedure, but we made it this way. There are simple bots which do not do all the magic behind the scenes, but they simply can’t adjust to the massive traffic which we sometimes see. We know - during many of the real-life tests that our bot can easily work with tens of thousands of incoming messages per second without any signs of slowing down or skipping the messages that it’s supposed to filter. Thanks to the fact that all microservices run in Kubernetes cluster on many servers and there are at least two copies of each at the same time makes the bot not only fast but reliable. If one of the servers goes down for any reason - the microservices which run on it will be automatically moved to the other one. No messages should be ignored and no spammer should ever go unpunished.