How to hide Telegram BOT commands when it is part of a group?
Asked Answered
H

3

10

I'm trying to use a Telegram BOT to send messages to a group. First, I thought that it'd be enough to know the group chat id to accomplish that, but it's not. The BOT MUST be part of that group. OK, it kind of make sense, but the problem is: When you add a BOT into a group (a large group in this case) everyone start seeing a new icon on their devices, a "slash" icon. And what do they do ? They click on it, see the list of commands, choose one of them, and all of a sudden everyone is getting a new message from the group: a "/something". Imagine dozens of people doing that ? It's pretty annoying. So, any of these would work for me:

1) Can I send messages from a BOT to a group without having that BOT in the group ? 2) Can I have a kind of "no methods" BOT, that only send messages ? 3) Can I disable "slash" icon from clients so I won't have a "bot method war" in the group ?

Thank you

Haldis answered 14/9, 2015 at 18:55 Comment(0)
J
7
  1. No, you cannot have bots send messages to a group without being a part of that group.
  2. You can simply not set commands with BotFather, and then clients will have no commands to display.
  3. It is always there if a bot is in the current chat, but here is what it does with no commands set in BotFather:

Jeffry answered 15/9, 2015 at 3:50 Comment(1)
Yeah. Yesterday a figured out that a "no methods" BOT could do the trick. So I created one and it worked !! PS:Using the latest Android App version, I can't even see the "slash". Perfect ! Thank you very much for your answer.Haldis
S
2

I got a much better solution: there is the possibility to customize the commands directly by code, also depending from the context (i.e. private chat, groups, etc...)

This example was done by using Telegraf but that's not so different from basic code

bot.start(function(ctx) {
    // If bot is used outside a group
    ctx.telegram.setMyCommands(
        [
            {
                "command": "mycommand",
                "description": "Do something in private messages"
            }, {
                "command": "help",
                "description": "Help me! :)"
            }
        ],
        {scope: {type: 'default'}}
    )

    // If bot is used inside a group
    ctx.telegram.setMyCommands(
        [
            // <-- empty commands list
        ],
        {scope: {type: 'all_group_chats'}}
    )

    ctx.reply('Hello! I\'m your super-cool bot!!!')
})

Bonus Point, you can also manage the command behavior by checking the source. So, for example, if a user in a group still try to use manually your command and you don't want to execute anything:

bot.help(function(ctx) {
    // Check if /help command is not triggered by a private chat (like a group or a supergroup) and do nothing in that case
    if (ctx.update.message.chat.type !== 'private') {
        return false
    }

    ctx.reply('Hi! This is a help message and glad you are not writing from a group!')
})
Syncope answered 23/2, 2022 at 19:13 Comment(0)
U
0

You can set a scope in which commands are available. Set it to all_private_chats and commands will be visible only in private chats.

https://core.telegram.org/bots/api#setmycommands

https://core.telegram.org/bots/api#botcommandscope

Unilobed answered 29/8 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.