In a previous tutorial, we saw how to create and deploy a Telegram chatbot using Xenioo.
While we did cover the basics of single and group conversations and the concept of bot privacy, we didn’t go into the depths on how to fully manage a Telegram group chatbot.
This time we are going to see exactly this: we will create a very simple chatbot that will act as a group manager and moderator.
Telegram Group Chatbot Privacy Mode
When building you chatbot using the BotFather, you will notice multiple configuration options where you can change the name, the avatar and some other settings.
One of these options is /setprivacy and can be used to turn on or off “Group Privacy”. What is it exactly?
- With Bot Privacy OFF, your chatbot will receive every message that is sent to the chat by any user. If you have a hundred thousands users in your group, Xenioo will receive all of the messages and process them.
- With Bot Privacy ON, none of the messages sent by any user will reach Xenioo. Only messages starting with the symbol ‘/’, or direct mentions of your chatbot, will be received.
By default, Bot Privacy is ON and for our example, we will leave it as it is.
As we said, we are going to create a Group Manager and we do not want, for now, to check all users messages.
Reacting to Chat Commands
As explained, with group privacy turned on, we will be receiving only chat messages starting with a ‘/’. A typical use of these commands are /help or /about but you can just handle whatever command you like.
Using Xenioo global bot operations you can handle these commands very easily and make sure that they are always triggered whatever the status of your chatbot.
In our chatbot sample, the /hep command will reply with a very simple bubble proposing two different commands.
Whatever text starts with a ‘/ ‘ symbol inside a Xenioo text will be transformed into a link command inside the Telegram Bubble.
The user can write the command or just click on the command link inside the bubble to invoke your chatbot again.
Users coming and going. Manage them with your Telegram Group Chatbot
As soon as you add your Xenioo chatbot to a Telegram group you will start receiving events related to every user joining and leaving.
Each event will trigger chatbot populating specific variables you can use in your flow to react and eventually welcome.
|telegram_new_users_count||Contains the number of new users added. If the value is equal to zero, no user has been added.|
|telegram_new_user_<num>||Contains the full username of one of the users that just entered the group where <num> represents the index number of the user.|
|telegram_new_user_<num>_type||Contains the type of user that just joined the group where <num> represents the index number of the user. The value can be either USER for a normal user or BOT for a bot.|
|telegram_user_leaving||Contains the full username of the user that just left the group.|
|last_user||Contains the full username of the last user that sent a text or a command in the group |
Users can be added massively with a single invite so make sure to always check for the correct number indicated by the telegram_new_user_count variable.
On big groups, it is not uncommon to have tens of users joining altogether. Using Xenioo variable manipulation capabilities you can create a simple loop very easily.
On the other hand, users leaving can be multiple but never massive as removal is immediate and never simultaneous. Variable telegram_user_leaving value is always related to a single user and your flow will be called for each user.
Overseeing users behavior
Another important aspect of managing groups is overseeing users behavior.
If your group runs with specific policy, for example, a chatbot can be a very effective way of enforcing it. Xenioo gives not only the tools to detect user text but also to kick them from the group.
The first thing to do is turn off group privacy on your bot: this will allow Xenioo to receive and process each and every message that goes in the group.
After this, you can easily create an NLP intent containing all of the expressions that you do not want your users to use.
The expressions can vary from simple, even single word entries to more complex sentences covering different topics. The Xenioo NLP engine will automatically train itself and detect also texts that are different but express the same intent.
Once you’re satisfied with your intent, we can link it to our global chatbot operations and attach it to an interaction.
As you can see from the diagram our approach is to give the user up to 3 warnings before taking on final action.
User warning count has its own variable and chatbot will be able to take note of count even after months. You can go ahead and reset counter once in a while using just a broadcast.
Having a high number of users may require a more complex and database oriented approach but for our example, it is going to be just fine.
Your Personal Group Manager
We’ve just seen how building a Telegram Group Manager bot with Xenioo is incredibly easy and flexible.
Want to try it right away? There’s a free template built exactly like this example and ready to be used for your project in your account.