42

I want to write a bot telegram.How to put possible option in my bot.I insert a picture of sample bot with this functionality.

enter image description here

maryam
  • 1,437
  • 2
  • 18
  • 40

4 Answers4

121

For that, you have to talk to BotFather.

  1. In the Telegram App, open the chat with BotFather.

  2. Send him /setcommands. He will present you with a list of your bots.

  3. Pick the bot for which you want to set the command menu.

  4. Compose and send the command list. Using your image as an example, these 4 lines should do:

start - Description 1
menu - Description 2
help - Description 3
stop - Description 4

Note that command part of each line(left side of - signs) must have just lowercase characters, and no slashes. There should also be spaces around the - sign.

Once you complete this process, exit and kill the Telegram App. Re-open it, go to the chat with your target bot, type a / (or tab on the / button next to the text field), the command menu should come up.

hamed
  • 7,939
  • 15
  • 60
  • 114
Nick Lee
  • 5,639
  • 3
  • 27
  • 35
  • Thanks a lot for the information, i think there is no way to control admin and user commands. if you don't want users to see your admin commands you should not put them here. – GurhanCagin Dec 03 '18 at 07:13
  • 3
    EDIT ROLLBACK: ***Note that each command should be written in lowercase, and that the command may not begin with a /slash. There should also be spaces around the - sign. Otherwise it won't work. – JaFizz Jan 12 '20 at 00:07
  • No need to restart Telegram, works instantly –  vrnvorona Nov 24 '22 at 10:40
8

New dynamic way to set commands

Telegram introduced a separate method setMyCommands which allows you to set commands via API directly from your code.

{
  "commands": [
    {
      "command": "start",
      "description": "Start using bot"
    },
    {
      "command": "help",
      "description": "Display help"
    },
    {
      "command": "menu",
      "description": "Display menu"
    }
  ],
  "language_code": "en"
}

Moreover, it allows you to customize commands per language code with language_code parameter

Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
2

Without json.dumps([]) I get error response from Tlg: {'ok': False, 'error_code': 400, 'description': "Bad Request: can't parse commands JSON object"}

The code below works as expected.

{
  "commands": json.dumps([
    {
      "command": "start",
      "description": "Start using bot"
    },
    {
      "command": "help",
      "description": "Display help"
    },
    {
      "command": "menu",
      "description": "Display menu"
    }
  ])
}
Jerry
  • 21
  • 3
0
<?php
$comandos = [
["command" => "a", "description" => "aaa"],
["command" => "b", "description" => "bbb"],
["command" => "c", "description" => "ccc"],
];

defineMenuOptions($comandos);

function defineMenuOptions($comandos) {

$comandosEnc = "setMyCommands?commands=" . json_encode($comandos);
$retorno = file_get_contents(API_URL.$comandosEnc);

}
?>