8

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

4

2 回答 2

6
  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:

于 2015-09-15T03:50:28.790 回答
0

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!')
})
于 2022-02-23T19:13:59.010 回答