How to require joining chats?ΒΆ
Do you want to require users to join channels or groups? In this guide, we'll explain how to do it, step by step.
For this guide, we'll require users to join 2 channels when they start the bot, i.e., when they send /start
. However, you can use this feature anywhere in the bot, not just in /start
.
We'll show a message to users when they start the bot, telling them they must join the channels, along with a button they must press to verify that they have joined. The final result will look like this:
Here's how it works: the message will be shown to the user whenever they send /start
. When they click the β
Verify β
button, the bot will check if the user is joined to the channels. If not, the message will be shown again, and this process will continue until the user joins. Once joined, the main menu of the bot will be displayed.
Let's go step by step:ΒΆ
- First, add your bot as an administrator to the channels you want to require users to join.
- Now, enter the editor mode of your bot by clicking the
π Edit
button, and send/start
to edit the command. Then click㪠Message
and add the message that will be shown in the main menu of the bot (if you don't have one yet). For example: "Welcome to the bot." This will help illustrate this guide better. It's also recommended to have at least one button in the bot. - While editing
/start
, click onActions π
. -
Add an action of
Send message
, click onConfigure π
, and save the message that will ask users to join the channels. For example, I've saved the following message: -
Then, within the same action, click on
π Deploy keyboard π
, then onπ Create new keyboard π
, selectπ· Normal
, and send the name of the button to be created, in this case it will beβ Verify β
. This will display the button when sending the message. - Next, add a
Wait for answer
action, so the bot waits for the user to press the button, as the button is a message. You don't need to configure the action, just activate it. -
Then add a
Chat member
action and configure it. Click on theπ Set chats π
button and send the IDs or @usernames of your channels to verify, separated by a comma. I'll send the @usernames, which are@channel_of_cats, @channel_of_dogs
.As you can see, each chat has been assigned a number, according to the order in which it was saved. It's important to remember this for later.
-
Go back to your list of actions and add a
Add condition
action, configure it. Click onπ New condition π
, here we must enter the expression to evaluate, save the following:{is_inside(#CmfwA.chat1.status#, ("member", "administrator", "owner")) and is_inside(#CmfwA.chat2.status#, ("member", "administrator", "owner"))}
Replace
CmfwA
with the ID of yourChat member
action added in step 6, in my case it'sCmfwA
, but in yours it'll be different. Notice that it must be replaced twice in the expression.Now let's analyze the expression in detail:
- First, all expressions are inside braces
{ }
. - We've also used the is_inside() function, which checks if the first argument is inside the second argument.
-
To understand better, let's analyze the first use of is_inside():
is_inside(#CmfwA.chat1.status#, ("member", "administrator", "owner"))
-
The first argument would be
#CmfwA.chat1.status#
. - And the second argument would be
("member", "administrator", "owner")
, as you can see, it's enclosed in parentheses, meaning it's a tuple. - What the function does is check if what's passed in the first argument is inside the elements of the second argument.
chat1
refers to the first saved channel, remember that we told you to remember it before. In my case, it's@channel_of_cats
, as you can see in the image above.- Also, remember that
status
returns the user's status in the chat, which can bemember
,administrator
,owner
,left
,banned
,restricted
. You can see all of this in more detail in the article dedicated solely to the Chat member action. - Therefore, this first part of the expression can be translated as:
If the current user's status in the @channel_of_cats channel is a member, administrator, or owner, the condition is true; otherwise, it's false.
- In the second part of the expression, we see almost exactly the same as the first part, with the only difference being that instead of
chat1
, it's nowchat2
, meaning that the second saved channel is being evaluated, which in my case is@channel_of_dogs
, as you can see in the image above. - Both evaluations are separated by
and
, which means that the condition will only be true if both evaluations are true, in other words: the condition will only be true if the user is amember
,administrator
, orowner
of both channels.
- First, all expressions are inside braces
-
Once the expression is saved and understood, we only need to specify what will happen if the condition is true and if it's false.
- Click on the
π If true
, then clickβ Continue
. This tells it to continue if the condition is true (if the user is joined to both channels), which in this case, with no more actions, means it will go to the main menu of the bot. -
Click on
If false π
, then clickπ Execute action by ID π
, copy the ID of theSend message
action from step 4, and paste it here, make sure it's selectedβ‘οΈ After action: continue β©
.In my case, it looks like this:
What have we done here? Firstly, we've told it that if the condition is false, i.e., if the user hasn't joined both channels, execute the
Send message
action that shows the message asking the user to join the channels, and then continue with the rest of the actions that follow. Thus, it continues in a loop that only exits when the user joins the channels. -
Don't forget to activate all the actions.
And that's it! With this, you'll require your users to join your channels before they can use the bot.
My actions look like this:
It's important to mention that all of this is fully customizable to your liking. For example, in step 11, instead of executing the Send message
action that displays the message asking the user to join the channels, you could execute another Send message
action, but isolated by a separator, which would send a message telling the user that they haven't joined, and from there, you could create a Jump to action action to return to the Chat member
action and verify again if the user has joined.
You could also add a Add condition
action at the beginning to check if the user is new to the bot through their primitive variable is_new
, and if they are new, require them to join the channels, but if they aren't new, send them directly to the main menu.
Another thing you could do is create the β
Verify β
button as a inline keyboard, although that is a bit more complex.
The possibilities are endless, and it's up to you, as a bot creator, to play with all the power that VisualMaker
puts in your hands.